PDA

Click to See Complete Forum and Search --> : Disable mouse button


samui
Sep 22nd, 2000, 02:32 AM
How can I disable right button or left button of my mouse by VB codes or Win API ?

Mad Compie
Sep 22nd, 2000, 01:49 PM
This is code to disable the Mouse completely...
Otherwise, you can write your own code in the MouseUp or MouseDown events to test the Button parameter (1=left, 2=right).

'
'This form needs two Command Buttons...
'
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Sub Form_Load()
Command1.Caption = "Limit Cursor Movement"
Command2.Caption = "Release Limit"
End Sub
Private Sub Command1_Click()
'Limits the Cursor movement to within the form.
Dim Client As RECT

ShowCursor 0&
SetRect Client, 0, 0, 0, 0
ClipCursor Client
End Sub
Private Sub Command2_Click()
'Releases the cursor limits
ClipCursor ByVal 0&
ShowCursor 1&
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Releases the cursor limits
Command2_Click
End
End Sub

Sep 22nd, 2000, 03:21 PM
Remember: The MouseDown and MouseUp events occur after the buttons are clicked. If you want to truley disable them then you should subclass the WM_RBUTTONDOWN message.

Add the following to a Module.

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
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
Private Const GWL_WNDPROC = (-4)
Private Const WM_RBUTTONDOWN = &H204
Private Const MK_RBUTTON = &H2

Public WndProcOld As Long

Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_RBUTTONDOWN And wParam = MK_RBUTTON Then Exit Function
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function

Sub SubClassWnd(hwnd As Long)
WndProcOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub

Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld
WndProcOld = 0
End Sub


Add the following to a Form.

Private Sub Form_Load()
SubClassWnd hwnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hwnd
End Sub