Reputation: 301
I have border-less simple win-form in c#. Now I want to resize form using mouse when form state is WindowState = FormWindowState.Normal
. Resize behavior should be same like normal form with border.
I used this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Attached is form image for reference.
Please suggest me the best way to perform this task. It is required to have border-less form. Because form UI should be like flat UI.
Upvotes: 1
Views: 3205
Reputation: 764
Here you go, a copy of the 2nd answer from the below link. Translate it if you need to.
How to move and resize a form without a border?
VB Code
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.None
Me.DoubleBuffered = True
Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.FillRectangle(Brushes.Green, Top)
e.Graphics.FillRectangle(Brushes.Green, Left)
e.Graphics.FillRectangle(Brushes.Green, Right)
e.Graphics.FillRectangle(Brushes.Green, Bottom)
End Sub
Private Const HTLEFT As Integer = 10, HTRIGHT As Integer = 11, HTTOP As Integer = 12, HTTOPLEFT As Integer = 13, HTTOPRIGHT As Integer = 14, HTBOTTOM As Integer = 15, HTBOTTOMLEFT As Integer = 16, HTBOTTOMRIGHT As Integer = 17
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = &H84 Then
Dim mp = Me.PointToClient(Cursor.Position)
If TopLeft.Contains(mp) Then
m.Result = CType(HTTOPLEFT, IntPtr)
ElseIf TopRight.Contains(mp) Then
m.Result = CType(HTTOPRIGHT, IntPtr)
ElseIf BottomLeft.Contains(mp) Then
m.Result = CType(HTBOTTOMLEFT, IntPtr)
ElseIf BottomRight.Contains(mp) Then
m.Result = CType(HTBOTTOMRIGHT, IntPtr)
ElseIf Top.Contains(mp) Then
m.Result = CType(HTTOP, IntPtr)
ElseIf Left.Contains(mp) Then
m.Result = CType(HTLEFT, IntPtr)
ElseIf Right.Contains(mp) Then
m.Result = CType(HTRIGHT, IntPtr)
ElseIf Bottom.Contains(mp) Then
m.Result = CType(HTBOTTOM, IntPtr)
End If
End If
End Sub
Dim rng As New Random
Function randomColour() As Color
Return Color.FromArgb(255, rng.Next(255), rng.Next(255), rng.Next(255))
End Function
Const ImaginaryBorderSize As Integer = 16
Function Top() As Rectangle
Return New Rectangle(0, 0, Me.ClientSize.Width, ImaginaryBorderSize)
End Function
Function Left() As Rectangle
Return New Rectangle(0, 0, ImaginaryBorderSize, Me.ClientSize.Height)
End Function
Function Bottom() As Rectangle
Return New Rectangle(0, Me.ClientSize.Height - ImaginaryBorderSize, Me.ClientSize.Width, ImaginaryBorderSize)
End Function
Function Right() As Rectangle
Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, Me.ClientSize.Height)
End Function
Function TopLeft() As Rectangle
Return New Rectangle(0, 0, ImaginaryBorderSize, ImaginaryBorderSize)
End Function
Function TopRight() As Rectangle
Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, ImaginaryBorderSize)
End Function
Function BottomLeft() As Rectangle
Return New Rectangle(0, Me.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize)
End Function
Function BottomRight() As Rectangle
Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, Me.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize)
End Function
End Class
C# Code
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Transparent, Top());
e.Graphics.FillRectangle(Brushes.Transparent, Left());
e.Graphics.FillRectangle(Brushes.Transparent, Right());
e.Graphics.FillRectangle(Brushes.Transparent, Bottom());
}
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
var mp = this.PointToClient(Cursor.Position);
if (TopLeft().Contains(mp))
m.Result = (IntPtr)HTTOPLEFT;
else if (TopRight().Contains(mp))
m.Result = (IntPtr)HTTOPRIGHT;
else if (BottomLeft().Contains(mp))
m.Result = (IntPtr)HTBOTTOMLEFT;
else if (BottomRight().Contains(mp))
m.Result = (IntPtr)HTBOTTOMRIGHT;
else if (Top().Contains(mp))
m.Result = (IntPtr)HTTOP;
else if (Left().Contains(mp))
m.Result = (IntPtr)HTLEFT;
else if (Right().Contains(mp))
m.Result = (IntPtr)HTRIGHT;
else if (Bottom().Contains(mp))
m.Result = (IntPtr)HTBOTTOM;
}
}
private Random rng = new Random();
public Color randomColour()
{
return Color.FromArgb(255, rng.Next(255), rng.Next(255), rng.Next(255));
}
const int ImaginaryBorderSize = 2;
public new Rectangle Top()
{
return new Rectangle(0, 0, this.ClientSize.Width, ImaginaryBorderSize);
}
public new Rectangle Left()
{
return new Rectangle(0, 0, ImaginaryBorderSize, this.ClientSize.Height);
}
public new Rectangle Bottom()
{
return new Rectangle(0, this.ClientSize.Height - ImaginaryBorderSize, this.ClientSize.Width, ImaginaryBorderSize);
}
public new Rectangle Right()
{
return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, this.ClientSize.Height);
}
public Rectangle TopLeft()
{
return new Rectangle(0, 0, ImaginaryBorderSize, ImaginaryBorderSize);
}
public Rectangle TopRight()
{
return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, ImaginaryBorderSize);
}
public Rectangle BottomLeft()
{
return new Rectangle(0, this.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize);
}
public Rectangle BottomRight()
{
return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, this.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize);
}
Upvotes: 5