使用WindowChrome.WindowChrome禁用上下文菜单

时间:2017-03-14 05:17:42

标签: wpf wpf-controls prism wpftoolkit

如果我使用WindowChrome.WindowChrome样式,我无法禁用ContextMenu。 请让我知道您的输入以解决此问题。 没有WindowChrome,我可以使用相同的代码禁用上下文菜单。

    <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>

背后的代码

        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;
            }
        }

2 个答案:

答案 0 :(得分:0)

使用此代码:

你需要设置windowstyle属性None。

属性: - WindowStyle =“无”

<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;
        }

答案 1 :(得分:0)

也许你并不需要这个,但我解决了它并想把它分享给需要的人。

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