FindWindow的問題 |
答題得分者是:pceyes
|
lirick2003
一般會員 發表:17 回覆:16 積分:11 註冊:2007-02-23 發送簡訊給我 |
|
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
先看這篇
Win32 API 使用討論區(Delphi) » 如何获取自己在程序中运行的外部EXE的Handle? http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=35055 // 例子整理於后 [code delphi] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; ID: TLabel; Title: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} 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,pchar(edit1.text), 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))); ID.Caption :='App PID= ' inttostr(PI.dwProcessID); Title.caption := 'App Title= ' s; end; CloseHandle(PI.hProcess); CloseHandle(PI.hThread); end; end; end. [/code] // dfm [code delphi] object Form1: TForm1 Left = 140 Top = 125 Width = 544 Height = 375 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ID: TLabel Left = 168 Top = 256 Width = 11 Height = 13 Caption = 'ID' end object Title: TLabel Left = 168 Top = 288 Width = 20 Height = 13 Caption = 'Title' end object Button1: TButton Left = 128 Top = 112 Width = 249 Height = 81 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object Edit1: TEdit Left = 104 Top = 48 Width = 281 Height = 21 TabOrder = 1 Text = 'CALC.EXE' end end [/code]
------
努力會更接近成功
編輯記錄
pceyes 重新編輯於 2008-07-14 22:19:15, 註解 無‧
|
careychen
尊榮會員 發表:41 回覆:580 積分:959 註冊:2004-03-03 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |