如何右对齐菜单中的快捷键?
我添加了菜单项快捷键,但它们的排列很差。
我需要将其设置为这样:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我添加了菜单项快捷键,但它们的排列很差。
我需要将其设置为这样:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
啊,我想我终于弄清楚了如何做到这一点(很长一段时间以来,我一直困惑你是如何获得这两个不同的屏幕截图的......)。事实证明,您可以通过两种方法指示 Windows 对齐下拉菜单中的快捷键。
第一种(可能也是最标准的)方法是在与菜单文本对应的字符串中插入制表符(
\t
)。这会产生原始问题中显示的底部示例,其中所有标识符都是左对齐的,并且有些稍微悬垂。这是几乎所有 Microsoft 应用程序的标准,也是我直到几分钟前才知道存在的唯一选项。&Print…\tCtrl+P
第二种方法是将
\t
转义序列替换为资源字符串中的\a
(是的,奇怪的是,它通常表示 警报铃)。这会导致 Windows 右对齐菜单中的所有快捷键序列,从而生成第一个屏幕截图中所示的示例。这确实产生了一个更有效地使用屏幕空间的菜单(它更小),但这是以每个快捷键序列沿着其左侧边距整齐对齐为代价的,我认为这使得可读性稍微容易一些。&Print…\aCtrl+P
因此,如果您希望菜单看起来像第二个 在你原来的问题中的例子(是的,我混淆了我的样本向后排列。抱歉),你需要在包含你的资源文件中用制表符(
\t
)来分隔快捷键序列菜单项文本字符串。奇怪的是,您声称正在使用 .NET WinForms,它会自动处理所有这些(使您免于弄乱资源文件的痛苦)。我知道它会插入制表符,并且我见过它生成的所有菜单都看起来像您的第二个示例。
最好的办法是将菜单切换到 .NET Framework 早期版本中包含的旧
MainMenu
控件。 (为此,您可能必须右键单击工具箱并手动添加控件 - 在 Visual Studio 的更高版本中默认情况下不存在该控件。)这将确保您看到所需的对齐行为,与记事本等所有标准 Windows 应用程序一致。它还生成看起来像本机操作系统菜单的菜单(在 Vista 和 7 中,它们被涂成蓝色),而不是完全超越由MenuStrip
控件生成的看起来业余的自制菜单。在现代版本的 Windows 中很常见。Microsoft 的官方文档确实如此 确认
MainMenu
control 仍受支持以供将来使用,因此无需担心在您的应用中使用它。我强烈建议大家使用这个:Ah, I think I finally figured out how to do this (it had been puzzling me for quite some time how you'd gotten the two different screenshots...). It turns out there are two ways that you can instruct Windows to align shortcut keys in a drop-down menu.
The first (and probably most standard) way is to insert a tab character (
\t
) in the string corresponding to the menu text. This produces the bottom example shown in your original question, the one where all the identifiers are left-aligned and some overhang slightly. This is the standard in almost all Microsoft applications, and the only option that I knew existed until a few minutes ago.&Print…\tCtrl+P
The second way is to replace that
\t
escape sequence with\a
in the resource string (which, yes, strangely enough would normally indicate an alert bell). This causes Windows to right align all of the shortcut key sequences in the menu, producing the example illustrated in your first screenshot. This does produce a menu that uses screen space more efficiently (it's smaller), but this comes at the cost of a neat alignment of each of the shortcut key sequences down their left-hand margins, which I think makes for slightly easier readability.&Print…\aCtrl+P
So if you want your menus to look like the second example in your original question (Yes, I've confusingly arranged my samples backwards. Sorry), you need to delimit the shortcut key sequence with a tab character (
\t
) in the resource file containing your menu item text strings.The strange thing is that you claim to be using .NET WinForms, which handles all of this automatically (saving you from the pain of messing with resource files). I know for a fact that it inserts tab characters, and all of the menus I've ever seen it generate do look like your second example.
The best thing to do is switch your menu to the old
MainMenu
control included with earlier version of the .NET Framework. (To do this, you'll probably have to right-click on the Toolbox and add the control manually—it isn't there by default on later versions of Visual Studio.) This will ensure that you see the alignment behavior you expect, consistent with all of the standard Windows applications like Notepad. It also produces menus that look like the native operating system menus (in Vista and 7, they are painted blue) rather than the amateur-looking owner-drawn menus produced by theMenuStrip
control that are completely out-of-place in modern versions of Windows.Microsoft's official documentation does confirm that the
MainMenu
control is still supported for future use, so there's no reason to fear using it in your apps. I highly recommend everyone use this instead:正如您所看到的,它们是“右”对齐的。 (没有双关语的意思)。
这是菜单的默认行为。您需要自定义绘图才能以不同的方式进行操作。
They are 'right' aligned, as you can see. (No pun intended).
This is default behaviour for a menu. You'll need custom drawing to do it differently.
每个操作系统都有其特定的外观和感觉,我想您一定有充分的理由不尊重桌面上其他应用程序的外观。我想您要么放弃这个问题,要么用
OwnerDrawn
项目扩展菜单。这里是关于该主题的过度杀戮文章。
Every OS has its particular look and feel, and I guess that you have to have pretty good reasons not to honor how every other application on the desktop looks. I guess you will either drop the issue, or will extend the menu with
OwnerDrawn
items.Here is the overkill article on the subject.