Outman
Outman

Reputation: 3330

Add click event everywhere on a winform except for a specific panel

I was looking for an answer and found an almost complete one by @LarsTech but without the exception : https://stackoverflow.com/a/21314496/7026554

//You can still use MessageFilter and just filter for the ActiveForm:

private class MouseDownFilter : IMessageFilter {
  public event EventHandler FormClicked;
  private int WM_LBUTTONDOWN = 0x201;
  private Form form = null;

  [DllImport("user32.dll")]
  public static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);

  public MouseDownFilter(Form f) {
    form = f;
  }

  public bool PreFilterMessage(ref Message m) {
    if (m.Msg == WM_LBUTTONDOWN) {
      if (Form.ActiveForm != null && Form.ActiveForm.Equals(form)) {
        OnFormClicked();
      }
    }
    return false;
  }

  protected void OnFormClicked() {
    if (FormClicked != null) {
      FormClicked(form, EventArgs.Empty);
    }
  }
}

//Then in your form, attach it:

public Form1() {
  InitializeComponent();
  MouseDownFilter mouseFilter = new MouseDownFilter(this);
  mouseFilter.FormClicked += mouseFilter_FormClicked;
  Application.AddMessageFilter(mouseFilter);
}

void mouseFilter_FormClicked(object sender, EventArgs e) {
  // do something...
}

What I want is to hide the notifications panel but not when clicking on its content or the profile picture that shows it. I have a procedure called NotificationVisible(bool IsVisible) Any help is greatly appreciated.

Upvotes: 0

Views: 235

Answers (1)

Sergey Shevchenko
Sergey Shevchenko

Reputation: 570

So your mouseFilter_FormClicked fires whenever user clickes on form. Now the only thing you need to do is to detect the control that is placed behind the mouse cursor and with that you can deside whether you need to hide your panel or not.

You can do this with WindowFromPoint method. Check this thread.

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pnt);

void mouseFilter_FormClicked(object sender, EventArgs e) {        
    IntPtr hWnd = WindowFromPoint(Control.MousePosition);
    if (hWnd != IntPtr.Zero) {
        Control ctl = Control.FromHandle(hWnd);
        if (ctl != YourPanelControl) {
             HideThePanel();
        }
    }
}

Upvotes: 1

Related Questions