WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Determine If Menu Bar Has Been Clicked


Question:

I've been banging my head against the wall over this one for weeks. Can anyone help me?

I'm trying to determine if a window's menu bar has been clicked on (so I can avoid SendKeying at an inappropriate time). I thought using the GetMenuBarInfo API function should be able to help me but I cannot get the function to work.

Code I've been using:

OBJID_CLIENT = -4
OBJID_MENU = -3
OBJID_SYSMENU = -1

w_title = "~Notepad"
hwnd = dllhwnd(w_title)
terminate(!hwnd, "bah", "window not found")

; set up the MENUBARINFO structure (do I need this?)
structsize = 4 + 4*4 + 4 + 4 + 4 + 4
menubarinfo = binaryalloc(structsize)
binaryeodset(menubarinfo, structsize)
binarypoke4(menubarinfo, 0, structsize)

; call the API
menu0 = dllcall("user32.dll", long:"GetMenu", long:hwnd)  ; handle to main menu
ismenu = dllcall("user32.dll", long:"IsMenu", long:menu0)  ; this returns 1 (success)
success = DllCall("user32.dll", long:"GetMenuBarInfo", long:menu0, long:OBJID_MENU, long:0, lpbinary:menubarinfo)  ; but haven't made this succeed yet no matter what the OBJID
; look at fBarFocused member of MENUBARINFO structure here...
Would be really grateful if anyone had any ideas.

Answer:

Looking at the source code for the MFC implementation of the function, I would guess you need the Windows handle (hwnd) to the app's main window instead of the handle to menu as the first parameter.

However, the function still does not work with this change ( the error number returned from DllLastError() just changes) so there is something else wrong.

User Reply:

Not a guru but MSDN has this to say about the second member of the MENUBARINFO structure: "rcBar - Pointer to a RECT structure that specifies the coordinates of the menu bar, popup menu, or menu item."

With this in mind I changed the structsize to 24 and added these lines:

RECT = binaryalloc(16)
pRect = IntControl(42, RECT, 0, 0, 0)
binarypoke4(menubarinfo, 4, pRECT)
but no joy.

Answer:

Yup, the docs are incorrect. If you look at WinUsers.h you will see that the member rcBar is a RECT and not an LPRECT.

The last two members of the MENUBARINFO structure are declared as bit fields. Change the structure size from 36 to 32 bytes and it should work.


Article ID:   W16458
File Created: 2005:02:18:12:20:50
Last Updated: 2005:02:18:12:20:50