Solved

Right Click and Default Menu

Posted on 1998-08-10
11
144 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

22 Experts available now in Live!

Get 1:1 Help Now