之前做過拖動,找了兩種方法:
1、
private Point myPoint;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosition = Control.MousePosition;
myPosition.Offset(myPoint.X, myPoint.Y);
this.DesktopLocation = myPosition;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosition = Control.MousePosition;
myPosition.Offset(myPoint.X, myPoint.Y);
this.DesktopLocation = myPosition;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
myPoint = new Point(-e.X, -e.Y);
}
這種方法要加三個事件Form1_MouseMove,Form1_MouseUp,Form1_MouseDown,可以實現但是拖動過程中有殘影。
2、
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
try
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
catch
{ }
}
這種方法沒有殘影。
作者“yysyangyangyangshan的專欄”