Results 1 to 6 of 6

Thread: How do i trap a right-mouse click !!!!!!!!!1

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 1999
    Posts
    52

    Post

    hi,

    how do i trap a right-mouse click on a form or button.

    thanxxx..in adv
    kandan

  2. #2
    Addicted Member
    Join Date
    Sep 1999
    Posts
    229

    Post


    Hi Kandan,

    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    MsgBox (Button)
    End Sub

    Left is 1
    Right is 2

    Does it help ?

    Regards

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 1999
    Posts
    52

    Post

    hi keiko,

    thanxx for ur help it works fine..but when i click in a command button r any other control it does'nt work.

    any help??????????
    kandan

  4. #4
    Addicted Member
    Join Date
    Sep 1999
    Posts
    229

    Post


    Hi Kandan,

    Yes It does

    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    MsgBox (Button)
    End Sub

    You can try others.

    Does it help ?

    Regards

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 1999
    Posts
    52

    Post

    hi keiko,

    i wanna generalized 1..in ur case i hav to write this event for each and every control that i put in the form which is ridiculus..so would u suggest me ?????????

    thanxxxx
    kandan
    E-mail [email protected]
    ICQ 52774533

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    There are a Couple of methods you can use:

    1. Use the GetAsyncKeyState API and a Timer Control..
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    
    Private Sub Form_Load()
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        'Make sure the focus is on this Form
        If GetForegroundWindow <> hWnd Then Exit Sub
        If GetAsyncKeyState(vbRightButton) Then
            Caption = "Right Click"
            'Do Your Form Global Event Here
        Else
            Caption = "Form1"
        End If
    End Sub
    2. Create a Windows Hook on the Mouse..(Cleaner Method)..

    In a Form..
    Code:
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Const WH_MOUSE = 7
    Private lHook As Long
    
    Private Sub Form_Load()
        lHook = SetWindowsHookEx(WH_MOUSE, AddressOf GlobalRightMouseProc, App.hInstance, App.ThreadID)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call UnhookWindowsHookEx(lHook)
    End Sub
    In a Module..
    Code:
    Private Const WM_RBUTTONDOWN = &H204
    
    Public Function GlobalRightMouseProc(ByVal iCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If wParam = WM_RBUTTONDOWN And iCode = 0 Then
            'Thread Global Right Click Event Goes In Here
        End If
    End Function

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width