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);
}