65.9K
CodeProject is changing. Read more.
Home

How to detect a left mouse click on a Winform titlebar

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.21/5 (14 votes)

Jun 2, 2004

viewsIcon

52750

This simple code will trap a left mouse click on a winform titlebar and sets the opacity of the form to 50%, when the left mouse button is released the forms opaccity is set back to 100%

       Private Const WM_NCLBUTTONDOWN As Long = &HA1
       Private Const WM_NCLBUTTONUP As Long = &HA0

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
  ' -- You might want to trap for user clicking, min, max or close and ignore
  ' -- Trap left mouse click down on titlebar
  If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
    ' -- Set the forms opacity
    If Me.Opacity <> 0.5 Then Me.Opacity = 0.5
    ' -- Trap left mouse click up on titlebar
  ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then
    If Me.Opacity <> 1.0 Then Me.Opacity = 1.0
  End If

   ' -- Business as usual
   MyBase.DefWndProc(m)
End Sub