Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Remove a Titlebar from a .Sizable Form

Status
Not open for further replies.

PixelGod

Programmer
Aug 24, 2015
16
0
0
US
As some may already know, I am relatively new to the VB.net scene and I am learning as I go. With any luck, you guys can help me (yet again)! Everyone has been extremely helpful and patient with me so far. Let's see if you can assist with me with my latest conundrum:

I have created a new form with FormBorderStyle.Sizable as its border option. However, I do not want the Titlebar present on the form because I have created my own custom titlebars. However, when I removed the form's caption and controlbox, there is still a "ghost" titlebar present. Albeit, much smaller than the original, for some reason I cannot get it to disappear. (Screenshot attached) Any ideas?

Code:
newForm.FormBorderStyle = FormBorderStyle.Sizable
newForm.ControlBox = False
newForm.Text = ""

In advance, thank you for you assistance!
 
 http://files.engineering.com/getfile.aspx?folder=76b3be34-6016-4910-b478-92858ae118c0&file=TitlebarWindow.png
The hosting form.........:

Code:
	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles CreateFormButton.Click

		Dim MyNewForm As New MyCodeDesignedForm

		'Use one of the following, solving the top border problem requires one of these
		'MyNewForm.FormBorderStyle = FormBorderStyle.FixedSingle
		'MyNewForm.FormBorderStyle = FormBorderStyle.FixedToolWindow

		'******* insert any other code here to design the rest of the form.

		MyNewForm.Show()

	End Sub

If you need to refer to any specific form outside of this sub create a form variable such as an Array, Dictionary or other list containing type as being of Form type. That way you will be able to locate and refer to the individual forms after creation.
 
The code to handle the resizing is a combination of code that I wrote some years ago for a slightly different purpose and other code I found on the Internet.

I've used a lot of spacing between blocks of code to make it easier to read and follow ........

Code:
Public Class ResizeableForm

	Public Event ResizingDone(ByRef ftr As Form)

	Private WithEvents FormToResize As Form

	Private _Edge As EdgeEnum = EdgeEnum.None
	Private _AllowEdges As EdgeEnum = EdgeEnum.All

	Private _OutlineWidth As Integer = 4
	Private _OutlineDrawn As Boolean = False
	Private _OutlineColor As Drawing.Color = Color.Yellow

	Private _MouseDown As Boolean = False
	Private _MousePointerLastLocation As Point = New Point(0, 0)

	Private _OriginalCursor As Cursor = Nothing

	Private g As Graphics = Nothing

	Public Enum EdgeEnum

		None = 0
		Right = 1
		Left = 2
		Top = 4
		Bottom = 8

		All = Left Or Right Or Top Or Bottom

	End Enum


	Public Property AllowEdges() As EdgeEnum
		Get
			Return _AllowEdges
		End Get
		Set(ByVal value As EdgeEnum)
			_AllowEdges = value
		End Set
	End Property

	Public Property OutlineColor() As Drawing.Color
		Get
			Return _OutlineColor
		End Get
		Set(ByVal value As Drawing.Color)
			_OutlineColor = value
		End Set
	End Property

	Public Property MouseDown As Boolean
		Get
			Return _MouseDown
		End Get
		Set(value As Boolean)
			_MouseDown = value
		End Set
	End Property

	Public Property OriginalCursor As Cursor
		Get
			Return _OriginalCursor
		End Get
		Set(value As Cursor)
			_OriginalCursor = value
		End Set
	End Property

	Public Sub New(ByVal FormToResize As Form)

		Me.FormToResize = FormToResize

	End Sub

	Private Sub FormToResize_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseDown

		If e.Button = MouseButtons.Left Then _MouseDown = True

	End Sub

	Private Sub FormToResize_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseUp

		_MouseDown = False

	End Sub

	Private Sub FormToResize_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseMove

		With FormToResize

			g = .CreateGraphics
			Dim b As New SolidBrush(OutlineColor)

			If _OutlineDrawn Then
				.Refresh()
				_OutlineDrawn = False
			End If

			Select Case _Edge
				Case EdgeEnum.Left
					g.FillRectangle(b, 0, 0, _OutlineWidth, .Height)
					_OutlineDrawn = True
				Case EdgeEnum.Right
					g.FillRectangle(b, .Width - _OutlineWidth, 0, .Width, .Height)
					_OutlineDrawn = True
				Case EdgeEnum.Top
					g.FillRectangle(b, 0, 0, .Width, _OutlineWidth)
					_OutlineDrawn = True
				Case EdgeEnum.Bottom
					g.FillRectangle(b, 0, .Height - _OutlineWidth, .Width, _OutlineWidth)
					_OutlineDrawn = True
			End Select

			If _MouseDown AndAlso _Edge <> EdgeEnum.None Then

				.SuspendLayout()
				Select Case _Edge
					Case EdgeEnum.Left
						.SetBounds(.Left + e.X, .Top, .Width - e.X, .Height)
					Case EdgeEnum.Right
						.SetBounds(.Left, .Top, .Width - (.Width - e.X), .Height)
					Case EdgeEnum.Top
						.SetBounds(.Left, .Top + e.Y, .Width, .Height - e.Y)
					Case EdgeEnum.Bottom
						.SetBounds(.Left, .Top, .Width, .Height - (.Height - e.Y))
				End Select

				RaiseEvent ResizingDone(FormToResize)
				.ResumeLayout()

			Else
				OriginalCursor = Cursor.Current
				Select Case True
					Case e.X <= _OutlineWidth
						.Cursor = Cursors.SizeWE
						_Edge = EdgeEnum.Left
					Case e.X > .Width - (_OutlineWidth + 1)
						.Cursor = Cursors.SizeWE
						_Edge = EdgeEnum.Right
					Case e.Y <= _OutlineWidth
						.Cursor = Cursors.SizeNS
						_Edge = EdgeEnum.Top
					Case e.Y > .Height - (_OutlineWidth + 1)
						.Cursor = Cursors.SizeNS
						_Edge = EdgeEnum.Bottom
					Case Else
						.Cursor = OrininalCursor
						_Edge = EdgeEnum.None
				End Select

			End If

		End With

	End Sub

	Private Sub FormToResize_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormToResize.MouseLeave

		FormToResize.Cursor = OriginalCursor
		_Edge = EdgeEnum.None
		FormToResize.Refresh()

		g = Nothing

	End Sub


End Class
 
One thing I forgot to mention - if you use FixedSingle then the form will have built in Minimum Height and Width therefore a side effect of the code is that when you reduce the size from the left border to the right border or from the top border to the bottom border, once these minimums have been reached the form will move down or left. If you use FixedToolWindow the form can be sized to the minimums you set (i.e. 0 by default).
 
I made some changes to the posted code, which I hadn't implemented in my code - and they don't work.

So change every reference to
[tt]
= OrginalCursor
[/tt]

to
[tt]
= Cursors.Default
[/tt]

Sorry about that - After posting it crossed my mind that you might have designed your own cursors and so that was a quick (but unfortunately untested solution to save the "incoming" cursor and restore it at the end. I should have tested first!!
 
I don't like being beaten, but I haven't had much time recently. I WILL solve these issues.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top