2011-08-08 113 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; 
    } 
}