C
cangyu
Unregistered / Unconfirmed
GUEST, unregistred user!
我在service开了一个线程,在这个线程使用findwindows了api来查找TForm1这个窗口,但却找不到想要的窗口句柄。这是为什么啊,哪位高人指点一下,感激不尽!
<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs,tlhelp32, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> ListBox1: TListBox;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>type<br> TWindowInfo = record<br> Handle:LongWord; //窗口句柄<br> ClassStr:String; //窗口类<br> Caption:String; //窗口标题<br> end;<br><br>var<br> List: Array of TWindowInfo;<br><br>//-------------------------------------------<br>function StrCmp(String1, String2: string): boolean; //比较两个字符串--不区分大小写<br>begin<br> Result := False;<br> if lstrcmpi(pchar(String1), pchar(String2)) = 0 then Result := True;<br>end;<br><br>function GetCaption(const hWnd: LongWord): string;<br>var<br> szWindowText: array[0..MAX_PATH] of Char;<br> szTextLength: Integer;<br>begin<br> szTextLength := SendMessage(hWnd, WM_GETTEXT, MAX_PATH, Integer(@szWindowText[0]));<br> szWindowText[szTextLength] := #0;<br> Result := szWindowText;<br>end;<br><br>function GetClassStr(const hWnd: LongWord): String;<br>var<br> szWindowClass: array[0..MAX_PATH] of Char;<br>begin<br> GetClassName(hWnd,szWindowClass,Sizeof(szWindowClass));<br> Result := szWindowClass;<br>end;<br>//-----------------------------------------------------------------------<br><br>function EnumPIDWindows(WinHwnd: LongWord; Param: LongWord): Boolean; stdcall;<br>var<br> PID:dword;<br>begin<br> if WinHwnd=0 then<br> begin<br> Result:=False;<br> Exit;<br> end;<br> GetWindowThreadProcessId(WinHwnd,@PID);<br> if Param=PID then<br> begin<br> SetLength(List, Length(List) + 1);<br> List[High(List)].Handle:=WinHwnd;<br> List[High(List)].ClassStr:=GetClassStr(WinHwnd);<br> List[High(List)].Caption:=GetCaption(WinHwnd);<br> end;<br> Result:=True;<br>end;<br><br>function GetProessID(PressName:String):dword;<br>var<br> Process32: TProcessEntry32;<br> ProcessSnapshot: THandle;<br>begin<br> Result:=0;<br> ProcessSnapshot := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);<br> Process32.dwSize := SizeOf(TProcessEntry32);<br> Process32First(ProcessSnapshot, Process32);<br> repeat<br> if StrCmp(PressName,Process32.szExeFile)then //Result:=Process32.th32ProcessID; //判断进程名-不区分大小写~<br> begin<br> Result:=Process32.th32ProcessID;<br> SetLength(List, 0);<br> EnumWindows(@EnumPIDWindows,Process32.th32ProcessID);<br> end;<br> until not (Process32Next(ProcessSnapshot, Process32));<br> CloseHandle(ProcessSnapshot);<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> I:Integer;<br>begin<br> Form1.Caption:='进程句柄:'+IntToStr(GetProessID(Edit1.Text));<br> if GetProessID(Edit1.Text)>0 then<br> begin<br> Form1.Caption:=Form1.Caption+',窗口数量:'+IntToStr(Length(List));<br> if Length(List)=0 then Exit;<br> ListBox1.Items.Clear;<br> for I:=Low(List) to High(List) do<br> begin<br> ListBox1.Items.Add('窗口句柄:'+IntToStr(List[I].Handle)+',窗口类名:'+List[I].ClassStr+',窗口标题:'+List[I].Caption);<br> end;<br> end;<br>end;<br><br>end.<br>