[英]How can I draw arrows on my left-docked MenuStrip?
I have a C# form into which I've placed a left-docked MenuStrip
. 我有一个C#表单,在其中放置了一个左对接的MenuStrip
。 This MenuStrip
contains some menu items which contain submenus, and some menu items which are effectively buttons (clicking on them results in an action taking place; nb, I realize this is not a good design). 此MenuStrip
包含一些包含子菜单的菜单项,以及一些实际上是按钮的菜单项(单击它们会导致发生动作;请注意,我意识到这不是一个好的设计)。
I would like to have the menu items which have menus associated with them draw the right-pointing arrow on the menu item, the same way a contextual menu does. 我想让具有与之关联的菜单的菜单项在该菜单项上绘制向右箭头,就像上下文菜单所做的一样。 I've subclassed ToolStripProfessionalRenderer
and can call OnRenderArrow()
at the appropriate time (eg, within OnRenderItemText()
or similar), but I don't seem to have a way to determine the correct location of the arrow. 我已经将ToolStripProfessionalRenderer
子类化,可以在适当的时候(例如,在OnRenderItemText()
或类似的时间OnRenderArrow()
调用OnRenderArrow()
,但是我似乎没有办法确定箭头的正确位置。
So, two interrelated questions here: 因此,这里有两个相互关联的问题:
OnRenderArrow()
can be called manually? 如果不是,是否有办法确定菜单项上箭头的正确位置,以便可以手动调用OnRenderArrow()
? Thanks! 谢谢!
Why do you not look into using a System.Windows.Forms.ToolStrip rather than a MenuStrip. 为什么不考虑使用System.Windows.Forms.ToolStrip而不是MenuStrip。 This will allow you to have the arrow functionality build in and will even solve the bad desing problem you are having. 这将使您具有内置的箭头功能,甚至可以解决您遇到的不良设计问题。
Should you want you can specify that the toolstrip items do not show images and only show text. 如果需要,可以指定该工具栏项不显示图像,而仅显示文本。 In this way you can almost exactly mimic the functionality of the menustrip, but get the "drop down arrows" free. 这样,您几乎可以完全模仿菜单条的功能,但可以免费获得“下拉箭头”。
I was able to hack this together as a solution, but I'd still like something less obtuse: 我可以将其作为一种解决方案,但是我仍然想要一些不太钝的东西:
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
base.OnRenderItemText(e);
if (e.Item.GetType() == typeof(ToolStripMenuItem))
{
ToolStripMenuItem tsmi = (ToolStripMenuItem)e.Item;
if (tsmi.HasDropDownItems && tsmi.OwnerItem == null)
{
Rectangle bounds = tsmi.Bounds;
bounds.X = bounds.Right - 25;
bounds.Width = 25;
bounds.Y = 0;
ToolStripArrowRenderEventArgs tsarea = new ToolStripArrowRenderEventArgs(
e.Graphics,
e.Item,
bounds,
e.TextColor,
ArrowDirection.Right);
OnRenderArrow(tsarea);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.