2015-06-14 68 views
-1

我有一個沒有任何邊框的窗體窗體。 因此我添加了一個圖片框,並且我希望在單擊該圖片框時移動整個表單。從C中的圖片框移動窗體窗體#

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

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

這是我使用atm.But我的問題,它的代碼是,如果我移動光標很快它不是貼在圖片框。

我試圖找到一個解決方案,但沒有出現。 我用不同於2個鏈接的一些信息:

link 1

link 2

任何想法?

編輯: 這裏是我的表格整個代碼

public Form1() 
    { 
     InitializeComponent(); 
    } 

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

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

我無法複製。什麼是Picturebox的功能?你有沒有相同的代碼Form1_MouseDown?任何其他鼠標事件編碼? – TaW

+0

該函數是MouseDown。並且僅用於picturebox.And沒有其他鼠標事件編碼。 我只想從header_image中移動整個表單。 – Silver

回答

0

請參閱下面的代碼:

+0

感謝它的工作。 – Silver

0

使用MouseMove()事件,而不是MouseDown()

private void header_image_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      ReleaseCapture(); 
      SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
     } 
    } 
+0

仍然是同樣的問題.. – Silver