2012-07-07 32 views
3

我有這個System.Windows.Forms.Panel,我想啓用,以便如果用戶單擊並拖動鼠標拖動窗口左右。如何在Panel移動窗體窗口中製作mousedrag?

我可以這樣做嗎?我必須執行多個事件嗎?

+0

使用面板的MouseMove事件。這不是航天科技。 – 2012-07-07 22:42:36

回答

3

您可以通過面板的MouseMove事件

例子應該是這樣的(對不起沒有測試過)

private void panel1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     this.Location = new Point(Cursor.Position.X + e.X , Cursor.Position.Y + e.Y); 
    } 
} 
0

你可能想看看這個組件,我貼在這裏:

http://pastebin.com/5ufJmuay

這是,你將能夠在一個窗體上放置,然後通過拖動拖動形式的組件在裏面。

16

最適合我的解決方案是使用非託管實現它代碼,它可以讓你平滑的移動窗口,不像HatSoft發佈的答案。

3個小步拖動窗口面板運動

using System.Runtime.InteropServices; 

添加這些六行類

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

[DllImportAttribute("user32.dll")] 
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 
[DllImportAttribute("user32.dll")] 
public static extern bool ReleaseCapture(); 

內和麪板的MouseMove事件應該是這樣的

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

貼吧有點遲了:),誰知道我們未來可能還需要它。

+0

發佈額外信用的消息常量。 – Rotem 2013-10-21 10:09:16

+0

例如'const int WM_NCLBUTTONDOWN = 0x00A1;' – Rotem 2013-10-21 11:13:39

+0

哦,是的,我錯過了,謝謝指出。 :)更新了我的答案 – Bravo 2013-10-21 11:27:28

-1

Bravo的代碼工作得非常好,但我無法得到這個工作,直到我明確地讓我的面板的panel's-> properties-> event部分中的MouseMove事件啓用時,我想移動。

+1

暗示 - WinForms 101.這篇文章只是一個評論。 – LarsTech 2017-05-05 20:57:40