-
Sep 29th, 2010, 12:33 AM
#1
Thread Starter
New Member
Title bar handling
I am creating a small controller window and wanted it to have a smaller than usual 'title bar'. It is 7 pixels tall at the moment.
The window should be movable by grabbing this title bar and I also wanted it to 'roll up' by clicking on the title bar. I already had the window moving working by using the API methods discussed here (a bunch of similar results turn up on Google). My problem was that I also need it to respond to clicks.
My solution was to put the API calls in the MouseMove event instead of MouseClick. A couple of state variables are used to determine whether the mouse button is down and whether it is being dragged. Then, I handled the pseudo click event in the MouseUp event.
I tried it without the MouseDown handler, and use e.Button logic instead of the _mouseDown variable, but the rollup/down function was less reliable for some reason.
I started out with a Panel but currently using a ToolStrip with close, minimize and 'pin' (keep on top) buttons aligned on the right side of the bar.
Code:
#Region "Title bar handling... dragging / clicking"
Private _mouseDown As Boolean
Private _mouseDragging As Boolean
Private Sub MainForm_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs) _
Handles pnlTitle.MouseDown
If e.Button <> Windows.Forms.MouseButtons.Left Then Return
_mouseDown = True
_mouseDragging = False
End Sub
Private Sub MainForm_MouseMove(ByVal sender As Object, _
ByVal e As MouseEventArgs) _
Handles pnlTitle.MouseMove
If _mouseDown Then
_mouseDragging = True
If Me.WindowState <> FormWindowState.Maximized Then MoveForm()
End If
End Sub
Private Sub MainForm_MouseUp(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) _
Handles pnlTitle.MouseUp
If e.Button <> Windows.Forms.MouseButtons.Left Then Return
If _mouseDown AndAlso Not _mouseDragging Then ClickTitleBar()
_mouseDown = False
_mouseDragging = False
End Sub
''' <summary>
''' Use Windows API SendMessage to make Windows think the form
''' the mouse is dragging on the form's title bar (the standard
''' Window's title bar).
''' </summary>
Private Sub MoveForm()
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
End Sub
''' <summary>
''' What happens when the title bar is clicked...
''' The form's 'rolled' a.k.a 'shade' state is toggled.
''' In the unrolled state, the form is its normal size.
''' In the rolled state, the form's height is only the
''' height of the title bar.
''' </summary>
Private Sub ClickTitleBar()
Static rolled As Boolean
Static unrolledSize As Size
If rolled Then
' restore the 'unrolled' size
Me.ClientSize = unrolledSize
Else
' remember the current size so it can be restored
unrolledSize = Me.ClientSize
Me.ClientSize = Me.pnlTitle.Size
End If
rolled = Not rolled
End Sub
#End Region
Last edited by Hack; Sep 29th, 2010 at 05:35 AM.
-
Sep 29th, 2010, 05:37 AM
#2
Re: Title bar handling
Split into its own thread from the linked thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|