当 RenderMode 为“系统"时,ToolStripMenuItem 无法显示复选标记和图像(图标)? [英] ToolStripMenuItem can't show checkmark and Image (icon) when RenderMode is "System"?
问题描述
我的 Windows 窗体应用程序有一个 MenuStrip
并且一些菜单项 (ToolStripMenuItem
) 有一个图标(设置 ToolStripMenuItem.Image
属性).
My Windows Forms application has a MenuStrip
and some of the menu items (ToolStripMenuItem
) have an icon (setting the ToolStripMenuItem.Image
property).
当MenuStrip
的RenderMode
属性设置为ToolStripRenderMode.System
时,当Checked
或 CheckOnClick
属性为 true 且菜单项有一个图标.
When the RenderMode
property of the MenuStrip
is set to ToolStripRenderMode.System
, the checkmark doesn't display when the Checked
or CheckOnClick
property is true and the menu item has an icon.
当我将 MenuStrip.RenderMode
属性切换为 ToolStripRenderMode.Professional
或 ToolStripRenderMode.RenderManagerMode
时,它确实显示.
It does display when i switch the MenuStrip.RenderMode
property to ToolStripRenderMode.Professional
or ToolStripRenderMode.RenderManagerMode
.
不幸的是,这是一个问题,因为我的应用需要:
Unfortunately, this is a problem because my app requires:
- 选取框模式下的
ProgressBar
,因此需要Application.EnableVisualStyles()
才能使其正常工作. - 该应用程序需要平面"视觉样式,我通过省略对
Application.EnableVisualStyles()
的调用并保留默认的ToolStripRenderMode.RenderManagerMode
在菜单条.但后来我无法获得我的选取框 ProgressBar! - 将
RenderMode
设置为ToolStripRenderMode.System
解决了外观要求,但取消了使用图标检查菜单项的能力.
- A
ProgressBar
in marquee mode, soApplication.EnableVisualStyles()
is required to get this to work. - The app requires a "flat" visual style, which i accomplished by leaving out the call to
Application.EnableVisualStyles()
and leaving the defaultToolStripRenderMode.RenderManagerMode
on the MenuStrip. But then i can't get my marquee ProgressBar! - Setting the
RenderMode
toToolStripRenderMode.System
solves the look and feel requirement, but takes away the ability to have checked menu items w/icons.
有什么办法可以满足我的所有要求吗?我错过了什么吗?感谢您的关注.
Is there any way to satisfy all my requirements? Am i missing something? Thanks for looking.
推荐答案
哇,我难住了!现在我知道我必须在编写一些严肃的代码.
Wow, i stumped SO! Now i know i must be working on some serious code.
无论如何,答案是:通过创建一个继承自 ToolStripSystemRenderer
的类来实现您自己的 ToolStripRenderer
.
Anyway, the answer is: implement your own ToolStripRenderer
by creating a class that inherits from ToolStripSystemRenderer
.
使用您自己的代码覆盖绘制项目的方法.这是我专门寻找的用于绘制选中项的内容.它会检查 ToolStripMenuItem
是否没有图像.
Override the methods that draw the items with your own code. Here's what i was looking for specifically that draws the checked item. It draws a check if there's no image for the ToolStripMenuItem
.
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
{
base.OnRenderItemCheck(e);
if (e.Item.Selected)
{
Rectangle rect = new Rectangle(3, 1, 20, 20);
Rectangle rect2 = new Rectangle(4, 2, 18, 18);
SolidBrush b = new SolidBrush(Color.FromArgb(49, 106, 197));
SolidBrush b2 = new SolidBrush(Color.Orange);
e.Graphics.FillRectangle(b, rect);
e.Graphics.FillRectangle(b2, rect2);
e.Graphics.DrawImage(e.Image, new Point(5, 3));
}
else
{
Rectangle rect = new Rectangle(3, 1, 20, 20);
Rectangle rect2 = new Rectangle(4, 2, 18, 18);
SolidBrush b = new SolidBrush(Color.FromArgb(49, 106, 197));
SolidBrush b2 = new SolidBrush(Color.Orange);
e.Graphics.FillRectangle(b, rect);
e.Graphics.FillRectangle(b2, rect2);
e.Graphics.DrawImage(e.Image, new Point(5, 3));
}
}
这篇关于当 RenderMode 为“系统"时,ToolStripMenuItem 无法显示复选标记和图像(图标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!