移动窗口的问题

时间:2011-08-08 10:47:35

标签: c# window

我使用下面的代码移动窗口的表单,移动工作正常,但问题是不透明度和关闭。我希望以这种方式工作:当我按下按钮opacity = 0.5,当我向上按钮opacity = 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();
    }
}

知道如何修复此代码吗?

1 个答案:

答案 0 :(得分:3)

使用WM_NCLBUTTONDOWN发送HT_CAPTION将耗尽您的MouseUp事件。

您需要做的就是在Opacity来电后立即更改SendMessage

工作示例:

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;
  }
}