首先添加可拖动区域先拖一个panel(name=panel1)再在panel1上拖动一个label上面写上”可拖动区域”。
然后在Form1.cs中添加以下代码:
[csharp] view plain copy print?
//定义无边框窗体Form
[DllImport(“user32.dll”)]//***********拖动无窗体的控件
public static extern bool ReleaseCapture();
[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 gPanelTitleBack_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//***********调用移动无窗体控件函数
}
让panel1使用此方法:
[csharp] view plain copy print?
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Firebrick;
this.panel1.Controls.Add(this.label2);
this.panel1.Location = new System.Drawing.Point(0, 1);
this.panel1.Name = “panel1”;
this.panel1.Size = new System.Drawing.Size(284, 22);
this.panel1.TabIndex = 1;
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gPanelTitleBack_MouseDown);