Mouse events for Combo Control

Is there a way to add mouse events to the combo control? I want to attach a popupmenu to the combo control, but can't figure out how to get mouse events.
I tried creating an activex control with the mouse events enabled, but they don't fire if you click on the combo box.
I've considered using the forms 2.0 combo box, but from what I've read from the microsoft site, the forms 2.0 controls are not allowed to be distributed.
LVL 1
chaos_59Asked:
Who is Participating?
 
wsh2Connect With a Mentor Commented:
In a Standard.Exe project.. add a ComboBox (Combo1) to Form1.. and create a top level menu item with the name mnuTest. To mnuTest add at at least one menu subitem. In Form1 paste the following...

<----- Form1 Begin ------->

Option Explicit

Private Sub Form_Load()
    ' Subclass Combo1
    Call Module1.xComboHook(Combo1.hwnd)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    ' Release Subclassing
    Call Module1.xComboUnhook(Combo1.hwnd)
End Sub

<----- Form1 End ----->

Next, from the Project Menu.. select AddModule (Module1) and in the code box paste the following..

<----- Module1 Begin ----->

Option Explicit

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal ilngwParam As Long, ByVal lParam As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_MOUSEACTIVATE = &H21
Public Const WM_CONTEXTMENU = &H7B
Public Const WM_RBUTTONDOWN = &H204

Private mwndComboHandle As Long
Private mintMousePress As Integer

Public Function xComboCallback _
    (ByVal ihWnd As Long, _
    ByVal ilngMsg As Long, _
    ByVal ilngwParam As Long, _
    ByVal ilnglParam As Long) _
    As Long
   
    Select Case ilngMsg
       
        Case WM_MOUSEACTIVATE
           
            On Error Resume Next
            Call CopyMemory(mintMousePress, ByVal VarPtr(ilnglParam) + 2, 2)
            If mintMousePress = WM_RBUTTONDOWN Then
                Form1.PopupMenu Form1.mnuTest
                If (Err) _
                Then
                    Call xComboUnhook(ihWnd)
                    Exit Function
                End If
                SendKeys "{ESC}"
            End If
       
        Case WM_CONTEXTMENU
           
            On Error Resume Next
            Form1.PopupMenu Form1.mnuTest
            If (Err) _
            Then
                Call xComboUnhook(ihWnd)
                Exit Function
            End If
            SendKeys "{ESC}"
   
    End Select
   
    xComboCallback = CallWindowProc(mwndComboHandle, ihWnd, ilngMsg, ilngwParam, ilnglParam)

End Function

Public Function xComboHook _
(ByVal ihWnd As Long) _
As Long
    mwndComboHandle = SetWindowLong(ihWnd, GWL_WNDPROC, AddressOf Module1.xComboCallback)
    xComboHook = mwndComboHandle
End Function

Public Function xComboUnhook _
(ByVal ihWnd As Long) _
As Long
    mwndComboHandle = SetWindowLong(ihWnd, GWL_WNDPROC, mwndComboHandle)
    xComboUnhook = mwndComboHandle
End Function

<----- Module1 End ----->

Hit the Start Button.. Right Mouse click on Combo1.. and VOILE.. Popup Menu mnuTest appears.. <smile>.


0
 
mcriderCommented:
There is no problem using the Microsoft Forms 2.0 Object Library (FM20.DLL) in your product... If Microsoft Office is installed on the target system, FM20.DLL is already installed.  If not, you can provide the "Microsoft ActiveX Control Pad" which is *FREE* and installs FM20.DLL.

Read this article:
INFO: Usage and Redistribution of FM20.DLL
http://support.microsoft.com/support/kb/articles/Q224/3/05.ASP 
 

Cheers!®©

0
 
ArkCommented:
Hi
You can use subclassing
'---module code
Public Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Public Type COMBOBOXINFO
   cbSize As Long
   rcItem As RECT
   rcButton As RECT
   stateButton  As Long
   hwndCombo  As Long
   hwndEdit  As Long
   hwndList As Long
End Type

Declare Function GetComboBoxInfo Lib "user32.dll" (ByVal hwndCombo As Long, CBInfo As COMBOBOXINFO) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
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
Public Const GWL_WNDPROC = (-4)
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205

Public OldProc As Long
Public hcboList As Long

Public Function GetComboListHandle(ctl As ComboBox) As Long
   Dim CBI As COMBOBOXINFO
   CBI.cbSize = Len(CBI)
   Call GetComboBoxInfo(ctl.hWnd, CBI)
   GetComboListHandle = CBI.hwndList
End Function

Public Function WndProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 If wMsg = WM_RBUTTONDOWN Then
    MsgBox "You Right_Click on " & Form1.Combo1.List(Form1.Combo1.ListIndex) & " Item"
 End If
 WndProc = CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam)
End Function
'---Form1 code (form1 has Combo1 control)
Private Sub Form_Load()
  For i = 1 To 10
      Combo1.AddItem CStr(i)
  Next i
  hcboList = GetComboListHandle(Combo1)
  OldProc = GetWindowLong(hcboList, GWL_WNDPROC)
  SetWindowLong hcboList, GWL_WNDPROC, AddressOf WndProc
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    SetWindowLong hcboList, GWL_WNDPROC, OldProc
End Sub
'IMPORTANT - this code use subclassing, so don't try to stop program from IDE, use [x] button on form instead

Cheers
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
wsh2Commented:
Ooops.. don't want be rude.. LOL.. Changing answer to Comment.. <smile>
0
 
ArkCommented:
wsh2: Sorry,I cannot check your code - it hang VB IDE. Only Ctrl+Alt+Del help.
chaos_59: Do you want to fire Right_Click event from Edit Part of ComboBox or from dropdown list of combobox? (they have different handles, so you need different code)
0
 
wsh2Commented:
Ark.. I just reran it here.. and it works just fine.. <smile>. You may want to recheck your steps.. Recapping..

1.  Create a Standard.Exe Project

2.  In Form1..
    a.  Add a Combobox
    b.  Using the Menu Editor Create a top level menu item with the name mnuTest and uncheck its visible box.
    c.  Add at least one submenu item (an indented one) under mnuTest.. more if you like.
    d.  Copy the Form1 code from above into the Form1 code window.

3.  Add a basic Module (Project.. Add Module) and copy the Module1 code from above into the code window.

Good Luck... <smile>

0
 
wsh2Commented:
Uh Ark.. it just occurred to me.. did you add and or modify anything in the xComboCallback procedure? As this is a subclass.. you want to be very careful adding anything that may cause the IDE to stop.. (ie breakpoints and other debug schtuff).. the results of doing so, can often lead to an Alt-Ctrl-Del.
0
 
ArkCommented:
wsh2: Everything is OK - I forgot to add Menu. Sorry. I know about subclassing problems - my code do it almost the same way as yours, but I subclassed dropdown portion of ComboBox. I don't know, what part of combobox chaos_59 want to use.

Cheers :)
0
 
chaos_59Author Commented:
I haven't had time to test this. I tried it once and forgot your note about not stopping with the ide. It blew when I did, and I haven't tried it again. but I'm sure it will work. looks like what I need. I wish there was an easier way. Why would Microsoft leave the mouse events out of a control???
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.