我们在做winform开发时 会觉得窗体的 最大化 关闭等按钮很丑 这时我们就会把窗体的属性FormBorderStyle设置为None 从而自定义按钮 但此时 窗体就不能够被移动了
下面贴一下 可以移动的代码
第一步:在程序中添加以下代码
#region 使窗体可以移动的代码
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
#endregion
第二步:在窗体的Mouse_Down事件中调用
private void Main_MouseDown(object sender, MouseEventArgs e)
{
//拖动窗体
this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
this.Cursor = System.Windows.Forms.Cursors.Default;
}
OK了 窗体可以移动了