【问题标题】:Remove Title Bar's Context Menu completely in WPF Window在 WPF 窗口中完全删除标题栏的上下文菜单
【发布时间】:2014-01-19 20:25:18
【问题描述】:

我创建了一个 WPF 窗口并将其窗口样式属性设置为“无”。 然而,当我按下 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>

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

在后面的代码中添加以下代码会清除标题栏的样式并阻止系统菜单:

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

取自: How to hide close button in WPF window?

【讨论】:

    【解决方案2】:

    我认为你必须使用 Alt + Space 来显示系统菜单,而不是 Alt + Up :)。

    参见this article,它描述了如何删除 WPF 的系统菜单。

    【讨论】:

    • 你是对的 Jerry Alt+Space 显示菜单.. Alt 然后向上键也显示上下文菜单..可能是因为 Alt 将焦点设置为不可见的标题栏。
    【解决方案3】:

    在我的情况下,我想保留图标,但我也登陆了这里,所以如果你只想禁用系统菜单激活(并保留图标),你可以挂钩 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 2021-12-14
      • 2016-10-26
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多