Solved

Right Click and Default Menu

Posted on 1998-08-10
11
145 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 82
VBA filters 2 51
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 47
Prevent user closing word document opened with VB6 6 67
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

896 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

15 Experts available now in Live!

Get 1:1 Help Now