Answered by:
Custom Form Border Style....
Question
-
Hi.,
I am new to VB.Net...
I have created a app... and I am searching for custom form border style.... with minimize and close button ....
is it possible ? if yes then how..?
please help
- Edited by Amol53462 Saturday, June 14, 2014 11:26 AM
Saturday, June 14, 2014 11:25 AM
Answers
-
Here is an example of making the form as borderless, drawing the border, and being able to resize or move the form also. You can create a new Form project and add 2 buttons to the form. Then copy this code. You can experiment with it to get a feel for what i was saying in my first post.
Public Class Form1 Private Const WM_NCHITTEST As Integer = &H84 Private Const WM_MOUSEMOVE As Integer = &H200 Private Const WM_LBUTTONDOWN As Integer = &H201 Private Const WM_LBUTTONUP As Integer = &H202 Private Const MK_LBUTTON As Integer = &H1 Private Const HTLEFT As Integer = &HA Private Const HTRIGHT As Integer = &HB Private Const HTTOP As Integer = &HC Private Const HTTOPLEFT As Integer = &HD Private Const HTTOPRIGHT As Integer = &HE Private Const HTBOTTOM As Integer = &HF Private Const HTBOTTOMLEFT As Integer = &H10 Private Const HTBOTTOMRIGHT As Integer = &H11 Private OffSet As Point = Point.Empty Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.FormBorderStyle = FormBorderStyle.None Button1.Size = New Size(23, 23) Button2.Size = New Size(23, 23) Button1.Font = New Font(Button1.Font, FontStyle.Bold) Button2.Font = New Font(Button2.Font, FontStyle.Bold) Button1.Text = "_" Button2.Text = "X" Button2.Location = New Point(Me.Width - Button2.Width - 3, 3) Button1.Location = New Point(Me.Width - Button2.Width - Button1.Width - 6, 3) End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint e.Graphics.FillRectangle(Brushes.Blue, 0, 0, Me.Width, 30) e.Graphics.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, 29) e.Graphics.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, Me.Height - 1) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.WindowState = FormWindowState.Minimized End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Protected Overrides Sub WndProc(ByRef m As Message) If m.Msg = WM_NCHITTEST Then Dim loc As New Point(m.LParam.ToInt32 And &HFFFF, m.LParam.ToInt32 >> 16) loc = PointToClient(loc) Dim bTop As Boolean = (loc.Y < ClientRectangle.Y + 4) Dim bLeft As Boolean = (loc.X < ClientRectangle.X + 4) Dim bRight As Boolean = (loc.X > Width - 4) Dim bBottom As Boolean = (loc.Y > Height - 4) If bTop And bLeft Then m.Result = CType(HTTOPLEFT, IntPtr) Return ElseIf bTop And bRight Then m.Result = CType(HTTOPRIGHT, IntPtr) Return ElseIf bBottom And bLeft Then m.Result = CType(HTBOTTOMLEFT, IntPtr) Return ElseIf bBottom And bRight Then m.Result = CType(HTBOTTOMRIGHT, IntPtr) Return ElseIf bLeft Then m.Result = CType(HTLEFT, IntPtr) Return ElseIf bTop Then m.Result = CType(HTTOP, IntPtr) Return ElseIf bRight Then m.Result = CType(HTRIGHT, IntPtr) Return ElseIf bBottom Then m.Result = CType(HTBOTTOM, IntPtr) Return End If ElseIf m.Msg = WM_LBUTTONDOWN Then OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y) ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y) End If MyBase.WndProc(m) End Sub Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged Button2.Location = New Point(Me.Width - Button2.Width - 3, 3) Button1.Location = New Point(Me.Width - Button2.Width - Button1.Width - 6, 3) Me.Refresh() End Sub End Class
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 12:50 PM -
CodePlex - Drawing Custom Borders in Windows Forms
Code Project - Winforms SkinFramework
I found a link at Code Project the other day I believe that allowed increasing the titlebar or captionbar (top border) of a Forms height while maintaining the aero glass transparency affect of that area of a form but can not find it now. :( I don't know if either of the above links do that.
La vida loca
- Edited by Mr. Monkeyboy Saturday, June 14, 2014 1:39 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 1:05 PM -
Hi,
To make a custom border you can set the Forms BorderStyle to None and then use the Forms Paint event to draw your own border. You could do that using some of the Graphics methods or using the ControlPaint.DrawBorder method. You can check them out at the links below. Also, if you set the Forms BorderStyle to None and you want to still be able to move or resize the form then you can check out the (Float a Borderless Form On Desktop) link below. As for the buttons, you can ether use standard Buttons and position them in the location you want but, you would need to use the forms SizeChanged event to re-position them as the form it resized, or the second way would be to draw the buttons in the Forms Paint event but, that may get a little more involved to be able to tell when one is clicked or the mouse is hovered over it.
In short, its not a real simple task if your not familiar with VB.Net programing and the Graphics class methods. It can be done though.
ControlPaint.DrawBorder Method
Float a borderless form on desktop
You could also do some searches at the top of the forum in the Search box for Making Custom Forms or try some other key words and you may find an example in one of them that comes close to what you want. :)
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 12:20 PM
All replies
-
Hi,
To make a custom border you can set the Forms BorderStyle to None and then use the Forms Paint event to draw your own border. You could do that using some of the Graphics methods or using the ControlPaint.DrawBorder method. You can check them out at the links below. Also, if you set the Forms BorderStyle to None and you want to still be able to move or resize the form then you can check out the (Float a Borderless Form On Desktop) link below. As for the buttons, you can ether use standard Buttons and position them in the location you want but, you would need to use the forms SizeChanged event to re-position them as the form it resized, or the second way would be to draw the buttons in the Forms Paint event but, that may get a little more involved to be able to tell when one is clicked or the mouse is hovered over it.
In short, its not a real simple task if your not familiar with VB.Net programing and the Graphics class methods. It can be done though.
ControlPaint.DrawBorder Method
Float a borderless form on desktop
You could also do some searches at the top of the forum in the Search box for Making Custom Forms or try some other key words and you may find an example in one of them that comes close to what you want. :)
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 12:20 PM -
Here is an example of making the form as borderless, drawing the border, and being able to resize or move the form also. You can create a new Form project and add 2 buttons to the form. Then copy this code. You can experiment with it to get a feel for what i was saying in my first post.
Public Class Form1 Private Const WM_NCHITTEST As Integer = &H84 Private Const WM_MOUSEMOVE As Integer = &H200 Private Const WM_LBUTTONDOWN As Integer = &H201 Private Const WM_LBUTTONUP As Integer = &H202 Private Const MK_LBUTTON As Integer = &H1 Private Const HTLEFT As Integer = &HA Private Const HTRIGHT As Integer = &HB Private Const HTTOP As Integer = &HC Private Const HTTOPLEFT As Integer = &HD Private Const HTTOPRIGHT As Integer = &HE Private Const HTBOTTOM As Integer = &HF Private Const HTBOTTOMLEFT As Integer = &H10 Private Const HTBOTTOMRIGHT As Integer = &H11 Private OffSet As Point = Point.Empty Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.FormBorderStyle = FormBorderStyle.None Button1.Size = New Size(23, 23) Button2.Size = New Size(23, 23) Button1.Font = New Font(Button1.Font, FontStyle.Bold) Button2.Font = New Font(Button2.Font, FontStyle.Bold) Button1.Text = "_" Button2.Text = "X" Button2.Location = New Point(Me.Width - Button2.Width - 3, 3) Button1.Location = New Point(Me.Width - Button2.Width - Button1.Width - 6, 3) End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint e.Graphics.FillRectangle(Brushes.Blue, 0, 0, Me.Width, 30) e.Graphics.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, 29) e.Graphics.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, Me.Height - 1) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.WindowState = FormWindowState.Minimized End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Protected Overrides Sub WndProc(ByRef m As Message) If m.Msg = WM_NCHITTEST Then Dim loc As New Point(m.LParam.ToInt32 And &HFFFF, m.LParam.ToInt32 >> 16) loc = PointToClient(loc) Dim bTop As Boolean = (loc.Y < ClientRectangle.Y + 4) Dim bLeft As Boolean = (loc.X < ClientRectangle.X + 4) Dim bRight As Boolean = (loc.X > Width - 4) Dim bBottom As Boolean = (loc.Y > Height - 4) If bTop And bLeft Then m.Result = CType(HTTOPLEFT, IntPtr) Return ElseIf bTop And bRight Then m.Result = CType(HTTOPRIGHT, IntPtr) Return ElseIf bBottom And bLeft Then m.Result = CType(HTBOTTOMLEFT, IntPtr) Return ElseIf bBottom And bRight Then m.Result = CType(HTBOTTOMRIGHT, IntPtr) Return ElseIf bLeft Then m.Result = CType(HTLEFT, IntPtr) Return ElseIf bTop Then m.Result = CType(HTTOP, IntPtr) Return ElseIf bRight Then m.Result = CType(HTRIGHT, IntPtr) Return ElseIf bBottom Then m.Result = CType(HTBOTTOM, IntPtr) Return End If ElseIf m.Msg = WM_LBUTTONDOWN Then OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y) ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y) End If MyBase.WndProc(m) End Sub Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged Button2.Location = New Point(Me.Width - Button2.Width - 3, 3) Button1.Location = New Point(Me.Width - Button2.Width - Button1.Width - 6, 3) Me.Refresh() End Sub End Class
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 12:50 PM -
CodePlex - Drawing Custom Borders in Windows Forms
Code Project - Winforms SkinFramework
I found a link at Code Project the other day I believe that allowed increasing the titlebar or captionbar (top border) of a Forms height while maintaining the aero glass transparency affect of that area of a form but can not find it now. :( I don't know if either of the above links do that.
La vida loca
- Edited by Mr. Monkeyboy Saturday, June 14, 2014 1:39 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, June 23, 2014 8:28 AM
Saturday, June 14, 2014 1:05 PM