【问题标题】:WPF Default menuWPF 默认菜单
【发布时间】:2016-02-14 10:48:09
【问题描述】:

我有 WPF 应用程序,右键单击 WPF 应用程序的默认菜单(还原、移动、大小、最小化、最大化、关闭 Alt+F4)

我的问题:我想在此菜单中添加关于选项。 我能做到吗?

我想在现有菜单中使用并将关于添加到此菜单而不是创建新菜单!

谢谢。

之前:

之后

【问题讨论】:

    标签: c# wpf


    【解决方案1】:
    // P/Invoke constants
    private const int WM_SYSCOMMAND = 0x112;
    private const int MF_STRING = 0x0;
    private const int MF_SEPARATOR = 0x800;
    
    // P/Invoke declarations
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem);
    
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool InsertMenu(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, string lpNewItem);
    
    // ID for the About item on the system menu
    private int SYSMENU_ABOUT_ID = 0x1;
    
    public MainWindow()
    {
        InitializeComponent();
    }
    
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        var source = (HwndSource) PresentationSource.FromVisual(this);
        source.AddHook(WndProc);
    
        var helper = new WindowInteropHelper(this);
        // Get a handle to a copy of this form's system (window) menu
        IntPtr hSysMenu = GetSystemMenu(helper.Handle, false);
    
        // Add a separator
        AppendMenu(hSysMenu, MF_SEPARATOR, 0, string.Empty);
    
        // Add the About menu item
        AppendMenu(hSysMenu, MF_STRING, SYSMENU_ABOUT_ID, "&About…");
    }
    
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        // Handle messages...
        // Test if the About item was selected from the system menu
        if ((msg == WM_SYSCOMMAND) && ((int)wParam == SYSMENU_ABOUT_ID))
        {
            MessageBox.Show("Custom About Dialog");
        }
    
        return IntPtr.Zero;
    }
    

    来源:How can I customize the system menu of a Windows Form? 已修改以使其在 WPF 中工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2012-05-06
      • 2019-11-21
      • 2017-01-20
      相关资源
      最近更新 更多