感谢您之前对我的问题的回答。 你可以看到以下链接。
How to minimize and maximize in C#.Net?
现在我面临另一个问题。 当我将表单的bordertyle更改为none时,我无法像真实表单那样移动表单。 它稳定而且无法移动到任何地方。
在Windows窗体中,正常的bordertyle可以在任何地方移动。 但是我想在bordertyle的none属性中移动。 我怎样才能做到这一点? 如果可以,请告诉我。 谢谢你的时间。 :)
答案 0 :(得分:10)
public class AppFormBase : Form
{
protected override void OnLoad(EventArgs e)
{
if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
{
this.MouseDown += new MouseEventHandler(AppFormBase_MouseDown);
this.MouseMove += new MouseEventHandler(AppFormBase_MouseMove);
this.MouseUp += new MouseEventHandler(AppFormBase_MouseUp);
}
base.OnLoad(e);
}
void AppFormBase_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
downPoint = new Point(e.X, e.Y);
}
void AppFormBase_MouseMove(object sender, MouseEventArgs e)
{
if (downPoint == Point.Empty)
{
return;
}
Point location = new Point(
this.Left + e.X - downPoint.X,
this.Top + e.Y - downPoint.Y);
this.Location = location;
}
void AppFormBase_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
downPoint = Point.Empty;
}
public Point downPoint = Point.Empty;
}
答案 1 :(得分:4)
看一下本教程:http://www.codeproject.com/KB/cs/csharpmovewindow.aspx。
这是它的要点:
using System.Runtime.InteropServices;
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
答案 2 :(得分:1)
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Capture = false;
Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
base.WndProc(ref msg);
}