2011-08-08 49 views
1

我使用下面的代碼移動窗體的窗體,移動工作正常,但問題是不透明和關閉。我想這樣做:當我按下按鈕不透明度= 0.5時,當我向上按鈕不透明度= 1時,當左側按鈕向下時,我也移動鼠標窗口移動,當我只點擊窗體,然後窗體必須關閉。移動窗口的問題

using System; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

public partial class FormImage : Form { 

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

    public FormImage() { 
     InitializeComponent(); 
    } 

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

    private void FormImage_MouseDown(object sender, MouseEventArgs e) { 
     this.Opacity = 0.5; 
    } 

    private void FormImage_MouseUp(object sender, MouseEventArgs e) { 
     this.Opacity = 1; 
    } 

    private void FormImage_MouseClick(object sender, MouseEventArgs e) { 
     Close(); 
    } 
} 

任何想法如何修復此代碼?

回答

3

發送WM_NCLBUTTONDOWNHT_CAPTION會吃掉你的MouseUp事件。

您需要做的只是在撥打SendMessage後立即更改Opacity

工作實施例:

public partial class FormImage : Form 
{ 
    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(); 

    public FormImage() 
    { 
    InitializeComponent(); 
    } 

    private void FormImage_MouseDown(object sender, MouseEventArgs e) 
    { 
    this.Opacity = 0.5; 
    ReleaseCapture(); 
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
    this.Opacity = 1; 
    } 
}