這是我在一個(gè)論壇的討論稿,我想這些情況在實(shí)踐程序設(shè)計(jì)過程中會(huì)常碰到,所以就將這些討論資料寫了下來:
作者:e夢(mèng)緣
?
請(qǐng)問用什么方法可以運(yùn)行一個(gè)外部的EXE并返回它的Handle?
WinExec 返回不是該EXE的Handle
ShellExecute, ShellExecuteEx 也不是
CreatePRocess 返回的是 Process Handle
到底要如何才可以返回和 GetWindow 所返回的一樣的 Handle?
Re:
可以用FindWindow()函數(shù),它能夠解決這個(gè)問題,它有兩個(gè)參數(shù):lpClassName:程序的類名;lpWindowName:程序窗體的標(biāo)題。
例如:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
ExeHandle:Thandle;
Begin
//獲得句柄
ExeHandle:=FindWindow('notepad',nil);//'');//返回句柄
//關(guān)閉程序
if ExeHandle<>0 then
SendMessage(ExeHandle,WM_Close,0,0)
Else
application.MessageBox('沒有打開"記事本"程序!','提示',
MB_IconInformation+MB_OK);
end;
?
FindWindow() 方法不是我想要的,因?yàn)檫@里的lpWindowName 具有不確定性,沒有辦法準(zhǔn)確地找到。
我希望能在執(zhí)行這支外部程序里就可以得到它的 Handle.
比如有沒有辦法,通過Process Handle得到它有 
Application Handle 以及 MainForm Handle
Re:
您可以通過lpClassName:程序的類名來準(zhǔn)確地找到。
ExeHandle:=FindWindow('notepad',nil);//返回句柄
 function ExecuteFile(const FileName, Params, DefaultDir: String;
function ExecuteFile(const FileName, Params, DefaultDir: String; 
ShowCmd: Integer): THandle;
var
zFileName, zParams, zDir: array[0..79] of Char;
begin
Result := ShellExecute(Application.MainForm.Handle, nil,
StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
StrPCopy(zDir, DefaultDir), ShowCmd);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
hwd:thandle;
begin
hwd:=ExecuteFile('notepad.exe','','',SW_ShowNormal);
if hwd<>0 then showmessage('哈哈~~'+#13+'我取到handle了,是>>>'+inttostr(hwd));
end;
?
謝謝您的熱心幫助,
但ShellExecute得到的并不是Handle
用FindWindow得到的才是真正的Handle
你可以試一下.
最簡(jiǎn)單的方法就是向你得到的Handle發(fā)一個(gè)WM_QUIT消息,看它是否會(huì)被關(guān)閉.
如:
SendMessage(AHandle, WM_QUIT, 0, 0);
Re:
我看了很多delphi的資料,好像只有FindWindow()函數(shù)是 可以實(shí)現(xiàn)的,至于是否還有其它的方法,我 不太清楚! 
 
最后結(jié)果:
我已從大富翁論壇上得到了滿意的結(jié)論了,在這和大家分享一下:
(大富翁論壇: http://www.delphibbs.com/)
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID : DWord;
HWND : THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID : DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not result then EI.HWND := WND;
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI : TEnumInfo;
begin
EI.ProcessID := PID;
EI.HWND := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EI.HWND;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
SI : TStartupInfo;
PI : TProcessInformation;
H : THandle;
S : String;
begin
ZeroMemory(@SI, SizeOf(SI));
ZeroMemory(@PI, SizeOf(PI));
SI.cb := SizeOf(SI);
if CreateProcess(nil,'CALC.EXE', nil, nil, FALSE, 0 ,nil,nil, SI, PI) then
begin
//注意!
WaitForInputIdle(PI.hProcess, INFINITE);
H := FindMainWindow(PI.dwProcessID);
if H > 0 then
begin
SetLength(S, 255);
GetWindowText(H, PChar(S), 255);
SetLength(S, StrLen(PChar(S)));
ShowMessage(S);
end;
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end;
end;
end.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注