How to open window system menu on right click?

miroxlav

I have a borderless splash screen form displaying loading progress and containing Minimize and Close buttons (something similar to splash screens seen in Office 2013). I would also like to provide system menu of the window which opens on right click anywhere in the form.

Classic system menu of a window

Currently I'm achieving opening the menu by sending keys Alt+Space.

    System.Windows.Forms.SendKeys.SendWait("% ")   'sending Alt+Space

With this approach, window system menu always opens in top-left corner of the window.

Is there a way to programatically open the system menu the same way as Windows does natively when user right-clicks title bar of the window? An API call or message which pops up the menu open?

I would like to keep the system menu available in the app because I have added also items "About" and "Settings" in there. (This app serves as an independent launcher and updater of the core app.)

The platform is WPF with Windows Forms library included, too (due to that workaround using SendWait()). Feel free to choose VB or C# in case of posting some code.

Hans Passant

There is no baked-in winapi function to display the system menu. You can display it yourself by using pinvoke. The GetSystemMenu() function returns a handle to the system menu, you display it by using TrackPopupMenu(), you execute the selected command by calling SendMessage to send the WM_SYSCOMMAND.

Some sample code that shows how to do this and includes the necessary declarations:

using System.Runtime.InteropServices;
...
    private void Window_MouseDown(object sender, MouseButtonEventArgs e) {
        if (e.ChangedButton == MouseButton.Right) {
            IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
            RECT pos;
            GetWindowRect(hWnd, out pos);
            IntPtr hMenu = GetSystemMenu(hWnd, false);
            int cmd = TrackPopupMenu(hMenu, 0x100, pos.left, pos.top, 0, hWnd, IntPtr.Zero);
            if (cmd > 0) SendMessage(hWnd, 0x112, (IntPtr)cmd, IntPtr.Zero);
        }
    }
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    [DllImport("user32.dll")]
    static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport("user32.dll")]
    static extern int TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y,
       int nReserved, IntPtr hWnd, IntPtr prcRect);
    [DllImport("user32.dll")]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
    struct RECT { public int left, top, right, bottom; }

Note that you can display the menu anywhere you want, I just picked the upper-left corner of the window. Beware that the position values are in pixels.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

System hangs on "Open document" right click context menu (Xubuntu)

From Dev

How to add "open with custom command" option in right click menu of Nautilus

From Dev

How to add "open with custom command" option in right click menu of Nautilus

From Dev

Make GIMP 2.8 open in a maximised window and add an "Edit with GIMP" right-click context menu

From Dev

Adding an item to right-click menu on window

From Java

Open in ConEmu right click menu windows 7

From Dev

How to open D3.js Context Menu on left click instead of Right click

From Dev

How to open D3.js Context Menu on left click instead of Right click

From Dev

When the window menu is in the title bar, how can I set the focus click to not open a menu?

From Dev

How to actually click a generated right click menu?

From Dev

How to react to 'Close window' in right-click menu of task bar in Windows

From Dev

How do i add a cmd delete option to the right click context menu in window 10

From Dev

How to open kendo window on click

From Dev

How do you add "open in terminal" to the right-click mouse menu for folders/directories?

From Dev

How do you add "open in terminal" to the right-click mouse menu for folders/directories?

From Dev

How to make "open in terminal" in the right click menu use Terminator instead of gnome terminal?

From Dev

In Ubuntu 17.10, how can I add entries to the right click context menu, like "open in terminal"?

From Dev

How do I set right-click on folder background to show "Open PowerShell Window here"?

From Dev

How to disable default right click menu of browser

From Dev

How to add entry to right click menu?

From Dev

How to diagnose right-click menu slowness?

From Dev

(Actionscript 3) How to Hide Right Click Menu?

From Dev

Add "Open MSYS terminal here" in the Windows right click context menu?

From Dev

Open terminal from Nautilus right-click menu

From Dev

Can I open a file as root by just using the right click menu

From Dev

jquery act on right click when user tries to open a new window

From Dev

Selenium Python right click "Open link in new Window" - (Windows 7)

From Dev

Selenium Python right click "Open link in new Window" - (Windows 7)

From Dev

Right click on a subgrid missing the "Open in a New Window" option

Related Related

  1. 1

    System hangs on "Open document" right click context menu (Xubuntu)

  2. 2

    How to add "open with custom command" option in right click menu of Nautilus

  3. 3

    How to add "open with custom command" option in right click menu of Nautilus

  4. 4

    Make GIMP 2.8 open in a maximised window and add an "Edit with GIMP" right-click context menu

  5. 5

    Adding an item to right-click menu on window

  6. 6

    Open in ConEmu right click menu windows 7

  7. 7

    How to open D3.js Context Menu on left click instead of Right click

  8. 8

    How to open D3.js Context Menu on left click instead of Right click

  9. 9

    When the window menu is in the title bar, how can I set the focus click to not open a menu?

  10. 10

    How to actually click a generated right click menu?

  11. 11

    How to react to 'Close window' in right-click menu of task bar in Windows

  12. 12

    How do i add a cmd delete option to the right click context menu in window 10

  13. 13

    How to open kendo window on click

  14. 14

    How do you add "open in terminal" to the right-click mouse menu for folders/directories?

  15. 15

    How do you add "open in terminal" to the right-click mouse menu for folders/directories?

  16. 16

    How to make "open in terminal" in the right click menu use Terminator instead of gnome terminal?

  17. 17

    In Ubuntu 17.10, how can I add entries to the right click context menu, like "open in terminal"?

  18. 18

    How do I set right-click on folder background to show "Open PowerShell Window here"?

  19. 19

    How to disable default right click menu of browser

  20. 20

    How to add entry to right click menu?

  21. 21

    How to diagnose right-click menu slowness?

  22. 22

    (Actionscript 3) How to Hide Right Click Menu?

  23. 23

    Add "Open MSYS terminal here" in the Windows right click context menu?

  24. 24

    Open terminal from Nautilus right-click menu

  25. 25

    Can I open a file as root by just using the right click menu

  26. 26

    jquery act on right click when user tries to open a new window

  27. 27

    Selenium Python right click "Open link in new Window" - (Windows 7)

  28. 28

    Selenium Python right click "Open link in new Window" - (Windows 7)

  29. 29

    Right click on a subgrid missing the "Open in a New Window" option

HotTag

Archive