2

有什么办法可以隐藏 a 上的箭头ToolStripMenuItem吗?箭头包含在红色方块中。

截屏

4

1 回答 1

7

我发现这很有帮助,您可以创建自己的自定义 ToolStripRenderer 继承自ToolStripProfessionalRenderer,如下所示:

public class CustomToolStripRenderer : ToolStripProfessionalRenderer
{       
    protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
    {            
        e.ArrowRectangle = Rectangle.Empty;//Don't draw arrow
        base.OnRenderArrow(e);
    }
}
//and update the Renderer property of your MenuStrip
menuStrip1.Renderer = new CustomToolStripRenderer();

更新

根据您的要求,有一些方法可以做,但我认为这是一个好方法:

public class CustomToolStripRenderer : ToolStripProfessionalRenderer
{       
    protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
    {                                    
        if (RenderArrow != null) RenderArrow(this, e);
        base.OnRenderArrow(e);            
    }
    public new event ToolStripArrowRenderEventHandler RenderArrow;//This will hide the default RenderArrow event which can't help you change the e argument because the default is fired after the Arrow is rendered.
}
//Now you have to create your own List<ToolStripItem> to contain all the items whose arrows should not be rendered
List<ToolStripItem> ItemsWithoutArrow = new List<ToolStripItem>();
//Add a method to add an item to that list
private void SuppressDrawArrow(ToolStripItem item)
{
   if (!ItemsWithoutArrow.Contains(item)) ItemsWithoutArrow.Add(item);
}
//Assign custom ToolStripRenderer for your MenuStrip
menuStrip1.Renderer = new CustomToolStripRenderer();
//Now add a RenderArrow event handler, this RenderArrow event is the new we created in the class CustomToolStripRenderer
((CustomToolStripRenderer)menuStrip1.Renderer).RenderArrow += (s, e) =>
 {                
    if(ItemsWithoutArrow.Contains(e.Item)) e.ArrowRectangle = Rectangle.Empty;
 };
//Add some item to the ItemsWithoutArrow to test
SuppressDrawArrow(item1ToolStripMenuItem);

另一个解决方案(我喜欢一个问题的许多解决方案:)

public class CustomToolStripRenderer : ToolStripProfessionalRenderer
{       
    protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
    {                                    
       if(!itemsWithoutArrow.Contains(e.Item)) base.OnRenderArrow(e);            
    }
    public void SuppressDrawArrow(ToolStripItem item){
       if (!itemsWithoutArrow.Contains(item)) itemsWithoutArrow.Add(item);
    }
    public void AllowDrawArrow(ToolStripItem item){
       itemsWithoutArrow.Remove(item);
    }
    private List<ToolStripItem> itemsWithoutArrow = new List<ToolStripItem>();
}
//Use in code
CustomToolStripRenderer renderer = new CustomToolStripRenderer();
renderer.SuppressDrawArrow(item1ToolStripMenuItem);
menuStrip1.Renderer = renderer;    
//This solution fits your requirement (draw or don't draw arrow) but if you also want to change such as ArrowColor, the previous solution would be better.

我发现我们可以用许多选项自由地渲染它。那太棒了 :)

于 2013-06-18T15:23:22.543 回答