wpfwpf-controlsmenuitemapplication-icon

Adding MenuItem to the allready existing WPF header Menu


What at the moment is, What i want

I want to add an MenuItem to the existing Menu that opens when you click on the Application Icon.

Thanks for the help.


Solution

  • It can be done using interops to go to Win32 calls... First override the SourceInitialized method for the Window and add the following:

    public const uint MyMenuItemID = 0x0555;
    
    private const uint MF_BYCOMMAND = 0x00000000;
    private const uint MF_BYPOSITION = 0x00000400;
    
    [DllImport ("user32.dll")]
    private static extern IntPtr GetSystemMenu (IntPtr hWnd, bool bRevert);
    
    [DllImport ("user32.dll")]
    private static extern bool InsertMenu (IntPtr hMenu, uint uPosition, uint uFlags, uint uIDNewItem, string lpNewItem);
    
    private void Window_SourceInitialized (object sender, EventArgs e)
    {
        HwndSource source = PresentationSource.FromVisual (this) as HwndSource;
        if (source != null) {
            IntPtr hMenu = GetSystemMenu (source.Handle, false);
            InsertMenu (hMenu, 5, MF_BYPOSITION, MyMenuItemID, "my own MenuItem");
        }
    }