将MenuItem添加到所有现成的WPF标头菜单中

时间:2018-09-17 09:36:33

标签: wpf wpf-controls menuitem application-icon

What at the moment is, What i want

我想将MenuItem添加到单击“应用程序”图标时打开的现有菜单中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

可以使用互操作程序进行Win32调用...首先覆盖Window的SourceInitialized方法并添加以下内容:

public const uint MyMenuItemID = 0x0555;

private const uint MF_BYCOMMAND = 0x00000000;
private const uint MF_BYPOSITION = 0x00000400;

[DllImport ("user32.dll")]
private static extern IntPtr GetSystemMenu (IntPtr hWnd, bool bRevert);

[DllImport ("user32.dll")]
private static extern bool InsertMenu (IntPtr hMenu, uint uPosition, uint uFlags, uint uIDNewItem, string lpNewItem);

private void Window_SourceInitialized (object sender, EventArgs e)
{
    HwndSource source = PresentationSource.FromVisual (this) as HwndSource;
    if (source != null) {
        IntPtr hMenu = GetSystemMenu (source.Handle, false);
        InsertMenu (hMenu, 5, MF_BYPOSITION, MyMenuItemID, "my own MenuItem");
    }
}