΢ͻӦ
MICROCOMPUTER & ITS APPLICATIONS
2000 Vol.19 No.3 P.19-20



32λWindowsӦóʵ⼼
㡡ΰľ
ժҪ32λWindowsеʵ⼼о,Ըּ⼼˶Աȡ
ؼʣӦóʵ ʵ API
ϵͳWindowsûͬʱж,1Ӧó,ҲԴڴеĶ,ÿӦó1ڴ渱ΪʵһЩʵϵͳ,Ҫʵп,ֻӦó1ʵʱ,ڽҪǷ1ʵ16λWindowsӦó,ͨϵͳ͸WinMainĵ2hPrevInstance,ʵĴڡWinMainڽںͽϢѭ֮ǰ,hPrevInstanceֵ,˲ֵΪ˵ӦóʵѾڡȻ32λWindows,hPrevInstanceֵԶΪ,ΪжӦóʵǷڵı־DelphiΪ,32λWindowsӦóʵĸּ⼼
1 ڲҷ
ӦóѾ1ʵ,ôͨFindWindowAPIԼ⵽ʵĴھFindWindow2:ƺʹڱ,ǰ1APIRegisterWindowעᴰʱʹõWNDCLASS,Delphi,òӦóڵơWindows SDKƵĳ,ֵ1ʵWindowsעᴰ,ʵڴ֮ǰ,ҪⴰǷѾע,ûע,˵Ӧóĵ1ʵ˿DelphiDPRļжԳ¿:
//Test1.DPR
var hWnd:Thandle;
begin
//Ⲣȡھ
HWnd:=FindWindow
TMyMainForm,//
Test App Instance//⣨Ϊգ
;
if hWnd=0 then begin
Application.CreateForm
TmyMainForm,
MyMainForm
;
Application.Run;
end;
end.
ڷDelphi IDEп⵽ǰ1ʵڣ,ֻ֤Ӧó1ʵСȻ,DelphiIDEиóԶ!Ϊڹļ򿪵,ڴʼմ1Ӧóʱ̵ڡ
2 öٴڷ
ڲҷ޷ӦóǰʵIDEеʱĴʵ,Windows APIEnumWindows˸ĽöٺEnumWindowsҪ2,1ָ1ʵʽҪҵĿ괰뵱ǰеĴڽƥĻصMatchProcڴַ,2ڷҵĿ괰ڵľϵͳ˳ؽǰеÿڴݸصMatchProc,ֱյķֵΪ;յĴĿ괰ڽбȽ,ɹƥ,򷵻ؼ,֪ͨϵͳ1;򷵻,֪ͨϵͳֹͣݡ
//Test2.DPR
var hWnd:Thandle;
begin
hWnd:=0;
EnumWindProc
@MatchProc,
LONGINT@hWnd
;
if hWnd=0 then begin
Application.CreateForm
TMyMainForm,
MyMainForm
;
Application.Run;
End.
//ƥĻص
function MatchProc
hWndFromOS:THandle;
hMatchedWnd:^HWND;
bool;
stdcall;
var str1,str2:string100;
begin
Result:=True;
//ô
GetClassName
hWndFromOS,
PCharstr,
100
;
str1:=PCharstr;
if str1=TmyMainForm then begin
//ƥ,ȽϿִļ
GetModuleFileName
hInstance,
PCharstr1,
100
;
str1:=PCharstr1;
GetModuleFileName
GetWindowLong
HWndFromOS,
GWL_HINSTANCE
,
PCharstr2,
100
;
str2:=PCharstr2;
if str1=str2 then begin
hMatchedWnd^:=hWndFromOS;
Result:=False;
end;
end;
end;
ƥ,񴰿ڲҷдƱȽ,ȽӦóִļ,˷˴ڲҷĲ㡣ʹʱ̵Delphi IDE,Ҳ
3 ź
32λWindowsϵͳ,Microsoft˻ź,üҪӦڽ̼乲Դͬ,˻źڱʾ1ʵѾڡӦóʱ,ȵAPICreateMutex,ĳһƣΪFirstInstance1ź,Ȼ󷢳WaitForSingleObject API,źûбռ,WaitForSingleObject;֮,źѱռ,WaintForSingleObjectȴ,ֱźͷš
//Test3.DPR
var hMutex:THandle;
begin
hMutex:=CreateMutex
nil,
False,
FirstInstance
;
if WaitForSingleObjecthMutex,0<>
wait_timeout then begin
Application.CreateForm
TMyMainForm,
MyMainForm;
Application.Run;
end;
end.
ĸʵⷽȻDelphi,ƽ̨32λWindowsҲɽ
ʯѧԺѧϵ151400
㣨ʯѧԺѧϵ151400
ΰʯѧԺѧϵ151400
ľ٣ʯѧԺѧϵ151400
ոڣ1999-09-10
