Solved

Right Click and Default Menu

Posted on 1998-08-10
11
146 Views
Last Modified: 2010-04-30
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
Comment
Question by:lanyon
  • 5
  • 5
11 Comments
 

Author Comment

by:lanyon
ID: 1468423
Edited text of question
0
 
LVL 1

Expert Comment

by:jsuper
ID: 1468424
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
 

Author Comment

by:lanyon
ID: 1468425
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:lanyon
ID: 1468426
Adjusted points to 200
0
 
LVL 1

Accepted Solution

by:
jsuper earned 200 total points
ID: 1468427
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
 
LVL 1

Expert Comment

by:jsuper
ID: 1468428

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
 

Author Comment

by:lanyon
ID: 1468429
Thanks I will try that and let you know
0
 
LVL 1

Expert Comment

by:jsuper
ID: 1468430
Have you tried this out yet???
0
 

Expert Comment

by:jarrahjack
ID: 1468431
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
 

Author Comment

by:lanyon
ID: 1468432
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
 
LVL 1

Expert Comment

by:jsuper
ID: 1468433
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now