在使用.net toolStrip控件的时候,  toolStrip里面的item宽度超过本身宽度时,会出现一个溢出按钮:OverflowButton,这个按钮是控件的一个属性,其实也是继承自ToolStripDropDownItem,默认样式如下图:

如何才能替换成像chrome书签栏一样的向右小箭头呢?

本以为直接使用toolStrip.OverflowButton.BackgroundImage=Image.FromFile("OverflowArrowVertical.png")就能解决,可是这样好像没有任何效果。

  toolStrip的Renderer属性,能够让用户使用自定义的外观,只需要继承ToolStripRenderer类,重写其中的方法就能定义自己的样式,toolStrip的RenderMode是一个枚举值:

public enum ToolStripRenderMode
{
// 摘要:
// Indicates that the System.Windows.Forms.ToolStrip.RenderMode is not determined
// by the System.Windows.Forms.ToolStripManager or the use of a System.Windows.Forms.ToolStripRenderer
// other than System.Windows.Forms.ToolStripProfessionalRenderer, System.Windows.Forms.ToolStripSystemRenderer
[Browsable(false)]
Custom = ,
//
// 摘要:
// Indicates the use of a System.Windows.Forms.ToolStripSystemRenderer to paint.
System = ,
//
// 摘要:
// Indicates the use of a System.Windows.Forms.ToolStripProfessionalRenderer
// to paint.
Professional = ,
//
// 摘要:
// Indicates that the System.Windows.Forms.ToolStripManager.RenderMode or System.Windows.Forms.ToolStripManager.Renderer
// determines the painting style.
ManagerRenderMode = ,
}

  可以看到,.net本身提供了4种样式,要实现图标替换,只需要继承任意一个样式并重写OnRenderOverflowButtonBackground方法即可。代码如下:

public class DrawOverflowButtonRenderer : System.Windows.Forms.ToolStripProfessionalRenderer
{
Image ofArrowVertical;
public DrawOverflowButtonRenderer(Image pic)
{
ofArrowVertical = pic;
} protected override void OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs e)
{
if (e.ToolStrip.OverflowButton.Enabled)
{
if (e.ToolStrip.OverflowButton.Pressed)
{
LinearGradientBrush lgb = new LinearGradientBrush(e.ToolStrip.OverflowButton.Bounds, ofPressedColor1, ofPressedColor2, ofPressedAngle);
e.Graphics.FillRectangle(lgb, e.Graphics.ClipBounds);
lgb.Dispose(); }
else if (e.ToolStrip.OverflowButton.Selected)
{
LinearGradientBrush lgb = new LinearGradientBrush(e.ToolStrip.OverflowButton.Bounds, ofHighlightColor1, ofHighlightColor2, ofHighlightAngle);
e.Graphics.FillRectangle(lgb, e.Graphics.ClipBounds);
lgb.Dispose();
}
}
e.Graphics.DrawImage(ofArrowVertical, new Rectangle(, e.ToolStrip.OverflowButton.Height - ofArrowVertical.Height, e.ToolStrip.OverflowButton.Bounds.Width, e.ToolStrip.OverflowButton.Bounds.Height));
}
}

  调用:this.toolStrip1.Renderer = new DrawOverflowButtonRenderer(Image.FromFile("OverflowArrowVertical.png"))便能替换图标了,最终效果:

  这个问题纠结了好久,各种谷歌百度,论坛提问都没人解决,平时伸手党惯了,这个控件不行就用其他控件替代,最终还是用英文在google搜了一把,在codeproject上看了老外的一个讨论后得到启示才解决这个问题。感谢万能的谷歌!

如何替换掉.net toolStrip控件溢出按钮背景图的更多相关文章

  1. 把某个asp.net 控件 替换成 自定义的控件

    功能:可以把某个asp.net 控件 替换成 自定义的控件 pages 的 tagMapping 元素(ASP.NET 设置架构) 定义一个标记类型的集合,这些标记类型在编译时重新映射为其他标记类型. ...

  2. ToolStrip控件左右拖拽移动效果实现

    1.主窗体下部添加一个Panel乘放ToolStrip控件以实现ToolStrip在窗体下部定位.2.当ToolStrip控件中子控件超出屏幕时,拖动控件可以实现滑动效果.拖动到控件边缘距窗体边缘1/ ...

  3. 梦想CAD控件COM接口光栅图处理

    在CAD操作过程中,我们在设计绘图时,光栅图像也就是我们常说的图片,应用非常广泛,在CAD中可以直接插入光栅图像,并且可以对光栅图像进行裁剪.透明度调整等一些操作,在网页可以快速实现我们所需功能. 一 ...

  4. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  5. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  6. 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    介绍背水一战 Windows 10 之 控件(按钮类) ButtonBase Button HyperlinkButton RepeatButton ToggleButton AppBarButton ...

  7. Silverlight项目笔记5:Oracle归档模式引起的异常&&表格控件绑定按钮

    1.Oracle归档模式产生日志文件引起数据库异常 连接数据库失败,提示监听错误,各种检查监听配置文件,删除再添加监听,无果. sqlplus下重启数据库数据库依然无果,期间碰到多个错误提示: ORA ...

  8. Duilib 鼠标在某控件例如按钮上悬停后,对目标控件操作

    其实对WM_MOUSEHOVER消息的处理,因为WindowImplBase基类中对此消息未处理,所以在自己的窗口类中实现: .h文件中加入 LRESULT OnMouseHover( UINT uM ...

  9. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

随机推荐

  1. 起因:dell超级密码算号器 1F66

    班里有几个比较调皮的同学~(显然不包括我[开玩笑的]) 当初班长设置了密码,结果那几个玩拳皇的想玩,就找我删除了密码~ 最后有个大神就过来设置了BIOS.有种终于出动了的样子.有种我看你怎么办的样子. ...

  2. SQL2005中的事务与锁定(四)- 转载

    ------------------------------------------------------------------------ -- Author : HappyFlyStone - ...

  3. 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类

    import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...

  4. MMAP和DIRECT IO区别【转】

    转自:http://www.cnblogs.com/zhaoyl/p/5901680.html 看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-2710571 ...

  5. ViewPager With FragmentPagerAdapter

    采用PagerAdapter中的FragmentPagerAdapter来实现页面切换,适用于a handful of typically more static fragments to be pa ...

  6. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  7. Unity-The Courtyard demo学习

    1.编辑器相机同步 The Courtyard的demo中,会发现Scene视图和Game视图是编辑器下同步的,它通过一个CopySceneView.cs脚本实现 Scene视图和Game视图的显示效 ...

  8. Unix网络编程--卷二:FAQ

    1.编译unpipc库. 执行./configure时报错: checking host system type... Invalid configuration `x86_64-pc-linux-g ...

  9. Web Performance Test: 如果使用Plugin过滤Dependent Request

    前言 由于Visual Studio的Web Performance Test是基于XML脚本的,留给用户修改测试行为的自由度并不高.因此,Plugin机制就对于实现很多客户化的配置显得很重要. 问题 ...

  10. printf("%*s%s%*s",——)是什么?

    我们可能知道scanf里用*修饰符,是起到过滤读入的作用.比如一个有三列数值的数据,我只想得到第2列数值,可以在循环里用scanf(“%*d%d%*d”, a[i])来读入第i行的第2个数值到a[i] ...