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:
Option Explicit
Private Const GWL_WNDPROC = (-4)
Private Const WM_RBUTTONUP = &H205
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
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 m_lWndProc As Long
Private OriginalWndProc As Long
Public Sub WindowHook(hWnd As Long)
m_lWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf MessageCenter)
OriginalWndProc = hWnd
End Sub
Public Sub WindowFree(hWnd As Long)
SetWindowLong hWnd, GWL_WNDPROC, m_lWndProc
End Sub
Private Function MessageCenter(ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Only suppress context menu for hooked control
If Msg = WM_RBUTTONUP Then
If hWnd = OriginalWndProc Then
Form1.PopupMenu Form1.mnuCustom
MessageCenter = 0
Exit Function
End If
End If
'Pass it on to the normal window processor.
MessageCenter = CallWindowProc(m_lWndProc, hWnd, Msg, wParam, lParam)
End Function
Re: Right click menu in a combo box
This works for a combo :afrog:
VB Code:
'Module code
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
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
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_CONTEXTMENU As Long = &H7B
Public lpPrevWndProc As Long
Private lnghWnd As Long
Public Sub Hook(hWnd As Long)
Dim h As Long
h = FindWindowEx(hWnd, 0&, "Edit", vbNullString)
lnghWnd = h
lpPrevWndProc = SetWindowLong(lnghWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(lnghWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub
Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Select Case uMsg
Case WM_CONTEXTMENU
Exit Function
Case WM_DESTROY
UnHook
End Select
WindowProc = CallWindowProc(lpPrevWndProc, lnghWnd, uMsg, wParam, lParam)
End Function
In a form :
VB Code:
'form code
Option Explicit
Private Sub Form_Load()
Hook Combo1.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook
End Sub
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 :)
Re: Right click menu in a combo box
This will work for the dropdown portion of the cbo and not just the primary textbox?
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?
Re: Right click menu in a combo box
Yes, post #2. It looks like it doesnt apply to the dropdown portion.
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? :ehh: Just guessing though...
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.
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.
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 ;)
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? :ehh:
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. :D
Ps, too lazy to check it out myself. :lol:
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 :D You get it when the selected item changes... You also get a few messages when the list is dropped down or brought up :)
Re: Right click menu in a combo box
It's the Const WM_CTLCOLORLISTBOX As Long = &H134 that's sent when the selection changes...
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?
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?
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 :D I assume that that message would be sent, can't be 100% though without testing :thumb:
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.
Re: Right click menu in a combo box
Re: Right click menu in a combo box
Can you post your code? :)
Edit: I see your question if gone :)
You're welcome!
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.
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
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:
Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Select Case uMsg
Case WM_CONTEXTMENU
[hl=yellow][b]PopupMenu YourMenu[/b][/hl]
Exit Function
Case WM_DESTROY
UnHook
End Select
WindowProc = CallWindowProc(lpPrevWndProc, lnghWnd, uMsg, wParam, lParam)
End Function