Click here to Skip to main content
13,551,173 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 28 Jan 2012
Licenced CPOL

Customize Window System Menu in WPF

, 4 Feb 2012
Rate this:
Please Sign up or sign in to vote.
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();

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

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.

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))
        handled = true;

    return IntPtr.Zero;

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

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

Get the Context Menu in code behind through a property,

public ContextMenu SystemMenu
                return Resources["systemMenu"] as ContextMenu;

Show the ContextMenu in code behind:

private void ShowContextMenu()
            if (SystemMenu != null)
                SystemMenu.IsOpen = true;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jawahar Suresh Babu
Software Developer (Senior)
India India
Jawahar working as a Senior Development Engineer in Aditi Technologies,Bangalore, India. Specialist in all XAML frameworks. Very passionate on UX Design and Development. Skilled in Expression Blend, Design, WPF, Silverlight, Windows Phone 7/8, Windows 8. Good knowledge in Entity Framework, SQLite and SQL Server also. Also had good experience with PRISM, MVVM, Caliiburn Micro and other design patterns.

He developed few products for Syncfusion Inc. Also working on some freelancing projects. Worked as a lead developer of Metro Studio from Syncfusion Inc.

An active freelancer.

You may also be interested in...


Comments and Discussions

QuestionI have tried the code, but it didn't work for me. Pin
Member 94257714-Mar-14 6:01
memberMember 94257714-Mar-14 6:01 
GeneralMy vote of 2 Pin
Member 39979865-Jan-13 15:59
memberMember 39979865-Jan-13 15:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by Jawahar Suresh Babu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid