Re: Select menu choice in a .Net application via PostMessage by mr_unreliable
mr_unreliable
Tue Apr 03 17:03:13 CDT 2007
Broeden wrote:
> Hi all,
>
> Can anyone give me a hint how to select a menuchoice in an .Net
> application via a post message.
>
hi Broeden,
The short story is that you can invoke a menu item by posting
a wm_command message along with the menu item ID.
Here is some code on how to do it. Note that the api declarations
are in a format used by one of those untrustworthy and unreliable
third-party controls (ugh!). which allows for calling api's from
script. But you can change the declarations to standard microsoft
declarations if your compiler supports such a thing.
--- <snip> ---
' --- INVOKE MENU ITEM -------------------------
Public Sub InvokeMenuItem(nMainItemPos)
Dim GetMenu ' as object
Set GetMenu = oATO.DeclareAPI("USER32.DLL", "GetMenu", "ByVal hWnd As
Long")
Dim GetMenuItemID ' as object
Set GetMenuItemID = oATO.DeclareAPI("USER32.DLL", "GetMenuItemID",
"ByVal hMenu As Long", "ByVal nPos As Long")
Dim PostMessage : Set PostMessage = oATO.DeclareAPI("USER32.DLL",
"PostMessageA", "ByVal hWnd As Long", "ByVal wMsg As Long", "ByVal
wParam As Long", "lParam As Any")
Dim Sleep : Set Sleep = oATO.DeclareAPI("KERNEL32.DLL", "Sleep",
"ByVal dwMilliseconds As Long")
'
Const sMe = "[clsWIN:InvokeMenuItem], "
Dim idMenuItem
Const WM_COMMAND = &H111
' first(!), check that the handle we are using is still valid...
BugAssert (IsValid(c_hWnd)), sMe & c_sMsgInvalidHandle
c_hMainMenu = GetMenu(c_hWnd) ' get the "main" menu...
BugAssert (c_hMainMenu <> 0), sMe & "couldn't find Main Menu handle "
' dbPrint sMe & " ..found main menu, handle is: " & CStr(c_hMainMenu)
' --------------------------------------------
' special case for pkzip, where there are an indefinite number of
file menu items,
' with the last one being "exit". In order to deal with the
indefinite number,
' use a "special constant" (last item) to indicate the last item...
' --------------------------------------------
if (nMainItemPos = c_LastMenuItem) then
dbPrint sMe & "setting last menu item = menuItemCount.. "
End If
' if all went well, now get at menu's requested item id...
idMenuItem = GetMenuItemID(c_hMainMenu, nMainItemPos)
' and, invoke/execute that menu item...
Call PostMessage(c_hWnd, WM_COMMAND, idMenuItem, 0)
Call Sleep(c_tLoadDelay)
' dbPrint sMe & "invoked Menu Selection: " & CStr(nMainItemPos)
Set GetMenu = nothing
Set GetMenuItemID = nothing
Set PostMessage = nothing
Set Sleep = nothing
End Sub ' invoke menu item
--- </snip> ---
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)