如何关闭一个外部窗口(不是程序)?难道用wm_close不行?(50分)

  • 主题发起人 主题发起人 iseek
  • 开始时间 开始时间
I

iseek

Unregistered / Unconfirmed
GUEST, unregistred user!
我用findwindow找到一个窗口(不是程序)的句柄,然后试图关闭它,却不成功。<br>sendMessage(handle,wm_close,0,0);<br>请大虾出手。<br>您要觉得50不够,俺就再加50。
 
var hd: HWND;<br>begin<br>&nbsp; hd := FindWindow(nil,'未定问题 - 记事本');<br>&nbsp; if hd&lt;&gt;0 then<br>&nbsp; &nbsp; PostMessage(hd,WM_Close,0,0);<br>end;<br>
 
我同意以上的看法,不过我试了,似乎不行!
 
必须 DestroyWindow(hWnd)才行。<br>因为 WM_CLOSE 消息可能被屏蔽,<br>就象我们处理窗体的 Action := caNone 一样。<br>DestroyWindow()能强制销毁窗口。
 
&gt;&gt;必须 DestroyWindow(hWnd)才行<br>能不能给两句代码?或者,hWnd用findWindow得到吗?<br>另外,我再试一下,如果行,明儿或后天给你分。<br>iseek
 
下面是CloseWindow()API的说明:<br>//说明:WM_CLOSE相应的处理函数就是CloseWindow();<br>//WM_DESTROY相应的函数是DestroyWindow();<br><br>The CloseWindow function minimizes (but does not destroy) the specified window. <br><br>BOOL CloseWindow(<br><br>&nbsp; &nbsp; HWND hWnd // handle to window to minimize<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>hWnd<br><br>Identifies the window to be minimized. <br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is nonzero.<br>If the function fails, the return value is zero. <br>To get extended error information, call GetLastError. <br><br>Remarks<br><br>The window is minimized by reducing it to the size of an icon <br>and moving the window to the icon area of the screen. <br>Windows displays the window's icon instead of the window and <br>draws the window's title below the icon. <br>To destroy a window, an application must use the DestroyWindow function. <br>-------------------------------------------------------------------------<br>//注意划线标注的说明<br><br><br>所以直接处理WM_DESTROY能绕过这一情况而强制关闭窗口。<br>DestroyWindow(HWND hWnd)中的 hWnd 就是你 FindWindow()<br>返回的窗口句柄。<br><br>参考Sachow的代码:<br>var hd: HWND;<br>begin<br>&nbsp; hd := FindWindow(nil,'未定问题 - 记事本');<br>&nbsp; if hd&lt;&gt;0 then &nbsp; &nbsp; &nbsp; &nbsp; //其实应该是:hd &gt; 0.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //因为HWND 是一个DWORD类型,不可能 &lt; 0。<br><br>&nbsp; &nbsp; PostMessage(hd,WM_Close,0,0);<br>&nbsp; &nbsp;------------------------------<br><br>&nbsp; &nbsp; //把PostMessage(hd, WM_CLOSE, 0, 0)改为:<br>&nbsp; &nbsp; //PostMessage(hd,WM_DESTROY,0,0);或者<br>&nbsp; &nbsp; //SendMessage(hd,WM_DESTROY,0,0);或者直接套用句柄:<br>&nbsp; &nbsp; //DestroyWindow(hd);就可以了。<br>end;<br>
 
sachow的postmessage完全行,永远的学徒的热心令我感动。<br>给sachow40分,因为他先给出了正确答案。永远的学徒10分。<br>谢谢各位。<br>——iseek<br>
 
uses tlhelp32;<br>假设要终止的程序的文件名为:project2.exe,那么例程如下:<br>var<br>lppe:tprocessentry32;<br>sshandle:thandle;<br>hh:hwnd;<br>found:boolean;<br>begin<br>sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);<br>found:=process32first(sshandle,lppe);<br>while found do<br>begin<br>&nbsp; //进行你的处理其中lppe.szExefile就是程序名。<br>&nbsp; if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then<br>&nbsp; begin<br>&nbsp; &nbsp; hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);<br>&nbsp; &nbsp; TerminateProcess(hh,0);<br>&nbsp; end;<br>&nbsp; found:=process32next(sshandle,lppe);<br>end;<br>end;
 
sachow的postmessage完全行,永远的学徒的热心令我感动。<br>给sachow40分,因为他先给出了正确答案。永远的学徒10分。<br>谢谢各位。<br>——iseek<br>kingkong到我的另一个问题上取分去<br>
 
后退
顶部