本文介绍了移动窗口的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来移动窗口的窗体,移动工作正常,但问题在于不透明度和关闭.我想以这种方式工作:当我按下按钮 opacity=0.5 时,当我按下按钮 opacity=1 时,当左键按下时我移动鼠标窗口也会移动,当我点击表单时,表单必须关闭.

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() {
        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?

推荐答案

使用 HT_CAPTION 发送 WM_NCLBUTTONDOWN 会耗尽你的 MouseUp 事件.

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

您需要做的就是在调用 SendMessage 后立即更改 Opacity.

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

工作示例:

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

这篇关于移动窗口的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:43