求API函数,难!! (300分)

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

ysai

Unregistered / Unconfirmed
GUEST, unregistred user!
已知Word或IE的句柄,怎么取得其菜单项的句柄? <br>例如我要取得 打开... 菜单的句柄,和工具栏按钮的句柄<br>同样,如何取IE的菜单句柄<br><br>Word的菜单与众不同(我是Office XP)<br>工具栏上的菜单项,如文件菜单项的隶属关系是<br>OpusApp &nbsp;(WORD应用程序)<br>MsoCommandBarDock &nbsp;(停靠的地方)<br>MsoCommandBar &nbsp;(工具栏菜单项)<br><br>而下拉的菜单竟然直接隶属于WORD应用程序???!!!!!<br>OpusApp &nbsp;(WORD应用程序)<br>MsoCommandBarPopup &nbsp;(弹出菜单项,如文件中的打开...)<br><br>而IE中的菜单(就是我的电脑)更牛了<br>主菜单的隶属关系是<br>CabinetWClass &nbsp;(我的电脑)<br>WorkerW &nbsp;(未知,应是工具栏的停靠控件吧)<br>ReBarWindow32 &nbsp;(工具栏)<br>ToolBarWindow32 &nbsp;(主菜单,如文件)<br><br>文件中的新建菜单类名是<br>#32768<br>竟然没有父窗口???!!!晕倒<br><br>我该怎么取出来???<br><br>http://expert.csdn.net/Expert/topic/1413/1413913.xml?temp=.2750055
 
我不知道,但是帮你up。
 
关注此题。
 
你为什么要它的菜单,如果是要填加项的话,直接调用它的COM接口好象就可以了。
 
我并不只想取得WORD和IE的菜单句柄,还有其它所有未知应用程序的菜单句柄<br><br>我用EnumChildWindow枚举所有子窗体,用GetMenu取得菜单,但像上面的情况,<br>我应该用什么方法取得句柄呢?不止菜单,像上面的ToolBarWindow32中的按钮<br>的句柄也取不到!不可能所有用ToolBarWindow32控件的应用程序都提供接口吧
 
我已帮你做好了<br>到这里看看。<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1435428
 
还是贴出来吧<br>昨晚干到3点多,效果还是不是很理想。<br>主要是没有正确得到菜单中子菜单的句柄。高手们有没有好办法呢?<br>现在公开源代码,和大家一起研究研究。<br>如果这个问题解决,我想将功能扩张到拖动窗体时也将窗体变为半透明。这个功能我试<br>了,基本一能实现了。到时我也会将代码献出来给大家!<br>希望大家不会也UP一下!<br><br>以下是代码,功能当然是使用钩子实现<br><br>library HookMenu;<br><br>{*************************************************************}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp;HookMenu Library,Copyright lfpsoft 2002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; All rights reserverd. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; Bug Report : lfpsoft@163.net &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; WEB : http://www.lkgarden.com/lfpsoft &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* 效果不是很好,因为到现在我还没有想出如何得到菜单中的子菜单*}<br>{* 好方法,只能在WM_MENUSELECT这个消息里得到该子菜单的句柄 &nbsp; *}<br>{* 现在发布源代码,希望高手们指点指点,或者大家共同研究 &nbsp; &nbsp; &nbsp;*}<br>{* 还有手头上有基本实现当拖动窗体时就显示半透明的代码,过些天*}<br>{* 整理好后我会再放出来大家研究研究 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{*************************************************************}<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; HookMenuProc in 'HooKMenuProc.pas';<br><br>{$R *.RES}<br><br>exports<br>&nbsp; EnableMenuHook,<br>&nbsp; DisableMenuHook,<br>&nbsp; MenuHookExit,<br>&nbsp; SetAlpha,<br>&nbsp; SetTrayAlpha;<br><br>begin<br>&nbsp; IntoShare;<br>end.<br><br>unit HooKMenuProc;<br><br>{*************************************************************}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp;HookMenu Library,Copyright lfpsoft 2002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; All rights reserverd. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; Bug Report : lfpsoft@163.net &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; WEB : http://www.lkgarden.com/lfpsoft &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}<br>{* 效果不是很好,因为到现在我还没有想出如何得到菜单中的子菜单*}<br>{* 好方法,只能在WM_MENUSELECT这个消息里得到该子菜单的句柄 &nbsp; *}<br>{* 现在发布源代码,希望高手们指点指点,或者大家共同研究 &nbsp; &nbsp; &nbsp;*}<br>{* 还有手头上有基本实现当拖动窗体时就显示半透明的代码,过些天*}<br>{* 整理好后我会再放出来大家研究研究 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*}<br>{*************************************************************}<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils;<br>var<br>&nbsp; hNextHookProc: HHook;<br>&nbsp; procSaveExit: Pointer;<br><br>&nbsp;function MenuHookHandler(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>&nbsp;function EnableMenuHook: BOOL; export;<br>&nbsp;function DisableMenuHook: BOOL; export;<br>&nbsp;procedure MenuHookExit; far;<br>&nbsp;procedure IntoShare; stdcall;export;<br>&nbsp;procedure SetAlpha( bAlpha: Byte );stdcall; export;<br>&nbsp;procedure SetTrayAlpha( bAlpha: Byte);stdcall; export;<br><br>implementation<br><br>type<br>&nbsp; TGoData = record //将设置半透明的值共享到内存中的数据结构<br>&nbsp; &nbsp; bAlpha: byte;<br>&nbsp; end;<br>&nbsp; PGoData = ^TGoData;<br><br>const<br>&nbsp; user32 &nbsp; &nbsp; &nbsp; &nbsp;= 'user32.dll';<br><br>var<br>&nbsp; GoData : PGoData;<br>&nbsp; MemFile : THandle;<br><br>procedure GetWindowsVersion(var Major : integer;var Minor : integer);<br>var<br>&nbsp; l : longint;<br>begin<br>&nbsp; l := GetVersion;<br>&nbsp; Major := LoByte(LoWord(l));<br>&nbsp; Minor := HiByte(LoWord(l));<br>end;<br><br><br><br>procedure SetAlpha( bAlpha: Byte );stdcall; export; &nbsp; //设置半透明值<br>begin<br>&nbsp; if bAlpha &lt;10 then bAlpha :=10;<br>&nbsp; GoData^.bAlpha &nbsp;:= bAlpha;<br>end;<br><br>procedure SetWndAlpha(MenuHwnd:hwnd;const Alpha: Byte); &nbsp;// 设置半透明<br>var<br>&nbsp; major, minor : integer;<br><br>&nbsp; User32: Cardinal;<br>&nbsp; l: Longint;<br>&nbsp; SetLayeredWindowAttributes: function (hwnd: LongInt; crKey: byte; bAlpha: byte; dwFlags: LongInt): LongInt; stdcall;<br>begin<br>&nbsp; &nbsp; &nbsp;GetWindowsVersion(major, minor);<br>&nbsp; &nbsp; &nbsp;if ((major &gt;= 5) and (minor &gt;= 0)) then //判断是否是WIN2000以上的版本。<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; User32 := LoadLibrary('user32');<br>&nbsp; &nbsp; &nbsp; &nbsp; if User32 &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLayeredWindowAttributes := GetProcAddress(user32, 'SetLayeredWindowAttributes');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @SetLayeredWindowAttributes &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l := GetWindowLong(MenuHwnd, GWL_EXSTYLE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l := l or WS_EX_LAYERED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetWindowLong(MenuHwnd, GWL_EXSTYLE, l);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLayeredWindowAttributes(MenuHwnd,0,Alpha,LWA_ALPHA);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeLibrary(User32);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>//枚举所有窗体,如果是菜单或是历史菜单就设置半透明<br>function EnumWindowsProc(hWindow:hWnd;lParam:LongInt) : Bool {$IFDEF Win32} stdcall;{$ELSE}:Export;{$ENDIF}<br>var<br>&nbsp; csCompare,csClassName:String;<br>&nbsp; szClassname:Array[0..128] of Char;<br>begin<br><br>&nbsp; if ((lParam = 0) or (lParam = 2)) then csCompare := 'BaseBar'<br> else if (lParam = 1) then csCompare := '#32768';<br><br>&nbsp; GetClassName(hWindow, szClassname, 128);<br>&nbsp; csClassName := Trim(szClassname);<br> if csClassName = csCompare &nbsp;then<br>&nbsp; begin<br> if (lParam = 2) then SetWndAlpha(hWindow,255)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp;SetWndAlpha(hWindow,GoData^.bAlpha);<br>&nbsp; end;<br> result :=true;<br>end;<br><br><br>//钩子程序<br>function MenuHookHandler(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>&nbsp; szClassname:Array[0..128] of Char;<br>&nbsp; cwp: CWPRETSTRUCT;<br>&nbsp; hwndMenu:HWND;<br>&nbsp; csClassname:String;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; If iCode &lt; 0 Then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; &nbsp; Exit;<br>&nbsp;end;<br> cwp := PCWPRETSTRUCT(lParam)^;<br><br>&nbsp; if((cwp.message = WM_CREATE) or (cwp.message = WM_INITMENUPOPUP) or<br>&nbsp; (cwp.message = WM_INITMENU) or (cwp.message = WM_MENUSELECT)) then<br>&nbsp; begin<br>&nbsp; if cwp.message = WM_CREATE then<br>&nbsp; &nbsp; begin<br> &nbsp;hwndMenu := cwp.hwnd;<br> GetClassName(hwndMenu, szClassname, 128);//取得类名<br>&nbsp; &nbsp; &nbsp; csClassname := Trim(szClassname);<br> if ((csClassname ='#32768') or (csClassname = 'BaseBar')) then<br> begin &nbsp; &nbsp;//如果是菜单或历史菜单就...<br>&nbsp; &nbsp; &nbsp; &nbsp; SetWndAlpha(hwndMenu,GoData^.bAlpha) ;<br> end;<br>&nbsp; &nbsp; end<br> else<br>&nbsp; &nbsp; begin<br> EnumWindows(@EnumWindowsProc, 1);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp;Result := CallNextHookEx( hNextHookProc, iCode, wParam, lParam);<br>end;<br><br>//挂钩子<br>function EnableMenuHook: BOOL; export;<br>begin<br>&nbsp; Result := False;<br>&nbsp; EnumWindows(@EnumWindowsProc, 0);<br>&nbsp; if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp; hNextHookProc := SetWindowsHookEx(WH_CALLWNDPROCRET,<br>&nbsp; &nbsp; MenuHookHandler,<br>&nbsp; &nbsp; HInstance,<br>&nbsp; &nbsp; 0);<br>&nbsp; Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br>//取消钩子<br>function DisableMenuHook: BOOL; export;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; SetWndAlpha(FindWindow('Shell_TrayWnd', nil),255);<br>&nbsp; &nbsp; EnumWindows(@EnumWindowsProc, 2);<br>&nbsp; &nbsp; UnhookWindowsHookEx(hNextHookProc);<br>&nbsp; &nbsp; hNextHookProc := 0;<br>&nbsp; end;<br>&nbsp; Result := hNextHookProc = 0;<br>end;<br><br>//退出钩子<br>procedure MenuHookExit;<br>begin<br>&nbsp; SetWndAlpha(FindWindow('Shell_TrayWnd', nil),255);<br>&nbsp; if hNextHookProc &lt;&gt; 0 then DisableMenuHook;<br>&nbsp; ExitProc := procSaveExit;<br>end;<br><br>//将要设置半透明的值共享到内存中去<br>procedure IntoShare; stdcall;export;<br>begin<br>&nbsp; MemFile := OpenFileMapping( FILE_MAP_WRITE, False, 'CCSOFT' );<br>&nbsp; if MemFile = 0 then<br>&nbsp; &nbsp; MemFile:=CreateFileMapping( $FFFFFFFF, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PAGE_READWRITE, 0, SizeOf( TGoData ), 'CCSOFT');<br>&nbsp; GoData := MapViewOfFile( MemFile, FILE_MAP_WRITE, 0, 0, 0 );<br>&nbsp; if MemFile = 0 then<br>&nbsp; FillChar( GoData^, SizeOf( TGoData ),0);<br>end;<br><br>procedure SetTrayAlpha( bAlpha: Byte);stdcall;export; //设置任务栏半透明<br>begin<br>&nbsp; &nbsp;SetWndAlpha(FindWindow('Shell_TrayWnd', nil),bAlpha);<br>end;<br><br>end.<br><br>//测试代码<br>unit main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, ComCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; TrackBar1: TTrackBar;<br>&nbsp; &nbsp; CheckBox1: TCheckBox;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; procedure TrackBar1Change(Sender: TObject);<br>&nbsp; &nbsp; procedure CheckBox1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; //调用DLL<br>&nbsp; procedure EnableMenuHook; stdcall;external 'HookMenu.dll';<br>&nbsp; procedure MenuHookExit; stdcall;external 'HookMenu.dll';<br>&nbsp; procedure DisableMenuHook; stdcall;external 'HookMenu.dll';<br>&nbsp; procedure SetAlpha( bAlpha : Byte ); stdcall;external 'HookMenu.dll';<br>&nbsp; procedure SetTrayAlpha( bAlpha: Byte);stdcall; external 'HookMenu.dll';<br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br><br>&nbsp; &nbsp; setalpha(100);<br>&nbsp; &nbsp; SetTrayAlpha( 100);<br>&nbsp; &nbsp; EnableMenuHook;<br><br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; &nbsp; MenuHookExit;<br>end;<br><br><br>procedure TForm1.TrackBar1Change(Sender: TObject);<br>begin<br>&nbsp; &nbsp; setalpha(trackbar1.Position);<br>&nbsp; &nbsp; SetTrayAlpha( trackbar1.Position);<br>end;<br><br>procedure TForm1.CheckBox1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; if CheckBox1.Checked then<br>&nbsp; &nbsp; &nbsp; &nbsp;EnableMenuHook<br>&nbsp; &nbsp; else DisableMenuHook;<br><br>end;<br><br>end.
 
&nbsp;var fm,cm:Hmenu;<br>begin <br>fm:=getmenu(hwnd); &nbsp; //取得窗口菜单的句柄。<br>cm:=getsubmenu(fm,1); &nbsp;//取得窗口菜单第一菜单项子菜单的句柄。<br>end;<br>&nbsp; 获得句柄后可以调用removemenu()、insertmenu()等api函数操纵菜单。
 
怎么还不给分呀?
 
to wuan:<br>&nbsp; 请试试再说<br>to lfpsoft:<br>&nbsp; 近段时间太忙,你的代码还没测试,不过看了一下,不符合我的要求,我是要根据应用程序或<br>窗体的句柄取得其菜单的句柄,而你的程序只是用hook把所有菜单设为半透明,而且相信你的<br>程序对WORD的菜单没有作用.<br>&nbsp; 我的要求很简单,只要有个函数能像EnumChildWindow一样枚举出像WORD,IE这样的程序的<br>菜单和工具栏按钮的句柄,但有没有这样的函数很难说,因为连SPY++都无能为力.....
 
很简单呀,<br>将如下代码变换下就不可以了吗?<br>if ((csClassname ='#32768') or (csClassname = 'BaseBar')) then<br>改成 ccClassname=你的程序程序的类名 就可以啦。<br>不过如果要取得WORD的菜单就无能为力了。因为用正常的方法很难得到它的句柄。
 
楼上的老兄,我是要根据一个句柄枚举出所有菜单项!并不是用HOOK抓菜单.<br>我能得到WORD或IE的菜单句柄(工具栏按钮的得不到),但不是用我想要的方法.<br>如果谁有得到工具栏按钮的句柄的方法那就更好了(到底工具栏按钮有没有句柄?)<br>
 
FindWindow(nil,'发送消息');
 
关注。<br>= Microsoft Word 2000 =<br>菜单窗口:<br>Class Name:MsoCommandBar<br>Title:菜单栏<br>父窗口:<br>Class Name:MsoCommandBarDock<br>Title:MsoDockTop<br>父的父窗口:<br>Class Name:OpusApp<br>Title:文档 1 - Microsoft Word
 
wndhandle:=findwindow(nil,mystr); <br>//通过标题获得菜单句柄 <br>mainmenu:=getmenu(wndhandle); <br>//取得顶行菜单数 <br>mainnum:=getmenuitemcount(mainmenu);
 
菜单哪里有什么父窗体啊?<br>就是说 工具栏句柄 和菜单的句柄一点关系都没有!<br>我们点击工具栏出现菜单,不过是调用代码将菜单显示一下而已。<br>
 
菜单有父窗体!但并不是所有的都有.比如你在Delphi中放一个TMainMenu在一个Form里,并加<br>上几个菜单项,然后运行,我可以通过GetMenu函数通过主Form找出菜单的每一个项,但是,弹<br>出菜单无法找出来.<br>我没说工具栏按钮与菜单有关系,只是想得到它们的句柄.并不是根据工具栏按钮找到点击它<br>所弹出的菜单,这是两回事,要分别处理,而且都不容易
 
后退
顶部