我在 C# 中有无边框的简单 win 表单。现在我想在表单状态为 时使用鼠标调整表单大小WindowState = FormWindowState.Normal。调整大小行为应该与带边框的正常形式相同。
我用了this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
附上表格图片供参考。
请建议我执行此任务的最佳方法。要求具有无边框形式。因为表单 UI 应该像平面 UI 一样。
给你,来自以下链接的第二个答案的副本。如果需要的话请翻译一下。
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
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2810 次 |
| 最近记录: |