如果想拖曳 Windows Form 上的控件,要怎麼做呢? 在這邊用 groupBox 舉個例子,
1.先開個C#專案,然後拉一個 groupBox (假設名稱為 groupBox1 ) 到 Form1 上。
=============以下為重點============
2.
a.在全域的地方加入以下程式碼。 (我加在 Form1.Designer.cs 的 /// 設計工具所需的變數下)
[DllImportAttribute("user32.dll")]
private extern static bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
private extern static int SendMessage(IntPtr handle, int m, int p, int h);
b.加入 2a 會用到 using ,請加入以下程式碼。
using System.Runtime.InteropServices;
using System;
3.將 groupBox1 加入滑鼠點擊事件,請加入以下程式碼。(我加在 public Form1() { InitializeComponent();
下)
groupBox1.MouseDown += new MouseEventHandler(MyBaseControl_MouseDown);
4.加入以下程式碼,
protected void MyBaseControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
this.Cursor = Cursors.SizeAll; ReleaseCapture();
SendMessage(groupBox1.Handle, 0xA1, 0x2, 0);
this.Cursor = Cursors.Default;
}
}
( 這段程式碼就是 3 滑鼠事件註冊的函式 )
然後就建置,應該就可以執行了。
參考來源:http://www.soaspx.com/dotnet/csharp/csharp_20100306_3065.html
留言列表