简体   繁体   English

如何在我的左菜单栏上绘制箭头?

[英]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: 因此,这里有两个相互关联的问题:

  1. Is there a way to force the arrows to be drawn on top-level menu items? 有没有一种方法可以强制在顶级菜单项上绘制箭头?
  2. If not, is there a way to determine the proper location of the arrow on the menu item so that 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.

相关问题 如何通过使用vc#中的代码从左向右移动TabControl或TabStrip和MenuStrip以及SubMenuStrip之类的控件? - How I can move controls such as TabControl or MenuStrip ans SubMenuStrip from left to right by using code in vc#? 如何创建具有多个内容的菜单栏? - How can I create a menustrip with multi content? 如何在菜单栏中调用图像并将其隐藏? - How can I call an image in menustrip and hide it? 如何更改菜单条的保护级别,以便我可以以其他形式编写不是菜单条的代码 - How to change protection level of menustrip so I can code in other form where menustrip isn't it 如何删除 MDI 菜单条中的最大化、最小化和关闭图标 - How can i Remove Maximize,Minimize and Close icon in MDI Menustrip 如何为listBox中的每个项目添加menuStrip菜单? - How can i add a menuStrip menus for each item in a listBox? 如何删除MenuStrip控件下绘制的白线? - How can I remove the white line drawn under a MenuStrip control? 如何在单击时更改选定的 MenuStrip 按钮颜色? - How can I change the selected MenuStrip button color on click? 如何将子窗体类中的菜单条中的其他项合并到其基类中的菜单条中的原始项? (WInforms) - How can I merge additional items in a menustrip in a child form class to the original items in the menustrip in its base class? (WInforms) 当我单击此菜单时,如何在菜单条上添加V或其他显示给我的东西? - How can i add a V or something on a MenuStrip that show me when i clicked on this menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM