1、不允许窗体被拖动。即使点击蓝色标题条。
代码片段,加入不想被拖动的窗体中即可
protectedoverridevoid WndProc(refMessage m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
if ((IntPtr)2 == m.Result)
{
m.Result = (IntPtr)1;
}
}
}
2、使用API函数,拖动窗体的任意地方,使得窗体被拖动
代码片段如下,开放Form1_MouseDown事件。
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
publicstaticexternbool ReleaseCapture();
[DllImport("user32.dll")]
publicstaticexternbool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
publicconstint WM_SYSCOMMAND = 0x0112;
publicconstint SC_MOVE = 0xF010;
publicconstint HTCAPTION = 0x0002;
privatevoid Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
3、利用APi函数,实现任意控件的拖动。
代码片段如下,挂上要被拖动的控件的MouseDown事件即可
using System.Runtime.InteropServices;
constuint WM_SYSCOMMAND = 0x0112;
constuint SC_MOVE = 0xF010;
constuint HTCAPTION = 0x0002;
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
privatestaticexternint SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
[DllImport("user32.dll")]
privatestaticexternint ReleaseCapture();
void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage((sender asControl).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}