Results 1 to 7 of 7

Thread: catch mouse click on a form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    didn't decide yet
    Posts
    566

    catch mouse click on a form

    how can i tell if user click on the form without using any of the predefined form events.

    thnks
    Come and get our ISDN CallerID http://www.3wm.biz

  2. #2
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    It might help if you explain what you are trying to do - I'm not sure why you want to avoid using the Form events...

    But anyway, to detect these clicks before your form does, you'd have to subclass the form's window, and look for the mousedown, mouseup messages - then you could elect to pass them on or not....

    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    didn't decide yet
    Posts
    566
    i m building an activex and i do not want user to put code at form's mousedown event i want my active to detect mouseclick b4 form does can u give an example on how can i subclass form to get the info i want?
    Come and get our ISDN CallerID http://www.3wm.biz

  4. #4
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    Tina

    OK, I have an example for you - I've just got to trim it a bit - watch this space ....


    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  5. #5
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    OK, here's a little module:

    Code:
    Option Explicit
    
    Const WM_LBUTTONDOWN = &H201&  ' MouseDown - Left button
    Const WM_MBUTTONDOWN = &H207&  '             Middle
    Const WM_RBUTTONDOWN = &H204&  '             Right
    
    Const GWL_WNDPROC = -4
    
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim OldWindowProc As Long
    
    Public Function SubClass(hWnd As Long)
       OldWindowProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
       End Function
    
    Public Function UnSubClass(hWnd As Long) As Boolean
       Call SetWindowLong(hWnd, GWL_WNDPROC, OldWindowProc)
       End Function
    
    Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
       Select Case uMsg
          Case WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN
             Exit Function        ' block these events
          End Select
       '
       ' pass along all others
       '
       WndProc = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
       End Function
    And here's a little test form:

    Code:
    Private Sub Form_Load()
       Show
       mSubclassOn.Enabled = True
       mSubclassOff.Enabled = False
       End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
       UnSubClass Me.hWnd
       End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
       MsgBox "Form MouseDown Event", vbInformation
       End Sub
    
    Private Sub mSubclassOn_Click()
       SubClass Me.hWnd
       mSubclassOff.Enabled = True
       mSubclassOn.Enabled = False
       End Sub
    
    Private Sub mSubclassOff_Click()
       UnSubClass Me.hWnd
       mSubclassOn.Enabled = True
       mSubclassOff.Enabled = False
       End Sub
    
    Private Sub Command1_Click()
       MsgBox "Button", vbInformation
       End Sub
    
    Private Sub Label1_Click()
       MsgBox "Label", vbInformation
       End Sub
    
    Private Sub Picture1_Click()
       MsgBox "PictureBox", vbInformation
       End Sub
    Notes to follow .....
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  6. #6
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    Open a new project and paint a Command Button, a PictureBox, a Label on it.

    Create 2 menu options mSubclassOn and mSubClassOff

    Load the module

    When you run it, if you click mSubClassOn, you'll find that the Form_MouseDown event is disabled, but you can still click on the Button and the PictureBox.

    You CAN'T click on the label - it has no window of its own, it's managed by the Form.

    PS - don't "break" into the IDE while SubClassing is ON, or you'll crash rather hard!


    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    didn't decide yet
    Posts
    566
    thnks a lot MathImagics i ll try your code and tell you how it went
    Come and get our ISDN CallerID http://www.3wm.biz

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