Condividi tramite


Using ToolStrip to Create a Custom Title Bar

Our current documentation on creating nonrectangular forms could stand some enhancement. The most glaring piece of missing info is how to add a custom title bar. The document we do have gives code for how to move the form if the user clicks anywhere in the form area - but how often do you need this? In most cases, you want to slap your own custom title bar on the app, and move the app around when the user clicks on this title bar and drags it about.

Picture 1: Meditate Awake! application

For the sample app I've been developing, I decided to replace the standard title bar with a ToolStrip that acts like a title bar. (Can't you tell I'm no graphic designer?) It was surprisingly easy to enable the ToolStrip to act like a title bar. The secret ingredient was setting ToolStrip.Capture to True when the mouse button is clicked; this is necessary to prevent the cursor from moving faster than the form, which results in the cursor flying away from the ToolStrip before you've released the left mouse button.

    Private _MouseOffset As Point
Private isMouseDown As Boolean = False

    Private Sub TitleStrip_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TitleStrip.MouseDown
If e.Button = System.Windows.Forms.MouseButtons.Left Then
_MouseOffset = New Point(e.X, e.Y)
_IsMouseDown = True
TitleStrip.Capture = True
End If
End Sub

Private Sub TitleStrip_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TitleStrip.MouseMove
If _IsMouseDown Then
Dim _CurrentPos As Point = Control.MousePosition
_CurrentPos.X = _CurrentPos.X - _MouseOffset.X
_CurrentPos.Y = _CurrentPos.Y - _MouseOffset.Y
Me.Location = _CurrentPos
End If
End Sub

    Private Sub TitleStrip_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TitleStrip.MouseUp
If e.Button = System.Windows.Forms.MouseButtons.Left Then
_IsMouseDown = False
TitleStrip.Capture = False
End If
End Sub

Comments

  • Anonymous
    August 15, 2006
    使用 Windows Forms 2.0 可以更加轻松地创建高级的动态和导航布局。

  • Anonymous
    April 03, 2007
    摘要:学习如何使用WindowsForms2.0中的新控件创建智能化和可扩展的应用程序布局。 从Microsoft下载中心下载C#和VisualBasic代码示例(903K...

  • Anonymous
    May 31, 2009
    PingBack from http://woodtvstand.info/story.php?id=10217