1

I am trying to build a total new window acts as Context Menu.

the only problem i have is: when I am pressing the mouse buttons outside the window (ContextMenu), the window does not close. I can't find the event that can catch this action.

this is the code i am using now:

public partial class ContextMenu : Window
{
    public ContextMenu()
    {
        InitializeComponent();
        this.ShowInTaskbar = false;
        this.Deactivated += new EventHandler(ContextMenu_Deactivated);
    }

    void ContextMenu_Deactivated(object sender, EventArgs e)
    {
        this.Hide();
    }

    protected override void OnDeactivated(EventArgs e)
    {
        base.OnDeactivated(e);
        this.Hide();
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        this.Hide();            
    }

    protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
    {
        base.OnKeyDown(e);
        this.Hide();           
    }

    protected override void OnLostFocus(RoutedEventArgs e)
    {
        base.OnLostFocus(e);
        this.Hide();            
    }

}

non of the functions above catches the mouse press outside the window (ContextMenu).

I have tried to use http://www.hardcodet.net/taskbar, but the examples I found are not something like what i am looking for.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
Assaf Zigdon
  • 461
  • 2
  • 5
  • 9

3 Answers3

0

Looks like you need processing of global mouse hooks.

Here is nice solution to this issue

http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

Imaver
  • 18
  • 3
0

A control cannot detect mouse clicks that are outside their bounding Rectangles. However, the Window can detect a mouse click anywhere within its border. Therefore, all you need to do is to handle a PreviewMouseDown event in the MainWindow.xaml.cs file and then pass a message to the relevant control each time the event is raised.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • i want the application to know when the mouse was pressed outside the window so it will close the window (like in context menu). you say that i have to press the mouse in a different window from the app? – Assaf Zigdon Jul 15 '14 at 14:20
0

I believe you'll want to use Mouse.Capture to detect a click away from your window.

This question+answer may lead you in the right direction:

How do I use CaptureMouse or Mouse.Capture in my C# WPF application?

Community
  • 1
  • 1
jschroedl
  • 4,916
  • 3
  • 31
  • 46