Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customize Window System Menu in WPF

0.00/5 (No votes)
4 Feb 2012 1  
The article describes how to customize default System Menu in WPF
Most of the times, I wondered how System Menu in Native Window has been replaced with custom Menu in WPF. Finally, I found the trick. I just want to share the idea here.

Normally, WPF does not allow direct customization to Non-client area of Window. But that can be done using native methods. We need to hook the WndProc method. WPF has no override method as Winforms does. The following code shows how to hook WndProc();

C#
private void OnLoaded(object sender, RoutedEventArgs e)
        {
            IntPtr windowhandle = new WindowInteropHelper(this).Handle;
            HwndSource hwndSource = HwndSource.FromHwnd(windowhandle);
            hwndSource.AddHook(new HwndSourceHook(WndProc));
        }


C#
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam,    ref bool handled)
        {
            //Code goes here...

            return IntPtr.Zero;
        }


First, we should hide the default System Menu in Window.

To hide the default System Menu in Window, we should handle the particular message. Corresponding window message and parameter for opening System Menu is 0xa4 and 0x02 respectively.

C#
private const uint WM_SYSTEMMENU = 0xa4;
private const uint WP_SYSTEMMENU = 0x02;

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

    //Message for System Menu...

    if ((msg == WM_SYSTEMMENU) && (wParam.ToInt32() == WP_SYSTEMMENU))
    {
        ShowContextMenu();
        handled = true;
    }

    return IntPtr.Zero;
}


ShowContextMenu() method will show our custom Context Menu declared in Window resources.

HTML
<Window.Resources>
    <ContextMenu x:Key="systemMenu">
        <MenuItem Header="Help" InputGestureText="F1">
            <MenuItem.Icon>
                <Image Source="Help.png" Height="16"/>
            </MenuItem.Icon>
        </MenuItem>
        <MenuItem Header="Choose theme">
            <MenuItem.Icon>
                <Image Source="ChooseColor.png" Height="16"/>
            </MenuItem.Icon>
        </MenuItem>
        <MenuItem Header="Add Note">
            <MenuItem.Icon>
                <Image Source="NoteHS.png" Height="16"/>
            </MenuItem.Icon>
        </MenuItem>
        <Separator />
        <MenuItem Header="Exit"/>
    </ContextMenu>
</Window.Resources>


Get the Context Menu in code behind through a property,

C#
public ContextMenu SystemMenu
        {
            get
            {
                return Resources["systemMenu"] as ContextMenu;
            }
        }


Show the ContextMenu in code behind:

C#
private void ShowContextMenu()
        {
            if (SystemMenu != null)
            {
                SystemMenu.IsOpen = true;
            }
        }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here