Trả lời mọi thắc mắc của bạn

Liệt kê những bài viết mới nhất

Bài viết mớiNgười viếtVào lúc
paltalk code HdAd
Sun Mar 06, 2016 1:28 am
vb ListView HdAd
Sun Mar 06, 2016 1:00 am
Find occurent of character in string nth HdAd
Sat Jan 16, 2016 10:48 pm
Send listbox items to textbox HdAd
Wed Jan 13, 2016 9:55 pm
[VB] Find word in listbox items HdAd
Wed Jan 13, 2016 7:55 pm
code cho hint2 HdAd
Wed Jan 13, 2016 3:22 pm
[vb] Font RTB HdAd
Tue Dec 29, 2015 10:29 am
[vb] selected text RTB HdAd
Tue Dec 29, 2015 9:06 am
[vb] send unicode text HdAd
Tue Dec 29, 2015 9:03 am
[vb] format multi richtextbox HdAd
Tue Dec 29, 2015 9:01 am
Trang web tai cac bang sang che HdAd
Mon Dec 28, 2015 1:28 pm
[VB] Sử dụng resource của chính chương trình HdAd
Sat Dec 26, 2015 8:56 pm
[vb] Phân biệt class và Module HdAd
Thu Dec 24, 2015 2:39 pm
[VB] MY API and Functions declaration HdAd
Mon Dec 21, 2015 1:45 pm
[vb] Khai báo và sử dụng hàm Setforeground HdAd
Mon Dec 21, 2015 1:29 pm
[VB] Sử dụng hàm Sendmessage để gửi tiếng việt, và format richtextbox HdAd
Mon Dec 21, 2015 1:26 pm
[VB] Find menu context handle/ID by SPY++ HdAd
Fri Dec 18, 2015 1:20 pm
[VB] Find menu context handle/ID - Vu Hai Ninh HdAd
Fri Dec 18, 2015 1:00 pm
[VB] Find menu context handle/ID HdAd
Fri Dec 18, 2015 12:54 pm
[VB] Sleep in VB program HdAd
Thu Dec 17, 2015 7:48 pm
[VB] CLose window if found HdAd
Wed Dec 16, 2015 7:07 pm
[VB] Không cho thay đổi kích cỡ form khi chạy HdAd
Wed Dec 16, 2015 3:28 pm
[VB] Không cho thay đổi text box khi chạy HdAd
Wed Dec 16, 2015 3:22 pm
[VB] Tìm cửa sổ và Button bằng FindWindow & FindWindowsEx HdAd
Wed Dec 16, 2015 3:16 pm
[VB] Xóa text mặc định của text box khi click HdAd
Wed Dec 16, 2015 3:12 pm

You are not connected. Please login or register

[VB] Find menu context handle/ID

Go down  Message [Page 1 of 1]

1[VB] Find menu context handle/ID Empty [VB] Find menu context handle/ID on Fri Dec 18, 2015 12:54 pm



Getting Context Menu Handle

Bit late to post reply for this now [sorry Juergen]... I was asking the same question a while ago about obtaining context menu handle, and it took me several days to figure it out because i couldn't find a ready answer on the net, so I want to post something that might be of help to future googlers... Here we go:

I assume you really want the context menu handle just to be able to programmatically select a menu item from a popup/context menu. Actuallly it turns out that the menu handle is not necessary at all. Try these steps:

1. Use Spy++ tool included in Visual Studio to "Log Messages" on all windows. (very important to select "All-Window" because menu commands may be addressed not the the window we would expect, but to one of its ancestors.) Filter out all other messages other than WM_COMMAND.

2. Open up the context menu (you will see nothing yet on the msg log), open any submenus you need, then select the menuitem you want. Now, there should be one line of WM_COMMAND in the log. This is the command that selects that particular menu item. The wparam and lparam values are always the same, only the handle of the reciepient handle changes with each program run. So we need to obtain that handle programmatically. There are different ways to do this, the way i use is:

3. Note down the handle of the reciepient above. Make sure not to close the target window during the following. I use the freely downloadable WinSpy because I find it easier to follow trees of windows using WinSpy than Spy++. Using WinSpy, starting from the top level window of the target application, expand each layer of windows until you find the window handle noted above. Then note down the ancestors lineage until the top window. (Note: the very top of the application tree is not actually a window.)

4. Use FindWindow (..) API function to get the handle of the top level window. Then FindWindowEx (..) repeated to trace its children until you get the handle to the window you want. (Note. FindWIndowEx() searches only one level of children, so repeated calls is necessary.) For multiple children of the same class and name, you need to call FindWIndowEx (..) again modifying its IndexAfter parameter. 

5. Finally send the same message you see above, WM_COMMAND with the same wparam and lparam using SendMessage(..) API function using the handle you just found.

For more information, refer to Petzold's "Programming Windows 5th ed", chapter10, Menus. I figured out the above only after reading this section a couple of times. Also, reading MSDN documentation on WM_COMMAND and the API functions used above will help to understand what to put in the parameters.

The code for this in C# console application would be something like this:

using System.Runtime.InteropServices;

static void Main(string[] args)
int i = Win32.FindWindow(null, "WindowCaption";
i = Win32.FindWindowEx(i,0, "#32770", null);
i = Win32.FindWindowEx(i,0, "AfxOleControl42", null);
i = Win32.FindWindowEx(i,0, "AfxFrameOrView42", null);
Console.WriteLine("Target window handle: " + i);
Win32.SendMessage (i, 0x0111, 0x000080B7, 0);
//0x0111 stands for for WM_COMMAND


public class Win32
public static extern int FindWindow (string lpClassName, string lpWindowName);

public static extern int FindWindowEx (int hwndParent, int hwndChildAfter, string strClassName, string strWindowName);

[DllImportAttribute ("user32.dll")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam); 

Note: If you really need to obtain the handle of the context menu, try refering to "Win32 GUI Test" open-source source code, that contains an implementation of their GetPopupHandle() function. Basically they hook the window and simulate right button click and somehow obtain the handle... (I only skimmed through the code so I don't understand it fully)

That's all, I hope that helps... Good luck! [VB] Find menu context handle/ID Wave

View user profile

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum