Why is this not working? (GetSubMenu)
Hi guys, I want to be able to get the menu and its submenu rectangles. I tried the code below but without success. I use “WindowFromPoint” to get the handle of the window that the mouse is on and use the “GetSubMenu” function to get the menu’s activated submenu. I successfully can retrieve the menu handle by using “WidowFromPoint” but the “GetSubMenu” function returns zero. Can some one take a look on this and tell what I am doing wrong. Thanks in advance.
vb Code:
Imports System.runtime.InteropServices
Public Class Form1
Declare Function GetSubMenu Lib "user32" Alias "GetSubMenu" (ByVal hMenu As IntPtr, ByVal nPos As Integer) As IntPtr
<DllImport("user32", EntryPoint:="WindowFromPoint")> _
Public Shared Function WindowFromPoint(ByVal xPoint As Integer, _
ByVal yPoint As Integer) As IntPtr
End Function
End Class
I call the functions in the keydown event.
vb Code:
Dim hwind As IntPtr = WindowFromPoint(Control.MousePosition.X, Control.MousePosition.Y) ' <-- returns the menu handl
Console.WriteLine("Menu " & hwind.ToString)
Dim hsubMenu As IntPtr = GetSubMenu(hwind, 0) ' <-- returns 0
Console.WriteLine("SubMenu " & hsubMenu.ToString)
Re: Why is this not working? (GetSubMenu)
Because menu handle are not windows handles
You use something like this:
1. Find the window with WindowFromPoint
2. Use that handle and get its menu handle with GetMenu.
3. Once all the handles are correct and if you want a specific sub menu item, use GetSubMenu...
BTW: You sould use msdn to see the correct param values
Re: Why is this not working? (GetSubMenu)
I am sorry but can you be more specific. I am sure that WindowFromPoint function returns the menu or the container of the menu handle because when I call the GetWindowRect function I get the menu’s rectangle.
Re: Why is this not working? (GetSubMenu)
Quote:
Originally Posted by Kal-El
Because menu handle are not windows handles
You use something like this:
1. Find the window with WindowFromPoint
2. Use that handle and get its menu handle with GetMenu.
3. Once all the handles are correct and if you want a specific sub menu item, use GetSubMenu...
BTW: You sould use msdn to see the correct param values
I also did that but GetMenu function returns zero.
Re: Why is this not working? (GetSubMenu)
Quote:
Originally Posted by VBDT
I am sorry but can you be more specific. I am sure that WindowFromPoint function returns the menu or the container of the menu handle because when I call the GetWindowRect function I get the menu’s rectangle.
No sir, see msdn, only retrieves a handle to the window, no menus. And you getting error zero, because you are using invalid handle values for each API function. To get rectangles or boundies, you can also see GetMenuItemRect.
Re: Why is this not working? (GetSubMenu)
I am already using the WindowFromPoint function to retrive the rectangle of the menu and it works. The msdn doesn't say that it doesn't return menu handle. I am not saing that the handle it returns is menu hadle but it might be menu's container handle.
I don't want menuItemRectangle i want the menu and its submenu rectangles.
Re: Why is this not working? (GetSubMenu)
Sorry for the confusion, I mean shortcut menu. The menu that when you right click on a window or the desktop and it pops up.