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

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.....

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

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.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
' 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
Random Solutions  
 
programming4us programming4us