2011-08-08 12 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; 
    } 
} 
関連する問題