我创建了一个WPF窗口并将其Window Style属性设置为'None'。 然而,当我按 Alt + Up Key 组合时,窗口左上角会出现一个上下文菜单。
有没有办法禁用它..
注意:处理PreviewKeyDown事件可以完成工作,但我正在寻找不同的方法。
<Window
x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
Title="MainWindow" Height="350" Width="525">
<Grid/>
</Window>
答案 0 :(得分:6)
在代码后面添加以下代码会清除标题样式栏并阻止系统菜单:
private const int GWL_STYLE = -16; //WPF's Message code for Title Bar's Style
private const int WS_SYSMENU = 0x80000; //WPF's Message code for System Menu
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
// Handling the Messages in Window's Loaded event
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
答案 1 :(得分:3)
我认为您必须使用 Alt + Space 来显示系统菜单,而不是 Alt + Up :)
参见本文,它介绍了如何删除WPF的系统菜单: http://winsharp93.wordpress.com/2009/07/21/wpf-hide-the-window-buttons-minimize-restore-and-close-and-the-icon-of-a-window/
答案 2 :(得分:0)
在我的情况下,我想保留图标,但我也登陆这里,所以如果你只想禁用系统菜单激活(并保持图标),你可以挂钩WMProc并压制它。我的解决方案将使事件处理不受影响。
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
KeyUp += MainWindow_KeyUp;
}
protected override void OnKeyUp(KeyEventArgs e)
{
Log("KeyUp overridde: {0}", e.Key);
base.OnKeyUp(e);
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
Log("KeyUp event: {0}", e.Key);
}
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_KEYMENU = 0xF100;
private const int NO_KEYPRESS = 0;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(this.WndProc);
}
private void Log(string msg, params object[] args)
{
Debugger.Log(0, string.Empty, string.Format(msg, args));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle messages...
if (msg == WM_SYSCOMMAND)
{
if (wParam.ToInt32() == SC_KEYMENU)
{
int iLParam = lParam.ToInt32();
Log($"Key: {(char)iLParam} ");
if (lParam.ToInt32() == NO_KEYPRESS)
{
handled = true;
}
}
}
return IntPtr.Zero;
}
}