2012-11-20 131 views
6

我有一個WinForms項目。我的窗戶頂部有一個面板。我希望該面板能夠移動窗口,當用戶點擊它然後拖動。通過點擊並拖動控件來移動窗口

我怎樣才能做到這一點?

+1

谷歌表示,這可能是一個重複:http://stackoverflow.com/questions/30184/winforms-click-drag-where-in-the-form-to-move-as-if-if-click-in-the-the-form – rie819

+2

不!不是這樣。我不希望用戶能夠在窗體的任何位置移動窗口。我希望用戶能夠從'panel1'控件移動窗口 – Victor

+2

在google中查看「Daniel Moth,Vista Glass」。我知道他的教程向你展示了一種方法,它可以讓你做到這一點(它是一個Win32調用)。這也許有些興趣http://www.codeproject.com/Articles/55180/Extending-the-Non-Client-Area-in-Aero – series0ne

回答

15

添加以下declerations到類:

public const int WM_NCLBUTTONDOWN = 0xA1; 
public const int HTCAPTION = 0x2; 

[DllImport("User32.dll")] 
public static extern bool ReleaseCapture(); 

[DllImport("User32.dll")] 
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 

在面板的MouseDown事件將這個:

private void panel1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     ReleaseCapture(); 
     SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); 
    } 
} 
+0

這個作品完美。非常感謝! – Victor

+0

很高興幫助:) – Blachshma

+0

非常好! +1避免抽象試圖數學計算出光標位置,偏移量等......但不是這個人,而是這個人直接跑到建築物,得到冰淇淋錐並且進入windows消息系統。太好了!謝謝。 –