当菜单弹出后, 怎样得到显示菜单的窗口的handle?加500分!!! ( 积分: 200 )

  • 主题发起人 主题发起人 let_it_be
  • 开始时间 开始时间
L

let_it_be

Unregistered / Unconfirmed
GUEST, unregistred user!
这可是高深的问题哦
 
这可是高深的问题哦
 
PopupMenu好像不是必须和窗体关联吧.至于主菜单是和窗体关联的,自然可以得到.起码可以遍历,至于是否有直接的方法,这个需要查阅资料.<br>GetActiveWindow可以得到当前激活的窗口,这个大部分时候应该是正确的
 
讲清楚一点儿。
 
自己写的一个小代码,列出所有窗口HANDLE<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls,PSAPI;<br>const<br> &nbsp;CM_MAIN =WM_USER+$2010;<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br><br> &nbsp; &nbsp;Procedure ListAllActiveWnd();<br><br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;<br> &nbsp;<br>implementation<br><br>{$R *.DFM}<br><br>Procedure TForm1.ListAllActiveWnd();<br><br> &nbsp;type<br> &nbsp; &nbsp;PStringList = ^TStringList;<br><br> &nbsp;Function EnumWndProc(hWin: HWND; Param: LPARAM): BOOL; stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;szBuf: array[0..100] of Char;<br><br> &nbsp;begin<br> &nbsp; &nbsp;// 取得所有可見的視窗<br> &nbsp; &nbsp;//if (IsWindow(hWin) and IsWindowVisible(hWin)) then<br> &nbsp; &nbsp;if (IsWindow(hWin) ) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;// 只取最上層可見視窗<br> &nbsp; &nbsp; &nbsp;if (Windows.GetParent(hWin) = 0) then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FillChar(szBuf, SizeOf(szBuf), 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;GetWindowText(hWin, szBuf, SizeOf(szBuf));<br> &nbsp; &nbsp; &nbsp; &nbsp;if string(szbuf)='mainform' then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PostMessage(hWin, CM_MAIN, 0, 2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (szBuf &lt;&gt; '') then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PStringList(Param)^.AddObject(szBuf, TObject(hWin));<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;Result := TRUE;<br> &nbsp;end;<br><br>var<br> &nbsp;i: Integer;<br> &nbsp;slWnd: TStringList;<br> &nbsp;dwProcess1, dwProcess2: DWORD;<br> &nbsp;cnt:DWORD;<br> &nbsp;szModName:array [0..255]of char;<br> &nbsp;hProc:THandle;<br> &nbsp;hMod: HMODULE;<br>begin<br> &nbsp;slWnd := TStringList.Create;<br> &nbsp;// 用 EnumWindows 列舉出所有可能的視窗, 並記在TStringList中<br> &nbsp;EnumWindows(@EnumWndProc, LPARAM(@slWnd));<br><br> &nbsp;Memo1.Lines.Clear;<br><br> &nbsp;// 比對所有可能的視窗, 去掉屬於同一個Process的視窗<br> &nbsp;// 最後一個一定是Program Manager, 所以loop只到slWnd.Count - 2, 直接去掉<br> &nbsp;// Program Manager<br> &nbsp;for i := 0 to slWnd.Count-2 &nbsp;do<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowThreadProcessId(HWND(slWnd.Objects), @dwProcess1);<br> &nbsp; &nbsp;GetWindowThreadProcessId(HWND(slWnd.Objects[i + 1]), @dwProcess2);<br> &nbsp; &nbsp;if dwProcess1 &lt;&gt; dwProcess2 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;//Memo1.Lines.Add(slWnd.Strings);<br> &nbsp; &nbsp; &nbsp;// 以下為取得執行檔名稱的過程<br> &nbsp; &nbsp; &nbsp;hProc:= OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, dwProcess1);<br> &nbsp; &nbsp; &nbsp;if (hProc)&lt;&gt;0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FillChar(szModName, SizeOf(szModName), 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;EnumProcessModules(hProc, @hMod, SizeOf(hMod), cnt);<br> &nbsp; &nbsp; &nbsp; &nbsp;if GetModuleFileNameEx(hProc, hMod, szModName,sizeof(szModName))&lt;&gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add(slWnd.Strings+'---&gt;'+szModName);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hProc);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;slWnd.Free;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;ListAllActiveWnd;<br>end;<br><br>end.
 
可以得到, 利用 mennu 的 OwnerDraw
 
procedure TForm1.drawMenuItem(Sender: TObject;<br> &nbsp;ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);<br>var<br> &nbsp; mWnd: HWND;<br>begin<br> &nbsp; mWnd := WindowFromDC(ACanvas.Handle);<br> &nbsp; memo1.Lines.Add(inttostr(mWnd));<br>end;
 
接受答案了.
 

Similar threads

回复
0
查看
818
不得闲
D
回复
0
查看
841
DelphiTeacher的专栏
D
D
回复
0
查看
847
DelphiTeacher的专栏
D
D
回复
0
查看
681
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部