• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Right Click and Default Menu

I have an application that uses the right mouse click to put information into a bound text box. How can I get rid of the default menu that drops down with this action? I am using VB5.
0
lanyon
Asked:
lanyon
  • 5
  • 5
1 Solution
 
lanyonAuthor Commented:
Edited text of question
0
 
jsuperCommented:
The default context menu can be avoided by disabling the textbox on the mouse down event, then re-enabling it on the mouse up event for the form.  See the example below.   I know it sounds hokey, but it was the best method I have found.

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1.Enabled = True
End Sub

Private Sub Text1_DblClick()
    Text1.Enabled = False
End Sub

Private Sub Text1_Mousedown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
        Text1.Text = "Button pressed"
    End If
    Form1.SetFocus
    Text1.Enabled = False
End Sub


0
 
lanyonAuthor Commented:
You are right it is hokey:).
I seem to remember that msgblaster or some such could catch mousestrokes and eat them. Surely there is another to turn these menus off
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lanyonAuthor Commented:
Adjusted points to 200
0
 
jsuperCommented:
Allright, I found the answer you want.... this is MUCH cleaner.

Add the following code to a module in the project...

Option Explicit

Public 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 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_RBUTTONUP = &H205

Public defWndProc As Long

Public Sub Hook(hwnd As Long)
   If defWndProc = 0 Then
      defWndProc = SetWindowLong(hwnd, _
                                 GWL_WNDPROC, _
                                 AddressOf WindowProc)
   End If
End Sub

Public Sub UnHook(hwnd As Long)
    If defWndProc > 0 Then
      Call SetWindowLong(hwnd, GWL_WNDPROC, defWndProc)
      defWndProc = 0
   End If
End Sub

Public Function WindowProc(ByVal hwnd As Long, _
                           ByVal uMsg As Long, _
                           ByVal wParam As Long, _
                           ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_RBUTTONUP
           'this executes when the window is hooked
        Case Else
            WindowProc = CallWindowProc(defWndProc, _
                                        hwnd, _
                                        uMsg, _
                                        wParam, _
                                        lParam)
    End Select
End Function


To disable the context menu, use the following command in the form code...     Call Hook(txtHook.hwnd)

To enable the context menu, use the following code...
                 Call UnHook(txtHook.hwnd)

As an example, see below.  This code disables the popupmenu when the form is loaded, and reenables it when the form is unloaded or terminated...

Private Sub Form_Terminate()
   Call UnHook(txtHook.hwnd)
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Call UnHook(txtHook.hwnd)
End Sub

Private Sub Form_Load()
   Call Hook(txtHook.hwnd)
End Sub


Now, you can add your own popup menu or other code just as you normally would... Good Luck!

0
 
jsuperCommented:

Oh, by the way,  Once the textbox is "hooked",  the mouseup event will no longer respond to the right button.  You need to place the mouseup code in the WindowProc function (See the comment in the code).  When the box is "unhooked", the mouseup event begins to function normally.  If you need this part clarified, send me a coment back.
0
 
lanyonAuthor Commented:
Thanks I will try that and let you know
0
 
jsuperCommented:
Have you tried this out yet???
0
 
jarrahjackCommented:
Sorry for the delay. I have tried it but I am having problems getting it to work properly in my app. I have many controls on a form that need this facility and if I have more than one instance of your solution the program crashes and burns. It masks the behaviour of my right mouse click -  I don't want another menu!. It is also causing problems in other areas of locking the form. I would have thought that Microsoft would have a toggle that allowed this right click behaviour to be on or not.
0
 
lanyonAuthor Commented:
Sorry for the delay. I have tried it but I am having problems getting it to work properly in my app. I have many controls on a form that need this facility and if I have more than one instance of your solution the program crashes and burns. It masks the behaviour of my right mouse click -  I don't want another menu!. It is also causing problems in other areas of locking the form. I would have thought that Microsoft would have a toggle that allowed this right click behaviour to be on or not.

0
 
jsuperCommented:
It doesn't mask the use of the right mouse button, you need to place the code in the function WINDOWPROC where the comment "this executes when the window is executed" is. I'll see if I see what is going on with the other ones...
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now