?
Solved

Mouse events for Combo Control

Posted on 2000-03-03
9
Medium Priority
?
199 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:chaos_59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2582549
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
 
LVL 28

Expert Comment

by:Ark
ID: 2582608
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
 
LVL 14

Accepted Solution

by:
wsh2 earned 400 total points
ID: 2582689
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:wsh2
ID: 2582698
Ooops.. don't want be rude.. LOL.. Changing answer to Comment.. <smile>
0
 
LVL 28

Expert Comment

by:Ark
ID: 2582725
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2582748
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2582751
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
 
LVL 28

Expert Comment

by:Ark
ID: 2582760
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
 
LVL 1

Author Comment

by:chaos_59
ID: 2584525
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question