在使用.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. drop delete truncate 区别

    http://jingyan.baidu.com/article/8275fc8693e11846a03cf696.html

  2. AJAX-----04XMLHttpRequest对象的ajax

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. ASP.NET WEBAPI 简单CURD综合测试(asp.net MVC,json.net,sql基础存储过程和视图,sqlhelper,json解析)

    草图   真正的后端是不管前端是什么平台,用什么语言的,JSON格式的数据应该可以应对.用ASP.NET WEBAPI尝试做一个后端,实现最基本的CURD,业务逻辑和数据库操作都放在后端,前端只需要正 ...

  4. SVN使用教程总结

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  5. Linux内核链表深度分析【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51325646 链表简介: 链表是一种常用的数据结构,它通过指针将一系列数据节点连 ...

  6. Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现

    1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  7. 自定义分词器Analyzer

    Analyzer,或者说文本分析的过程,实质上是将输入文本转化为文本特征向量的过程.这里所说的文本特征,可以是词或者是短语.它主要包括以下四个步骤: 1.分词,将文本解析为单词或短语 2.归一化,将文 ...

  8. Android 对电话进行监听和挂断

    1.添加权限 <!--拨打电话的权限--><uses-permission android:name="android.permission.PROCESS_OUTGOIN ...

  9. iOS 编码规范

    Coding Guidelines for Cocoa https://developer.apple.com/library/prerelease/content/documentation/Coc ...

  10. Gitblit Go

    1.Download the "Gitblit Go" package from the www.gitblit.com 2.UnZip the package 3.Open th ...