Averiguar el Menu desplegado.

Iniciado por LeandroA, 29 Octubre 2008, 21:31 PM

0 Miembros y 1 Visitante están viendo este tema.

Dessa

Laendro, fijate si podes adaptar este code:



Option Explicit

Private Declare Function GetForegroundWindow Lib "user32" () As Long

Private Declare Function FindWindow _
        Lib "user32" _
        Alias "FindWindowA" _
       (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
       

Private Declare Function GetWindowRect Lib "user32" _
(ByVal Hwnd As Long, lpRect As RECT) As Long

Private Type RECT
    left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function SetCursorPos Lib "user32" _
(ByVal X As Long, ByVal Y As Long) As Long

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)

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Dim Hndl As String _


Private Sub Form_Load()

Shell "notepad", vbNormalFocus
Timer1.Interval = 50
End Sub


Private Sub Timer1_Timer()

Dim TR As RECT
Dim left_notepad As Integer
Dim left_contexo As Integer

Hndl = FindWindow("notepad", vbNullString)

If Hndl <> 0 Then
 
  If Hndl = GetForegroundWindow() Then
   
    Call GetWindowRect(Hndl, TR)
    left_notepad = TR.left
   
    Hndl = FindWindow("#32768", vbNullString)
     
      If Hndl = 65562 Then
        Me.Caption = "menu de notepad cerrado"
      Else
        Call GetWindowRect(Hndl, TR)
        left_contexo = TR.left
        Me.Caption = "Left   " & left_contexo - left_notepad
      End If
 
  End If

Else

  Me.Caption = "notepad cerrado"

End If

End Sub




PD: si encontraste el mensaje directo pasame el code, saludos
Adrian Desanti

LeandroA

Hola si encontre la solucion,pero bueno me colgue y no lo postie, pero aca va. hay otras formas mas, pero esta es la que mejor se adaptaba a mi nesesidad.

Código (vb) [Seleccionar]
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetMenuItemInfo Lib "user32.dll" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, ByRef lpMenuItemInfo As MENUITEMINFO) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long

Private Type MENUITEMINFO
    cbSize As Long
    fMask As Long
    fType As Long
    fState As Long
    wID As Long
    hSubMenu As Long
    hbmpChecked As Long
    hbmpUnchecked As Long
    dwItemData As Long
    dwTypeData As String
    cch As Long
End Type

Private Const WM_MENUSELECT     As Long = &H11F
Private Const GWL_WNDPROC = (-4)

Private Const MF_ENABLED As Long = &H0&
Private Const MF_HILITE As Long = &H80&
Private Const MF_DISABLED As Long = &H3&
Private Const MIIM_STATE As Long = &H1

Dim hMenu As Long
Dim PrevProc As Long

Public Sub HookForm(hWnd As Long)
    hMenu = GetMenu(hWnd)
    PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHookForm(hWnd As Long)
    SetWindowLong hWnd, GWL_WNDPROC, PrevProc
End Sub

Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    If uMsg = WM_MENUSELECT Then
        Debug.Print GetIdMenuSelected()
    End If
End Function

Private Function GetIdMenuSelected() As Long

Dim MnuCount As Long
Dim i As Long
Dim ItemState As Long
MnuCount = GetMenuItemCount(hMenu)

For i = 0 To MnuCount - 1
    ItemState = GetMenuState(i)
    If ItemState = (MF_HILITE Or MF_ENABLED) Or ItemState = (MF_HILITE Or MF_DISABLED) Then
        GetIdMenuSelected = i
        Exit Function
    End If
Next

GetIdMenuSelected = -1

End Function


Private Function GetMenuState(ID As Long) As Long
    Dim MII As MENUITEMINFO
    MII.cbSize = Len(MII)
    MII.fMask = MIIM_STATE
    Call GetMenuItemInfo(hMenu, ID, True, MII)
    GetMenuState = MII.fState
End Function


Form
Código (vb) [Seleccionar]
Option Explicit

Private Sub Form_Load()
    HookForm Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
    UnHookForm Me.hWnd
End Sub



Saludos