开发者

problems with moving window

I use below code to move window's form, move work fine, but problem is with opacity and close. I want to that work in this way: when I press button opacity=0.5, when I up button opacity=1, when the left button is down and I move mouse window move also, when I just click on form then form must close.

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() {
    开发者_StackOverflow中文版    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();
    }
}

Any idea how to repair this code?


Sending the WM_NCLBUTTONDOWN with HT_CAPTION will eat up your MouseUp event.

All you need to do is change the Opacity immediately after your SendMessage call.

Working Example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜