FindWindow(50分)

  • 主题发起人 主题发起人 ylw66
  • 开始时间 开始时间
Y

ylw66

Unregistered / Unconfirmed
GUEST, unregistred user!
我在主Form下点击一个button,使一个窗口show出来,但我在主Form下使用FindWindow找show出的窗口,返回的Handle却为0。另该函数的第二个参数是FormName还是FormCaption
 
是FormCaption
 
procedure TForm1.Button4Click(Sender: TObject);<br>var<br>&nbsp; i:integer;<br>begin<br>&nbsp; &nbsp;i:=findwindow(nil,'form2');<br>&nbsp; &nbsp;showmessage(inttostr(i))<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;HWndCalculator : HWnd;<br>begin<br>// find the exist calculator window<br>&nbsp; &nbsp;HWndCalculator := FindWindow(nil, 'Form.Caption'); // close the exist Calculator<br>&nbsp; &nbsp;if HWndCalculator&lt;&gt;0 then<br>&nbsp; &nbsp;PostMessage(HWndCalculator, WM_CLOSE, 0, 0);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; form2.Show;<br>end;
 
难道Form2.HANDLE不行?
 
FindWindow('TForm1', PChar(Caption));
 
如果不是childWindow,findWindow是可以找到的
 
findwindow天生不足,如果窗体没有标题,或者每次标题都改变,怎么找啊?
 
The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. <br><br>HWND FindWindow(<br><br>&nbsp; &nbsp; LPCTSTR lpClassName, // pointer to class name<br>&nbsp; &nbsp; LPCTSTR lpWindowName // pointer to window name<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>lpClassName<br><br>Points to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to the GlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpClassName; the high-order word must be zero. <br><br>lpWindowName<br><br>Points to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is the handle to the window that has the specified class name and window name.<br>If the function fails, the return value is NULL. To get extended error information, call GetLastError.
 
应该使用第一个参数,用窗口类名去 FINDWINDOW
 
同意SupermanTm说的。可以用FindWindow("TForm2",Nil)来找类名称为TForm2的窗口。
 
大家说的很清了。
 
form2是子窗口的话findwindow是找不到的<br><br>"This function does not search child windows. "
 
遍历窗体不可以吗?GetWindow(Handle, GW_PREVHWND)和GW_NEXTHWND<br><br>
 
if not Assigned(frm_3) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; frm_3:=Tfrm_3.Create(Application);<br>&nbsp; &nbsp; &nbsp; frm_3.Show;<br>&nbsp; &nbsp; end<br>&nbsp;else<br>&nbsp; &nbsp; &nbsp; frm_3.show;
 
根据类名找窗体的函数<br>function FindExistingForm(FFormName: string): TForm;<br>var<br>&nbsp; &nbsp; i: integer;<br>begin<br>&nbsp; &nbsp; Result := nil;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; if FFormName = '' then raise Exception.Create('需要类名!');<br>&nbsp; &nbsp; &nbsp; &nbsp; with Application do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0 to ComponentCount - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Components.ClassName = ('T' + FFormName) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := TForm(Components);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; on E: Exception do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage(E.Message)<br>&nbsp; &nbsp; &nbsp; &nbsp; else raise;<br>&nbsp; &nbsp; end;<br>end;<br>
 
后退
顶部