Reputation: 139
I have created a WPF Window and set its Window Style property to 'None'. Yet when I press Alt+Up Key combination, a context menu appears on the top left of the Window.
Is there a way to disable that too..
Note: Handling the PreviewKeyDown events does the job but I am looking for a different way.
<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>
Upvotes: 2
Views: 5785
Reputation: 4228
I think you have to use Alt + Space to show the system menu, not Alt + Up :).
See this article, it describes how to remove the system menu of WPF.
Upvotes: 3
Reputation: 1443
in my case i wanted to keep the icon, but i also landed here, so if you only want to disable the system menu activation (and keep the icon) you could hook the WMProc and supress that. my solution will let the eventhandling untouched.
/// <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;
}
}
Upvotes: 0
Reputation: 139
Adding the following code in code behind clears the Style of Title bar and blocks the System Menu:
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);
}
Taken From : How to hide close button in WPF window?
Upvotes: 7