110,530
社区成员
发帖
与我相关
我的任务
分享
// 移动窗体
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
// 窗体上鼠标按下时
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal)
{
// 移动窗体
this.Capture = false;
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
{
message.Result = (IntPtr)HTCAPTION;
}
}
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);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mypos = new Point(-e.X, -e.Y);
isok = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isok)
{
//Point xx = MousePosition;
Point xx = Control.MousePosition;
xx.Offset(mypos);
Location = xx;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isok = false;
}
}