Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf WindowChrome-如何修改或禁用标题栏中的上下文菜单?_Wpf_Contextmenu - Fatal编程技术网

Wpf WindowChrome-如何修改或禁用标题栏中的上下文菜单?

Wpf WindowChrome-如何修改或禁用标题栏中的上下文菜单?,wpf,contextmenu,Wpf,Contextmenu,我需要禁用使用WindowChrome的WPF窗口标题栏中的上下文菜单: <Window x:Class="WpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" He

我需要禁用使用WindowChrome的WPF窗口标题栏中的上下文菜单:

<Window x:Class="WpfApplication.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"
        ContextMenu="{x:Null}">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="35"
                      CornerRadius="0"
                      ResizeBorderThickness="5"
                      UseAeroCaptionButtons="False" />
    </WindowChrome.WindowChrome>
</Window>

ContextMenu=“{x:Null}”不起作用。 以下说明不起作用: 标题栏中的关联菜单始终不作任何更改。
有人有主意吗

虽然这可能回答了这个问题,但当您解释代码的作用时会更好,因为它可能不是对所有读者都显而易见的。此外,您还应该重新查看代码的格式化输出,并更正第一行。虽然这可能会回答问题,但最好解释一下代码的功能,因为并非所有读者都清楚这一点。此外,您应该再次查看代码的格式化输出,并更正第一行。
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

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

    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)
    {
        if ((msg == WM_SYSTEMMENU) && (wParam.ToInt32() == WP_SYSTEMMENU))
        {
            handled = true;
        }

        return IntPtr.Zero;
    }
}