C# 如何自定义Windows窗体的系统菜单?
我想将“关于”菜单项添加到我的应用程序中。我想把它添加到应用程序的“系统菜单”中,当我们点击左上角的应用程序图标时,它会弹出。那么,我怎样才能在.NET中做到这一点呢?对于您所需要的pinvoke数量来说,附加值相当小。但这是可能的。使用GetSystemMenu检索系统菜单句柄。然后插入菜单项以添加条目。必须在OnHandleCreated的重写中执行此操作,以便在重新创建窗口时重新创建菜单C# 如何自定义Windows窗体的系统菜单?,c#,.net,winforms,winapi,systemmenu,C#,.net,Winforms,Winapi,Systemmenu,我想将“关于”菜单项添加到我的应用程序中。我想把它添加到应用程序的“系统菜单”中,当我们点击左上角的应用程序图标时,它会弹出。那么,我怎样才能在.NET中做到这一点呢?对于您所需要的pinvoke数量来说,附加值相当小。但这是可能的。使用GetSystemMenu检索系统菜单句柄。然后插入菜单项以添加条目。必须在OnHandleCreated的重写中执行此操作,以便在重新创建窗口时重新创建菜单 重写WndProc以识别用户单击时生成的WM_SYSCOMMAND消息。请访问pinvoke.net以
重写WndProc以识别用户单击时生成的WM_SYSCOMMAND消息。请访问pinvoke.net以获取所需的pinvoke声明。Windows可以非常轻松地获取表单系统菜单副本的句柄,以便使用进行自定义。困难的部分是,您需要自己对它返回的菜单执行适当的修改,使用函数,如,并且就像您直接针对Win32 API编程一样 然而,如果你想做的只是添加一个简单的菜单项,其实并不难。例如,您只需要使用AppendMenu函数,因为您只需要在菜单的末尾添加一两个项目。做任何更高级的事情,比如在菜单中间插入一个项目,在菜单项上显示位图,显示选中的菜单项,设置默认菜单项等等。但一旦你知道怎么做,你就可以发疯了。这件事说明了一切 下面是一个表单的完整代码,该表单在其系统菜单(也称为窗口菜单)的底部添加分隔线和“关于”项:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class CustomForm : Form
{
// 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 CustomForm()
{
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// Get a handle to a copy of this form's system (window) menu
IntPtr hSysMenu = GetSystemMenu(this.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…");
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// Test if the About item was selected from the system menu
if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SYSMENU_ABOUT_ID))
{
MessageBox.Show("Custom About Dialog");
}
}
}
以下是成品的外观:
我进一步采用了Cody Gray的解决方案,并从中创建了一个可重用的类。它是我的应用程序日志提交工具的一部分,应该在系统菜单中隐藏其关于信息 它可以很容易地像这样使用:
class MainForm : Form
{
private SystemMenu systemMenu;
public MainForm()
{
InitializeComponent();
// Create instance and connect it with the Form
systemMenu = new SystemMenu(this);
// Define commands and handler methods
// (Deferred until HandleCreated if it's too early)
// IDs are counted internally, separator is optional
systemMenu.AddCommand("&About…", OnSysMenuAbout, true);
}
protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg);
// Let it know all messages so it can handle WM_SYSCOMMAND
// (This method is inlined)
systemMenu.HandleMessage(ref msg);
}
// Handle menu command click
private void OnSysMenuAbout()
{
MessageBox.Show("My about message");
}
}
我知道这个答案很古老,但我真的很喜欢LonelyPixel的答案。然而,它需要一些工作来正确使用WPF。以下是我编写的WPF版本,因此您不必:
/// <summary>
/// Extends the system menu of a window with additional commands.
/// Adapted from:
/// https://github.com/dg9ngf/FieldLog/blob/master/LogSubmit/Unclassified/UI/SystemMenu.cs
/// </summary>
public class SystemMenuExtension
{
#region Native methods
private const int WM_SYSCOMMAND = 0x112;
private const int MF_STRING = 0x0;
private const int MF_SEPARATOR = 0x800;
[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);
#endregion Native methods
#region Private data
private Window window;
private IntPtr hSysMenu;
private int lastId = 0;
private List<Action> actions = new List<Action>();
private List<CommandInfo> pendingCommands;
#endregion Private data
#region Constructors
/// <summary>
/// Initialises a new instance of the <see cref="SystemMenu"/> class for the specified
/// <see cref="Form"/>.
/// </summary>
/// <param name="window">The window for which the system menu is expanded.</param>
public SystemMenuExtension(Window window)
{
this.window = window;
if(this.window.IsLoaded)
{
WindowLoaded(null, null);
}
else
{
this.window.Loaded += WindowLoaded;
}
}
#endregion Constructors
#region Public methods
/// <summary>
/// Adds a command to the system menu.
/// </summary>
/// <param name="text">The displayed command text.</param>
/// <param name="action">The action that is executed when the user clicks on the command.</param>
/// <param name="separatorBeforeCommand">Indicates whether a separator is inserted before the command.</param>
public void AddCommand(string text, Action action, bool separatorBeforeCommand)
{
int id = ++this.lastId;
if (!this.window.IsLoaded)
{
// The window is not yet created, queue the command for later addition
if (this.pendingCommands == null)
{
this.pendingCommands = new List<CommandInfo>();
}
this.pendingCommands.Add(new CommandInfo
{
Id = id,
Text = text,
Action = action,
Separator = separatorBeforeCommand
});
}
else
{
// The form is created, add the command now
if (separatorBeforeCommand)
{
AppendMenu(this.hSysMenu, MF_SEPARATOR, 0, "");
}
AppendMenu(this.hSysMenu, MF_STRING, id, text);
}
this.actions.Add(action);
}
#endregion Public methods
#region Private methods
private void WindowLoaded(object sender, RoutedEventArgs e)
{
var interop = new WindowInteropHelper(this.window);
HwndSource source = PresentationSource.FromVisual(this.window) as HwndSource;
source.AddHook(WndProc);
this.hSysMenu = GetSystemMenu(interop.EnsureHandle(), false);
// Add all queued commands now
if (this.pendingCommands != null)
{
foreach (CommandInfo command in this.pendingCommands)
{
if (command.Separator)
{
AppendMenu(this.hSysMenu, MF_SEPARATOR, 0, "");
}
AppendMenu(this.hSysMenu, MF_STRING, command.Id, command.Text);
}
this.pendingCommands = null;
}
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SYSCOMMAND)
{
if ((long)wParam > 0 && (long)wParam <= lastId)
{
this.actions[(int)wParam - 1]();
}
}
return IntPtr.Zero;
}
#endregion Private methods
#region Classes
private class CommandInfo
{
public int Id { get; set; }
public string Text { get; set; }
public Action Action { get; set; }
public bool Separator { get; set; }
}
#endregion Classes
接受答案的VB.NET版本:
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class CustomForm
Inherits Form
' P/Invoke constants
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const MF_STRING As Integer = &H0
Private Const MF_SEPARATOR As Integer = &H800
' P/Invoke declarations
<DllImport("user32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function GetSystemMenu(hWnd As IntPtr, bRevert As Boolean) As IntPtr
End Function
<DllImport("user32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function AppendMenu(hMenu As IntPtr, uFlags As Integer, uIDNewItem As Integer, lpNewItem As String) As Boolean
End Function
<DllImport("user32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function InsertMenu(hMenu As IntPtr, uPosition As Integer, uFlags As Integer, uIDNewItem As Integer, lpNewItem As String) As Boolean
End Function
' ID for the About item on the system menu
Private SYSMENU_ABOUT_ID As Integer = &H1
Public Sub New()
End Sub
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
' Get a handle to a copy of this form's system (window) menu
Dim hSysMenu As IntPtr = GetSystemMenu(Me.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…")
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
' Test if the About item was selected from the system menu
If (m.Msg = WM_SYSCOMMAND) AndAlso (CInt(m.WParam) = SYSMENU_ABOUT_ID) Then
MessageBox.Show("Custom About Dialog")
End If
End Sub
End Class
这很有效。但是,如何显示About条目的加速键?就像Close有Alt+F4一样,我想显示Alt+A大约。不过,我找到了我评论的答案。您只需添加类似&About…\t+A的字符串,并使用制表符\t分隔。酷!,但是我可以在多项目库、dll中重复使用这个吗?@123iamking您是在询问代码的许可证吗?它与堆栈溢出上的所有内容一样。这意味着您可以自由使用它,并根据需要修改它,只要您提供属性,即包括我的名字。源代码中的注释就足够了。如果您询问的是实现问题,当然可以:您可以将自定义表单类打包到DLL或LIB或任何东西中,然后从中继承所有表单。@Yoda,现在轮到我为迟到道歉了…:-要向菜单项添加图标,请设置MENUITEMINFO的hbmpItem字段。在Vista和更高版本中,您可以指定PARGB32位图以获得一个漂亮的alpha混合图标。将来,当你有一个新问题时,请提出一个新问题,而不是将其作为评论发布。你会得到一个更好的答案。我想试试你的技巧,但我找不到SystemMenu的参考资料。它在哪里?@John它是链接处提供上述函数并处理P/Invoke的类。@John Oops,我是否错过了那条评论……我还更新了链接,使其更适合未来。我必须通知您,您的代码GitHub存储库的修改版本已被用作示例,而不是答案本身的一部分。您的代码已被修改,添加了允许在系统菜单中插入位图的操作。但是,如果你不同意,我会删除它。让我知道你的想法。@Jimi,没关系!我在我的本地参考文件副本中注意到了一个指向你文章的链接,这样我就知道有一天我需要图标支持时应该去哪里查看了-