Question

In my PowerPoint Add-in using VSTO, I am implementing an Application-level Mouse Hook to capture mouse events like double click, right click, mouse over etc, by using MouseKeyboardActivityMonitor.dll downloaded from Codeplex. The reason I am doing this is because PowerPoint doesn't have mouse related events to listen to and the ones it gives are not fired in edit-mode of the PowerPoint.

In my Add-in when user clicks a chart a menu appears which allows the user to perform various functions on the chart. All is working fine. I have captured mouse events and a customized menu is shown but the problem arises when the menu is closed after performing some action the default menu of PowerPoint appears on the screen.

Example: When user double clicks on the chart, I show my form menu like this.

//Listening to the MouseDoubleClick event
MyMouseHookListener.MouseDoubleClick += MyMouseHookListener_MouseDoubleClick;

//MouseDoubleClickEvent
void MyMouseHookListener_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
    FormMenu.ShowDialog(); //Displaying menu
}

This works fine, but when the user closes the form the default double click menu of PowerPoint Charts appears. Same is the problem with other mouse events.

How can I disable PowerPoint's event menus?

Update:

There is a bool property named Cancel which is provided by PowerPoint's WindowBeforeDoubleClick and WindowBeforeRightClick events. Which if set to true cancels the default action that PowerPoint performs when an event is fired. How can i access this property in my MouseHook events?

Était-ce utile?

La solution

There are several available events raised by the MouseKeyboardActivityMonitor. For a mouse down event you can choose to listen to either MouseDown or MouseDownExt. The latter supplies you with some extra options in the MouseEventExtArgs parameter, such as a Handled property. If you set this to true, the event will not propagate further.

When it comes to MouseDoubleClick event, there is no extended event available. I would therefore suggest that you implement the double click listener yourself by utilizing the MouseDownExt listener and counting the number of clicks that has occurred.

public void Initialize() {
    // Initialize your listener and set up event listeners
    MyMouseHookListener = new MouseHookListener(new AppHooker()) {Enabled = true};
    MyMouseHookListener.MouseDownExt += MyMouseHookListenerOnMouseDownExt;

    // UNDONE Delete when testing is done; included to show that the listener is never called
    MyMouseHookListener.MouseDoubleClick += MyMouseHookListenerOnMouseDoubleClick;
}

private static void MyMouseHookListenerOnMouseDoubleClick(object sender, MouseEventArgs mouseEventArgs)
{
    // NOTE: This listener should never be called
    Debug.Print("Mouse double-click!");
}

private static void MyMouseHookListenerOnMouseDownExt(object sender, MouseEventExtArgs mouseEventExtArgs)
{
    Debug.Print("Mouse down. Number of clicks: {0}", mouseEventExtArgs.Clicks);

    if (mouseEventExtArgs.Clicks == 2)
    {
        // TODO Insert your double-click code here
        mouseEventExtArgs.Handled = true;
    }
}

To be exhaustive, you should perhaps also test whether it is the left or right button that has been clicked, which you can to by checking the MouseEventExtArgs.Button property. A right-double-click will now be counted as a double-click, however, this is as far as I know similar to the original MouseDoubleClick event.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top