PDA

Click to See Complete Forum and Search --> : right click conflict...subclassing?


joblake
Oct 17th, 2001, 03:57 PM
Here's the scenario:

I've got a control (DataGrid) that I want to display my popup menu when I right click in a cell. So I have a "mouseup" routine that looks for a right click.

Problem is that the control, as part of its native functionality, also reacts to the right click event to display the standard edit popup menu.

My question is how to I pre-emp the native popup with my own?

subclassing...?

jim mcnamara
Oct 17th, 2001, 04:15 PM
You have to trap the message - stop it from going to the window, and then do your routine instead.

Classic use for sublcassing.

joblake
Oct 17th, 2001, 04:18 PM
OK.

Could you be real specific--because I'm new to VB and subclassing is just a concept right now.

Thanks!

jim mcnamara
Oct 18th, 2001, 06:03 AM
Subclass your datagrid to catch the WM_RBUTTONUP message.

in a .BAS module:



Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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

Const GWL_WNDPROC = (-4)
Const WM_RBUTTONUP = &H205

Global WndProcOld As Long

Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As _
Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If wMsg = WM_RBUTTONUP Then
' display your popup menu here
Exit Function ' since you only want your menu exit
' if you let the message thru, then the other menu
' will appear
End If

WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)

End Function

Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub

Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub



usage:

Call SubCLassWnd(Datagrid1.hWnd)

MAKE SURE TO CALL !!! ->

UnSubclassWnd(Datagrid1.hWnd)

before you exit or your program will crash on exit. That means
you have to do this in the Form_Unload event.