Results 1 to 2 of 2

Thread: WM_MenuSelect help!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    WM_MenuSelect help!

    I need to know how to use SendMessage or PostMessage to click a specific menu.

    I know a lot of window handles, and the SendMessage & PostMessage API. So if you need any extra details to help me, tell me.

    This is the menu I want to hit.
    Attached Images Attached Images  

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: WM_MenuSelect help!

    Here is one way :

    VB Code:
    1. 'Example by Daniel Kaufmann ([email protected])
    2.  
    3. 'Paste this code in a Form
    4. 'with a Menu named menu1 which has a menuitem named menu2
    5.  
    6. Private Type POINTAPI
    7.     x As Long
    8.     y As Long
    9. End Type
    10. Private Type RECT
    11.     Left As Long
    12.     Top As Long
    13.     Right As Long
    14.     Bottom As Long
    15. End Type
    16.  
    17. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    18. Private Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As Long
    19. Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    20.  
    21. Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
    22. Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    23. Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    24. Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
    25. Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
    26. Private Const MOUSEEVENTF_MIDDLEUP = &H40
    27. Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    28. Private Const MOUSEEVENTF_RIGHTUP = &H10
    29.  
    30. Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    31. Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
    32. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    33. Const SM_CXSCREEN = 0 'X Size of screen
    34. Const SM_CYSCREEN = 1 'Y Size of Screen
    35.  
    36. Private Sub Form_KeyPress(KeyAscii As Integer)
    37.     Dim mWnd As Long
    38.     mWnd = Me.hwnd
    39.    
    40.     Dim hMenu As Long, hSubMenu As Long
    41.  
    42.     hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
    43.     ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
    44.     hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
    45.     ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
    46.    
    47. End Sub
    48.  
    49.  
    50. Private Sub ScreenToAbsolute(lpPoint As POINTAPI)
    51. lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))
    52. lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
    53. End Sub
    54.  
    55. Private Sub Click(p As POINTAPI)
    56. 'p.X and p.Y in absolute coordinates
    57. 'Put the mouse on the point
    58.  
    59. mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, GetMessageExtraInfo()
    60. 'Mouse Down
    61. mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()
    62. 'Mouse Up
    63. mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()
    64. End Sub
    65.  
    66. Private Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos As Long)
    67. Dim ret As Long
    68. Dim r As RECT, p As POINTAPI
    69. ret = GetMenuItemRect(mWnd, hMenu, Pos, r)
    70. If ret = 0 Then Exit Sub
    71. p.x = (r.Left + r.Right) / 2
    72. p.y = (r.Top + r.Bottom) / 2
    73. ScreenToAbsolute p
    74. 'Click on p
    75. Click p
    76. End Sub
    77.  
    78. Private Sub Form_Load()
    79. Dim mWnd As Long, p As POINTAPI
    80. mWnd = Me.hwnd
    81. Dim hMenu As Long, hSubMenu As Long
    82. hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
    83. ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
    84. hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
    85. ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
    86. p.x = &HFFFF& / 2
    87. p.y = &HFFFF& / 2
    88. Click p
    89. Me.AutoRedraw = True
    90. Me.BackColor = vbWhite
    91. Print "Press any key"
    92. End Sub
    93.  
    94. Private Sub menu2_Click()
    95. MsgBox "Click"
    96. End Sub


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width