[英]How to resize borderless form from all edges
I have border-less simple win-form in c#. 我在C#中有无边界的简单胜利形式。 Now I want to resize form using mouse when form state is WindowState = FormWindowState.Normal
. 现在,当表单状态为WindowState = FormWindowState.Normal
时,我想使用鼠标调整表单大小。 Resize behavior should be same like normal form with border. 调整大小行为应与带边框的正常形式相同。
I used this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
我使用了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. 因为表单UI应该像平面UI。
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 VB代码
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 C#代码
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.