在 C# 中更改 ContextMenu 字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/96525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:40:30  来源:igfitidea点击:

Change ContextMenu Font Size in C#

提问by Ray Li

Is it possible to change the font size used in a ContextMenu using the .NET Framework 3.5 and C# for a desktop application? It seems it's a system-wide setting, but I would like to change it only within my application.

是否可以使用 .NET Framework 3.5 和 C# 为桌面应用程序更改 ContextMenu 中使用的字体大小?似乎这是一个系统范围的设置,但我只想在我的应用程序中更改它。

采纳答案by OwenP

If you are defining your own context menu via a ContextMenuStripin Windows Forms, use the Fontproperty.

如果您通过ContextMenuStripWindows 窗体中的a 定义自己的上下文菜单,请使用Font属性。

If you are defining your own context menu via a ContextMenuin WPF, use the various Fontxxxproperties such as FontFamilyand FontSize.

如果您通过ContextMenuWPF 中的a 定义自己的上下文菜单,请使用各种Fontxxx属性,例如FontFamilyFontSize

You cannot change the default context menus that come with controls; those are determined by system settings. So if you want the "Copy/Cut/Paste/etc." menu with a custom font size for a WinForms TextBox, you'll have to create a ContextMenuStripwith the appropriate font size and assign it to the TextBox's ContextMenuStripproperty.

您无法更改控件附带的默认上下文菜单;这些是由系统设置决定的。因此,如果您想要“复制/剪切/粘贴/等”。具有自定义字体大小的 WinForms 菜单TextBox,您必须创建ContextMenuStrip具有适当字体大小TextBox菜单并将其分配给的ContextMenuStrip属性。

回答by Hallgrim

You can change the font size of a System.Windows.Forms.ContextMenuStrip.

您可以更改 System.Windows.Forms.ContextMenuStrip 的字体大小。

If you need to change the font size of the default Cut/Copy/Paste context menu on text boxes I guess you need to set the ContextMenu property to a custom menu that replaces the default menu.

如果您需要更改文本框上默认剪切/复制/粘贴上下文菜单的字体大小,我猜您需要将 ContextMenu 属性设置为替换默认菜单的自定义菜单。

回答by Isak Savo

You mention .NET 3.5 - are you writing in WPF? If so, you can specify font size for the TextBlock.FontSize attached property

你提到 .NET 3.5 - 你是用 WPF 编写的吗?如果是这样,您可以为 TextBlock.FontSize 附加属性指定字体大小

<Whatever.ContextMenu TextBlock.FontSize="12">
  <MenuItem ... /> <!-- Will get the font size from parent -->
</Whatever.ContextMenu>

Or, you could specify it in a style that affects all menu items

或者,您可以以影响所有菜单项的样式指定它

<Style TargetType="MenuItem">
   <Setter Property="TextBlock.FontSize" Value="12" />
</Style>

Of course, it's always better to let the system setting determine the font size. Some people may have changed it to better fit their physical condition (like poor eye sight) or hardware (big/small screen). Whatever you force in your code will be the wrong choice for some people, while you give them no way to change it.

当然,让系统设置决定字体大小总是更好。有些人可能已经改变它以更好地适应他们的身体状况(如视力不佳)或硬件(大/小屏幕)。无论你在代码中强加什么,对某些人来说都是错误的选择,而你却没有办法改变它。

回答by Ben Straub

In WPF:

在 WPF 中:

<Window.ContextMenu FontSize="36">
    <!-- ... -->
</Window.ContextMenu

In WinForms:

在 WinForms 中:

contextMenuStrip1.Font = new System.Drawing.Font("Segoe UI", 24F);