简体   繁体   English

在winforms中,如何使控件不接受鼠标事件

[英]in winforms, how can i make a control not accept mouse events

I have a button and upon mouseenter, a little form pops up, and upon mouseleave of the button, the little form disappears. 我有一个按钮,在鼠标中心,弹出一个小形状,按下鼠标左键,小形状消失。 I am needing this form to not accept any mouse events, in other words, be "invisible" to the mouse. 我需要这个表单不接受任何鼠标事件,换句话说,对鼠标“隐形”。

The problem is, the form pops up under the mouse, which triggers the mouseleave event for the button. 问题是,窗体弹出窗体,触发按钮的mouseleave事件。 I know there are other ways to get around this, but i'm needing the form to hide when the mouse leaves the original button that triggered the form, and I also need the form to appear underneath the mouse. 我知道还有其他方法来解决这个问题,但是当鼠标离开触发表单的原始按钮时我需要隐藏表单,我还需要在鼠标下方显示该表单。

So how can I make the little pop-up form invisible to mouse-events, so that it doesn't cause the "mouse leave" event to trigger for the button? 那么如何才能使小弹出窗体对鼠标事件不可见,这样它就不会导致“鼠标离开”事件触发按钮?

The popup is of type "Form". 弹出窗口的类型为“Form”。 Here is the mouseEnter and mouseLeave code that triggers showing and hiding the form: 这是触发显示和隐藏表单的mouseEnter和mouseLeave代码:

private void btnPatientSearch_MouseEnter(object sender, EventArgs e)
        {
                _currentPatientInfo = new PatientInfo()
                {
                    MdiParent = this.MdiParent
                };
                _currentPatientInfo.Show();
                _currentPatientInfo.Location = new Point(181, 9);
            }
        }

        private void btnPatientSearch_MouseLeave(object sender, EventArgs e)
        {
            if (_currentPatientInfo == null) return;
            _currentPatientInfo.Hide();
            _currentPatientInfo = null;
        }

Inherit your popup form from the following form class. 从以下表单类继承您的弹出窗体。 This code is using some p/invokes and not tested, but it should work. 此代码使用了一些p / invokes而未经过测试,但它应该可以工作。

public class PopupForm : Form
{
  private const int WS_BORDER = 0x00800000;
  private const int WS_POPUP = unchecked((int)0x80000000);

  private const int WS_EX_TOPMOST = 0x00000008;
  private const int WS_EX_NOACTIVATE = 0x08000000;

  private const int WM_MOUSEACTIVATE = 0x0021;
  private const int MA_NOACTIVATEANDEAT = 4;

  private static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);

  private const int SWP_NOSIZE = 0x0001;
  private const int SWP_NOMOVE = 0x0002;
  private const int SWP_NOACTIVATE = 0x0010;

  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
    int X, int Y, int cx, int cy, int uFlags);

  public PopupForm()
  {
    SetStyle(ControlStyles.Selectable, false);
    FormBorderStyle = FormBorderStyle.None;
    StartPosition = FormStartPosition.Manual;
    ShowInTaskbar = false;
    Visible = false;
  }

  protected override CreateParams CreateParams
  {
    get
    {
      CreateParams cp = base.CreateParams;
      cp.Style |= WS_POPUP | WS_BORDER;
      cp.ExStyle |= WS_EX_TOPMOST | WS_EX_NOACTIVATE;
      return cp;
    }
  }

  protected override bool ShowWithoutActivation
  {
    get { return true; }
  }

  protected override void WndProc(ref Message m)
  {
    if (m.Msg == WM_MOUSEACTIVATE)
    {
      OnClick(EventArgs.Empty);
      m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
    }
    else
      base.WndProc(ref m);
  }

  public new void Show()
  {
    Windows.SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
      SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
    base.Show();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM