简体   繁体   中英

Disable right click (press and hold) in a C# Winforms application per control on Windows XP Embedded

I am working on a touch screen application which is running on Windows XP Embedded. If the user presses and holds down for a few seconds, a context menu will appear. However, this interferes with holding down on controls like scrollbars, preventing the user from scrolling all the way to the end. However, I don't want to disable the right click functionality application-wide. I only want controls like scrollbars to be excluded from this action.

When the user presses down on the down arrow on the scrollbar on the touchscreen the log shows:

----Application Starts----
OnMouseDown: Left, Capture = true
[A few seconds later in the log, even though the user is still holding down...]
OnMouseUp: Left, Capture = false
OnMouseCaptureChanged, Capture = false
OnMouseDown: Right
OnMouseDown: Middle
OnMouseCaptureChanged, Capture = false
OnMouseCaptureChanged, Capture = false
OnMouseUp: Right
OnMouseUp: Middle
----Application Ends----

I attempted to filter out the right and middle mouse down and up events using PreFilterMessage. In ctor of a custom scrollbar control I added: Application.AddMessageFilter(this); Followed by the PreFilterMessage:

public bool PreFilterMessage(ref Message m)
{
    switch (m.Msg)
    {
        case (int)WindowsMessages.WM_RBUTTONDOWN:
        case (int)WindowsMessages.WM_RBUTTONUP:
        case (int)WindowsMessages.WM_MBUTTONDOWN:
            return true;
        case (int)WindowsMessages.WM_MBUTTONUP:
            System.Diagnostics.Debug.WriteLine("**** Context-menu interruption.");
            return true;
    }

    return false;
}

Which still interrupts the user's action.

I attempted to push an OnMouseDown in the WM_MBUTTONUP case, however this just causes the scrollbar to continue to scroll after the user has let go of the screen because their own mouse down event is no longer in the event queue so there is no mouse up to stop it.

this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1,
                                    MousePosition.X, MousePosition.Y, 0));

I attempted to use SendInput to perform a left down instead. But, encountered the same problem as before.

INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));

I then attempted to solve this with a post message, but this didn't help.

PostMessage(this.Handle, (int)WindowsMessages.WM_LBUTTONDOWN,
            (int)VirtualKeys.MK_LBUTTON,
            (uint)((MousePosition.Y << 16) | MousePosition.X));

I tried the following also in the ctor, with no effect:

this.ContextMenu = new ContextMenu();

I also attempted a mouse hook where I disabled right and middle click entirely and the context menu effect still caused a left mouse up that stopped the user's action.

Basically, there doesn't seem to be a way to recover from this and I haven't found any answers on stackoverflow to address this. However, there was a similar question regarding WPF on stackoverflow where the answer was to override the OnPreviewMouseRightButtonDown and OnPreviewMouseRightButtonUp for the control, which doesn't exist in Winforms.

Help!

in your case i have only a idea how can achieve this result.

you should make a method to show popup menu and add a time control on your form.

OnMouseDown event start your timer control and handel timer's tick event, store start time in a variable and check it on every tick, if you want that user should press for 3 seconds then you should check as like this, consider that you have the variable of start time named 'StartTime' :

if((DateTime.Now - StartTime).Seconds >= 3)

if elapsed time is time is over 3 seconds then show popup menu

now OnMouseUp event you should stop the timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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