Solved

Right Click and Default Menu

Posted on 1998-08-10
11
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

724 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