Solved

How do I subclass in VB6 to capture user left mouse event on the title bar?

Posted on 2010-08-25
6
859 Views
Last Modified: 2012-08-13
I want to capture the event when the user left-clicks on the title bar of a form in VB6....mouse down, mouse click etc. Thanks.....
0
Comment
Question by:jazjef
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33521441
Begin with your normal subclassing routine but detect the non client area messages these are prefixed with (WM_NC) for example:

Mouse Input Notifications
http://msdn.microsoft.com/en-us/library/ff468877(v=VS.85).aspx 

WM_NCHITTEST
WM_NCLBUTTONDBLCLK
WM_NCLBUTTONDOWN
WM_NCLBUTTONUP
WM_NCMBUTTONDBLCLK
WM_NCMBUTTONDOWN
WM_NCMBUTTONUP
WM_NCMOUSEHOVER
WM_NCMOUSELEAVE
WM_NCMOUSEMOVE
WM_NCRBUTTONDBLCLK
WM_NCRBUTTONDOWN
WM_NCRBUTTONUP
WM_NCXBUTTONDBLCLK
WM_NCXBUTTONDOWN
WM_NCXBUTTONUP
In your WindowProc() method for example it would look something like this:
 

' Inside your WindowProc

Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If uMsg = WM_NCLBUTTONDBLCLK Then

 If wParam = HTCAPTION Then

  ' TODO:// Something when double clicked.

 End If

 WndProc = CallWindowProcA(func, hWnd, uMsg, wParam, lParam)

End If

End Function

Open in new window

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33521611
There is typo in previous example where the last line should be outside the If block statement. Below you can find a working example for detecting left double click, that will get you started then you can add the other events from the link above.

' Module1.bas



Option Explicit



Private Const WM_NCLBUTTONDBLCLK    As Long = &HA3&

Private Const HTCAPTION             As Long = &H2&

Private Const GWL_WNDPROC           As Long = (-4)



Private Declare Function SetWindowLongA Lib "User32.dll" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function CallWindowProcA Lib "User32.dll" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long



Private hWindow     As Long

Private func        As Long



Public Sub Attatch(ByVal hWnd As Long)

  If hWindow = 0 Then

    hWindow = hWnd

    func = SetWindowLongA(hWindow, GWL_WNDPROC, AddressOf WndProc)

  End If

End Sub



Public Sub Detatch()

  If hWindow <> 0 Then

    Call SetWindowLongA(hWindow, GWL_WNDPROC, func)

    hWindow = 0

  End If

End Sub

 

Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If uMsg = WM_NCLBUTTONDBLCLK Then

  If wParam = HTCAPTION Then

  ' TODO:// Something when double clicked.

    Debug.Print "Double Clicked: "; Time

  End If

End If

WndProc = CallWindowProcA(func, hWnd, uMsg, wParam, lParam)

End Function







' Form1

Option Explicit



Private Sub Form_Load()

  Attatch Me.hWnd

End Sub

 

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

  Detatch

End Sub

Open in new window

0
 
LVL 4

Author Comment

by:jazjef
ID: 33526571
Works great eql44. Thanks for teaching me how to do this. One small question.... is there a specific reason why it closes my development environment when I close the form? It shuts the form and my VB6 down when I click the X on the control box to exit.
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 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33526592
Well it shouldn't but if you have an error in the WindowProc(WndProc) it will cause all kinds of problems so I have to ask you did you add/modify the example with some extra code?
 
0
 
LVL 4

Author Comment

by:jazjef
ID: 33526932
:) Yes. I did. But it's working now. Thanks for showing me how this is done eql1044---really helpful and very informative. And thanks for adding in the mouse input notifications.....
0
 
LVL 4

Author Closing Comment

by:jazjef
ID: 33526950
Top-notch solution. Thanks eql1044.
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
Help in WHSCRIPT 9 47
Best way to parse out a json string in VB6? 10 116
Exit a vb6 apps when a calling it apps closes 15 43
Visual Studio 2005 text editor 10 35
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

861 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

24 Experts available now in Live!

Get 1:1 Help Now