Search code examples
wpfwpf-controlsprismwpftoolkit

Disable Context Menu with WindowChrome.WindowChrome


I am unable to disable ContextMenu, if I use WindowChrome.WindowChrome style. Please let me know your input to resolve this problem. Without WindowChrome i am able to disable context menu with same piece of code.

    <Window x:Class="ConextMenu_Sample.MainWindow"                
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <WindowChrome.WindowChrome>
            <WindowChrome CaptionHeight="35"
                          CornerRadius="0"
                          ResizeBorderThickness="5"
                          UseAeroCaptionButtons="False" />
        </WindowChrome.WindowChrome>
        <Grid>

        </Grid>
    </Window>

Code behind

        public partial class MainWindow : Window
        {
            private const uint WP_SYSTEMMENU = 0x02;
            private const uint WM_SYSTEMMENU = 0xa4;
            public MainWindow()
            {
                InitializeComponent();
                Loaded += OnLoaded;
            }

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

            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
            {
                if ((msg == WM_SYSTEMMENU) && (wparam.ToInt32() == WP_SYSTEMMENU))
                {
                    handled = true;
                }
                return IntPtr.Zero;
            }
        }

Solution

  • Use this Code:

    U need to set the windowstyle property None.

    Property:- WindowStyle = "None"

    <Window x:Class="ConextMenu_Sample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" WindowStyle="None">
    

    [OR]

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
            {
                if ((msg == WM_SYSTEMMENU) && (wparam.ToInt32() == WP_SYSTEMMENU))
                {
                    handled = false;  // Change the Boolean Value to false // 
                }
                return IntPtr.Zero;
            }