Results 1 to 23 of 23

Thread: Right click menu in a combo box [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Resolved Right click menu in a combo box [RESOLVED]

    I have some code, that I found and it works fine with a text box but i cant figure out how to make it work with a combo box since a combo box does not have a mousedown function.

    Anythoughts? or have a better way of doing it?

    PS. I tried subclassing the combo box to add the mousedown function with some code I found but that didnt seem to help.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const GWL_WNDPROC = (-4)
    4. Private Const WM_RBUTTONUP = &H205
    5.  
    6.  
    7.  
    8. Private Declare Function GetWindowLong Lib "user32" Alias _
    9. "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    10.  
    11. Private Declare Function SetWindowLong Lib "user32" Alias _
    12. "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As _
    13. Long, ByVal dwNewLong As Long) As Long
    14.  
    15. Private Declare Function CallWindowProc Lib "user32" Alias _
    16. "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd _
    17. As Long, ByVal Msg As Long, ByVal wParam As Long, _
    18. ByVal lParam As Long) As Long
    19.  
    20. Private m_lWndProc As Long
    21. Private OriginalWndProc As Long
    22.  
    23. Public Sub WindowHook(hWnd As Long)
    24.     m_lWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf MessageCenter)
    25.     OriginalWndProc = hWnd
    26. End Sub
    27.  
    28.  
    29.  
    30. Public Sub WindowFree(hWnd As Long)
    31.  
    32.     SetWindowLong hWnd, GWL_WNDPROC, m_lWndProc
    33. End Sub
    34.  
    35. Private Function MessageCenter(ByVal hWnd As Long, ByVal Msg As Long, _
    36. ByVal wParam As Long, ByVal lParam As Long) As Long
    37.  
    38. 'Only suppress context menu for hooked control
    39.  
    40.         If Msg = WM_RBUTTONUP Then
    41.          If hWnd = OriginalWndProc Then
    42.             Form1.PopupMenu Form1.mnuCustom
    43.             MessageCenter = 0
    44.            Exit Function
    45.         End If
    46.     End If
    47.    
    48.            
    49. 'Pass it on to the normal window processor.
    50. MessageCenter = CallWindowProc(m_lWndProc, hWnd, Msg, wParam, lParam)
    51. End Function
    Last edited by DKasler; Sep 6th, 2005 at 04:46 PM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

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

    Re: Right click menu in a combo box

    This works for a combo

    VB Code:
    1. 'Module code
    2. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    3.                                        (ByVal lpPrevWndFunc As Long, _
    4.                                                  ByVal hWnd As Long, _
    5.                                                   ByVal Msg As Long, _
    6.                                                ByVal wParam As Long, _
    7.                                          ByVal lParam As Long) As Long
    8.  
    9. Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    10.  
    11. Declare Function SetWindowLong Lib "user32" _
    12.                      Alias "SetWindowLongA" _
    13.                        (ByVal hWnd As Long, _
    14.                       ByVal nIndex As Long, _
    15.              ByVal dwNewLong As Long) As Long
    16.  
    17. Public Const GWL_WNDPROC = -4
    18. Public Const WM_CONTEXTMENU As Long = &H7B
    19.  
    20. Public lpPrevWndProc As Long
    21. Private lnghWnd As Long
    22.  
    23. Public Sub Hook(hWnd As Long)
    24.     Dim h As Long
    25.    
    26.     h = FindWindowEx(hWnd, 0&, "Edit", vbNullString)
    27.    
    28.     lnghWnd = h
    29.     lpPrevWndProc = SetWindowLong(lnghWnd, GWL_WNDPROC, AddressOf WindowProc)
    30. End Sub
    31.  
    32. Public Sub UnHook()
    33.     Dim lngReturnValue As Long
    34.     lngReturnValue = SetWindowLong(lnghWnd, GWL_WNDPROC, lpPrevWndProc)
    35. End Sub
    36.  
    37. Function WindowProc(ByVal hw As Long, _
    38.                     ByVal uMsg As Long, _
    39.                     ByVal wParam As Long, _
    40.                     ByVal lParam As Long) As Long
    41.  
    42.     Select Case uMsg
    43.         Case WM_CONTEXTMENU
    44.             Exit Function
    45.         Case WM_DESTROY
    46.             UnHook
    47.     End Select
    48.    
    49.     WindowProc = CallWindowProc(lpPrevWndProc, lnghWnd, uMsg, wParam, lParam)
    50. End Function

    In a form :

    VB Code:
    1. 'form code
    2.  
    3. Option Explicit
    4.  
    5. Private Sub Form_Load()
    6.     Hook Combo1.hWnd
    7. End Sub
    8.  
    9. Private Sub Form_Unload(Cancel As Integer)
    10.     UnHook
    11. End Sub


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

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

    Re: Right click menu in a combo box

    The reason seems to be that you need to subclass the textbox part of the combobox to catch the message, not the combobox itself. That's where the FindWindowEx API comes in handy! You get the handle of the textbox part of the combobox ("Edit" is the classname for it), and then subclass that instead of the combobox itself


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

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Right click menu in a combo box

    This will work for the dropdown portion of the cbo and not just the primary textbox?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: Right click menu in a combo box

    Quote Originally Posted by RobDog888
    This will work for the dropdown portion of the cbo and not just the primary textbox?
    My code?


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

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Right click menu in a combo box

    Yes, post #2. It looks like it doesnt apply to the dropdown portion.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: Right click menu in a combo box

    Yep, it doesn't... Don't know the full structure of the combobox but if you want to catch messages for the list part of the combobox you should do a FindWindowEx with whatever the classname is for the listbox? Just guessing though...


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

  8. #8

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Right click menu in a combo box

    Ok... 2 questions...

    1) What needs to be in the vbmenu editor for this to work?

    2) Im getting (;t put in the text box when the form loads, and it didnt do that till I hooked the contol.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Right click menu in a combo box

    Thats what I thought but perhaps the code can be modded easily to include that functionality. I dont think the dropdown listbox is really parented by the cbo since it may be a child of the editbox's control.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: Right click menu in a combo box

    According to my beloved Spy++ Rob the only child in the combobox is the Edit one... So to catch messages for the listbox part of it I'm assuming you get them when subclassing the combobox "generally", not specifically the textbox part


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

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

    Re: Right click menu in a combo box

    Quote Originally Posted by DKasler
    Ok... 2 questions...

    1) What needs to be in the vbmenu editor for this to work?

    2) Im getting (;t put in the text box when the form loads, and it didnt do that till I hooked the contol.
    1) vbmenu editor?

    2) that doesn't make any sense Did you just copy paste my code? Do a search in your project for those characters maybe you're adding it somewhere?


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

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Right click menu in a combo box

    Thats what I thought. The dropdown is independant from the cbo. Its not a child window then but a FindWindow API candidate.

    Ps, too lazy to check it out myself.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: Right click menu in a combo box

    Subclassing the combobox "generally" as we defined earlier, you only get the 308 message, whatever that is You get it when the selected item changes... You also get a few messages when the list is dropped down or brought up


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

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

    Re: Right click menu in a combo box

    It's the Const WM_CTLCOLORLISTBOX As Long = &H134 that's sent when the selection changes...


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

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Right click menu in a combo box

    Yes, but shouldnt the message for the click be the WM_COMMAND? or is it a different one for the right click?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Right click menu in a combo box

    Forgive me for being a bit confused...

    but from the code you pasted how can I get a right click menu on the combo box?
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

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

    Re: Right click menu in a combo box

    Quote Originally Posted by RobDog888
    Yes, but shouldnt the message for the click be the WM_COMMAND? or is it a different one for the right click?
    I was only moving the mouse around, not clicking I assume that that message would be sent, can't be 100% though without testing


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

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

    Re: Right click menu in a combo box

    Quote Originally Posted by DKasler
    Forgive me for being a bit confused...

    but from the code you pasted how can I get a right click menu on the combo box?
    In the WindowProc function, where you catch the WM_CONTEXTMENU message and cancel it out, put your popupmenu code.


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

  19. #19

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Right click menu in a combo box

    AWESOME! Thanks so much.
    Last edited by DKasler; Sep 6th, 2005 at 04:45 PM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

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

    Re: Right click menu in a combo box

    Can you post your code?

    Edit: I see your question if gone

    You're welcome!


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

  21. #21

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Right click menu in a combo box

    Quote Originally Posted by manavo11
    Can you post your code?
    I figured it out, I accidently removed the "Exit Function" when I put my menu code in.

    Thanks alot for the help.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  22. #22
    Addicted Member
    Join Date
    Nov 2005
    Posts
    153

    Re: Right click menu in a combo box

    Quote Originally Posted by DKasler
    I figured it out, I accidently removed the "Exit Function" when I put my menu code in.

    Thanks alot for the help.
    Can You Show Me What did you Do with "WindowProc" Function ?

    Because i want to Pop up my Menu
    This is The Prophet MOHAMMAD!
    =======================
    My SuperMen:
    RhinoBull, gigemboy, jmcilhinney, |2eM!x, Edneeis and Hack

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

    Re: Right click menu in a combo box [RESOLVED]

    In the code in post #2, you need to add code to popup your own menu :

    VB Code:
    1. Function WindowProc(ByVal hw As Long, _
    2.                     ByVal uMsg As Long, _
    3.                     ByVal wParam As Long, _
    4.                     ByVal lParam As Long) As Long
    5.  
    6.     Select Case uMsg
    7.         Case WM_CONTEXTMENU
    8.             [hl=yellow][b]PopupMenu YourMenu[/b][/hl]
    9.             Exit Function
    10.         Case WM_DESTROY
    11.             UnHook
    12.     End Select
    13.    
    14.     WindowProc = CallWindowProc(lpPrevWndProc, lnghWnd, uMsg, wParam, lParam)
    15. End Function


    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