C# 如何在所有的it上拖动无边框窗体';s区?如何调整无边框窗体的大小?
我已经为您创建了一个相当简单的类,它将完成您需要的所有操作。可以创建快捷键,调整窗体的大小(即使窗体没有边框),也可以通过单击并拖动窗体内的任意点来移动窗体。希望这有帮助,我尽了最大努力在注释中解释代码。如果你需要更多的澄清,请告诉我 CustomForm.cs:C# 如何在所有的it上拖动无边框窗体';s区?如何调整无边框窗体的大小?,c#,winforms,C#,Winforms,我已经为您创建了一个相当简单的类,它将完成您需要的所有操作。可以创建快捷键,调整窗体的大小(即使窗体没有边框),也可以通过单击并拖动窗体内的任意点来移动窗体。希望这有帮助,我尽了最大努力在注释中解释代码。如果你需要更多的澄清,请告诉我 CustomForm.cs: protected override void WndProc(ref Message m) { switch (m.Msg) { cas
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x84:
base.WndProc(ref m);
if ((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
这些问题在这里已经被多次回答了。总是在问之前谷歌这里!!!到目前为止,他们没有一个像我想要的那样工作。我希望它是无边界的,可以从任何窗体区域移动。Hans link可以拖动窗体,但只能从顶部的小部件栏拖动。在我的问题代码中,我可以从表单的任何区域拖动表单,但不能调整大小。所有的答案不是这个就是这个。无法使其从任何窗体区域移动,也无法调整大小。您只需处理窗体的MouseDown、MouseMove和MouseUp事件即可。在MouseDown事件中,存储当前鼠标指针位置,在MouseMove事件中,将其与当前进行比较并设置窗体位置,在MouseUp事件中,重新设置起始点值。回答正确。只需记住处置
mybrush
OnFormClosed
。如果表单在应用程序的生命周期中多次打开/关闭,这将是一个问题。您也可以在OnPaint
事件中使用(Brush mybrush=new SolidBrush()){}(如果引用的颜色由于某种原因发生变化,这也会很方便)
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x84:
base.WndProc(ref m);
if ((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
public class CustomForm : Form
{
/// <summary>
/// How close your cursor must be to any of the sides/corners of the form to be able to resize
/// </summary>
public int GrabSize = 8;
/// <summary>
/// The shortcut keys for this form
/// </summary>
public Dictionary<Keys, Action> ShortcutKeys = new Dictionary<Keys, Action>();
private bool Drag = false;
private Point DragOrigin;
private const int HT_LEFT = 10;
private const int HT_RIGHT = 11;
private const int HT_TOP = 12;
private const int HT_BOTTOM = 15;
private const int HT_TOPLEFT = 13;
private const int HT_TOPRIGHT = 14;
private const int HT_BOTTOMLEFT = 16;
private const int HT_BOTTOMRIGHT = 17;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// If hold left click on the form, then start the dragging operation
if (e.Button == MouseButtons.Left)
{
// Only start dragging operation if cursor position is NOT within the resize regions
if (e.X > GrabSize && e.X < Width - GrabSize && e.Y > GrabSize && e.Y < Height - GrabSize)
{
DragOrigin = e.Location;
Drag = true;
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// If let go of left click while dragging the form, then stop the dragging operation
if (e.Button == MouseButtons.Left && Drag)
Drag = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// Move the form location based on where the cursor has moved relative to where the cursor position was when we first started the dragging operation
if (Drag)
Location = new Point(Location.X + (e.Location.X - DragOrigin.X), Location.Y + (e.Location.Y - DragOrigin.Y));
}
/// <summary>
/// Invokes any shortcut keys that have been added to this form
/// </summary>
protected override bool ProcessCmdKey(ref Message msg, Keys key)
{
Action action;
if(ShortcutKeys.TryGetValue(key, out action))
{
action.Invoke();
return true;
}
return base.ProcessCmdKey(ref msg, key);
}
/// <summary>
/// Handles resizing of a borderless control/winform
/// </summary>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if(FormBorderStyle == FormBorderStyle.None && m.Msg == 0x84)
{
Point CursorLocation = PointToClient(new Point(m.LParam.ToInt32()));
if (CursorLocation.X <= GrabSize)
{
if (CursorLocation.Y <= GrabSize) // TOP LEFT
m.Result = new IntPtr(HT_TOPLEFT);
else if (CursorLocation.Y >= ClientSize.Height - GrabSize) // BOTTOM LEFT
m.Result = new IntPtr(HT_BOTTOMLEFT);
else
m.Result = new IntPtr(HT_LEFT); // RESIZE LEFT
}
else if (CursorLocation.X >= ClientSize.Width - GrabSize)
{
if (CursorLocation.Y <= GrabSize)
m.Result = new IntPtr(HT_TOPRIGHT); // RESIZE TOP RIGHT
else if (CursorLocation.Y >= ClientSize.Height - GrabSize)
m.Result = new IntPtr(HT_BOTTOMRIGHT); // RESIZE BOTTOM RIGHT
else
m.Result = new IntPtr(HT_RIGHT); // RESIZE RIGHT
}
else if (CursorLocation.Y <= GrabSize)
m.Result = new IntPtr(HT_TOP); // RESIZE TOP
else if (CursorLocation.Y >= ClientSize.Height - GrabSize)
m.Result = new IntPtr(HT_BOTTOM); // RESIZE BOTTOM
}
}
}
public partial class Form1 : CustomForm
{
SolidBrush mybrush;
private const int cGrip = 16; // Grip size
private const int cCaption = 32; // Caption bar height
public Form1()
{
InitializeComponent();
mybrush = new SolidBrush(this.BackColor);
// Adds a new shortcut key that will invoke CreateNewForm every time Ctrl+F is pressed
ShortcutKeys.Add(Keys.Control | Keys.F, CreateNewForm);
this.FormBorderStyle = FormBorderStyle.None;
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
e.Graphics.FillRectangle(mybrush, rc);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
/// <summary>
/// Create a new instance of this form
/// </summary>
private void CreateNewForm()
{
new Form1().Show();
}
}