如果使用ContextMenu样式,则无法禁用WindowChrome.WindowChrome。请让我知道您的意见,以解决这个问题。没有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;
}
}发布于 2017-03-14 05:37:59
使用此代码:
您需要设置窗口样式属性None。
属性:- 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">或
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;
}发布于 2017-08-22 06:40:27
也许你不需要这个,但我解决了它,想把它分享给需要它的人。
if (((msg == WM_SYSTEMMENU) && (wParam.ToInt32() == WP_SYSTEMMENU)) || msg == 165){
//ShowContextMenu();
handled = true;
}https://stackoverflow.com/questions/42778240
复制相似问题