using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms; namespace SplitButtonDemo
{
public class SplitButton : Button
{
#region Fields private bool _DoubleClickedEnabled; private bool _AlwaysDropDown;
private bool _AlwaysHoverChange; private bool _CalculateSplitRect = true;
private bool _FillSplitHeight = true;
private int _SplitHeight;
private int _SplitWidth; private String _NormalImage;
private String _HoverImage;
private String _ClickedImage;
private String _DisabledImage;
private String _FocusedImage; private ImageList _DefaultSplitImages; [Browsable(true)]
[Category("Action")]
[Description("Occurs when the button part of the SplitButton is clicked.")]
public event EventHandler ButtonClick; [Browsable(true)]
[Category("Action")]
[Description("Occurs when the button part of the SplitButton is clicked.")]
public event EventHandler ButtonDoubleClick; #endregion #region Properties
/// <summary>
///
/// </summary>
[Category("Behavior")]
[Description("Indicates whether the double click event is raised on the SplitButton")]
[DefaultValue(false)]
public bool DoubleClickedEnabled
{
get
{
return _DoubleClickedEnabled;
}
set
{
_DoubleClickedEnabled = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button")]
[Description("Indicates whether the SplitButton always shows the drop down menu even if the button part of the SplitButton is clicked.")]
[DefaultValue(false)]
public bool AlwaysDropDown
{
get
{
return _AlwaysDropDown;
}
set
{
_AlwaysDropDown = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button")]
[Description("Indicates whether the SplitButton always shows the Hover image status in the split part even if the button part of the SplitButton is hovered.")]
[DefaultValue(false)]
public bool AlwaysHoverChange
{
get
{
return _AlwaysHoverChange;
}
set
{
_AlwaysHoverChange = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button")]
[Description("Indicates whether the split rectange must be calculated (basing on Split image size)")]
[DefaultValue(true)]
public bool CalculateSplitRect
{
get
{
return _CalculateSplitRect;
}
set
{
bool flag1 = _CalculateSplitRect; _CalculateSplitRect = value; if (flag1 != _CalculateSplitRect)
{
if (_SplitWidth > && _SplitHeight > )
{
InitDefaultSplitImages(true);
}
}
}
} /// <summary>
/// Gets or sets a value indicating whether [fill split height].
/// </summary>
/// <value><c>true</c> if [fill split height]; otherwise, <c>false</c>.</value>
[Category("Split Button")]
[Description("Indicates whether the split height must be filled to the button height even if the split image height is lower.")]
[DefaultValue(true)]
public bool FillSplitHeight
{
get
{
return _FillSplitHeight;
}
set
{
_FillSplitHeight = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button")]
[Description("The split height (ignored if CalculateSplitRect is setted to true).")]
[DefaultValue()]
public int SplitHeight
{
get
{
return _SplitHeight;
}
set
{
_SplitHeight = value; if (!_CalculateSplitRect)
{
if (_SplitWidth > && _SplitHeight > )
{
InitDefaultSplitImages(true);
}
}
}
} /// <summary>
///
/// </summary>
[Category("Split Button")]
[Description("The split width (ignored if CalculateSplitRect is setted to true).")]
[DefaultValue()]
public int SplitWidth
{
get
{
return _SplitWidth;
}
set
{
_SplitWidth = value; if (!_CalculateSplitRect)
{
if (_SplitWidth > && _SplitHeight > )
{
InitDefaultSplitImages(true);
}
}
}
} /// <summary>
///
/// </summary>
[Category("Split Button Images")]
[Description("The Normal status image name in the ImageList.")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), RefreshProperties(RefreshProperties.Repaint), TypeConverter(typeof(ImageKeyConverter))]
public String NormalImage
{
get
{
return _NormalImage;
}
set
{
_NormalImage = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button Images")]
[Description("The Hover status image name in the ImageList.")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), RefreshProperties(RefreshProperties.Repaint), TypeConverter(typeof(ImageKeyConverter))]
public String HoverImage
{
get
{
return _HoverImage;
}
set
{
_HoverImage = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button Images")]
[Description("The Clicked status image name in the ImageList.")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), RefreshProperties(RefreshProperties.Repaint), TypeConverter(typeof(ImageKeyConverter))]
public String ClickedImage
{
get
{
return _ClickedImage;
}
set
{
_ClickedImage = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button Images")]
[Description("The Disabled status image name in the ImageList.")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), RefreshProperties(RefreshProperties.Repaint), TypeConverter(typeof(ImageKeyConverter))]
public String DisabledImage
{
get
{
return _DisabledImage;
}
set
{
_DisabledImage = value;
}
} /// <summary>
///
/// </summary>
[Category("Split Button Images")]
[Description("The Focused status image name in the ImageList.")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Localizable(true), RefreshProperties(RefreshProperties.Repaint), TypeConverter(typeof(ImageKeyConverter))]
public String FocusedImage
{
get
{
return _FocusedImage;
}
set
{
_FocusedImage = value;
}
} #endregion #region Construction public SplitButton()
{
InitializeComponent();
} #endregion #region Methods protected override void OnCreateControl()
{
InitDefaultSplitImages(); if (this.ImageList == null)
{
this.ImageList = _DefaultSplitImages;
} if (Enabled)
{
SetSplit(_NormalImage);
}
else
{
SetSplit(_DisabledImage);
} base.OnCreateControl();
} private void InitDefaultSplitImages()
{
InitDefaultSplitImages(false);
} private void InitDefaultSplitImages(bool refresh)
{
if (String.IsNullOrEmpty(_NormalImage))
{
_NormalImage = "Normal";
} if (String.IsNullOrEmpty(_HoverImage))
{
_HoverImage = "Hover";
} if (String.IsNullOrEmpty(_ClickedImage))
{
_ClickedImage = "Clicked";
} if (String.IsNullOrEmpty(_DisabledImage))
{
_DisabledImage = "Disabled";
} if (String.IsNullOrEmpty(_FocusedImage))
{
_FocusedImage = "Focused";
} if (_DefaultSplitImages == null)
{
_DefaultSplitImages = new ImageList();
} if (_DefaultSplitImages.Images.Count == || refresh)
{
if (_DefaultSplitImages.Images.Count > )
{
_DefaultSplitImages.Images.Clear();
} try
{
int w = ;
int h = ; if (!_CalculateSplitRect && _SplitWidth > )
{
w = _SplitWidth;
}
else
{
w = ;
} if (!CalculateSplitRect && SplitHeight > )
{
h = SplitHeight;
}
else
{
h = Height;
} h -= ; _DefaultSplitImages.ImageSize = new Size(w, h); int mw = w / ;
mw += (mw % );
int mh = h / ; Pen fPen = new Pen(ForeColor, );
SolidBrush fBrush = new SolidBrush(ForeColor); Bitmap imgN = new Bitmap(w, h);
Graphics g = Graphics.FromImage(imgN); g.CompositingQuality = CompositingQuality.HighQuality; g.DrawLine(SystemPens.ButtonShadow, new Point(, ), new Point(, h - ));
g.DrawLine(SystemPens.ButtonFace, new Point(, ), new Point(, h)); g.FillPolygon(fBrush, new Point[] { new Point(mw - , mh - ),
new Point(mw + , mh - ),
new Point(mw, mh + ) }); g.Dispose(); Bitmap imgH = new Bitmap(w, h);
g = Graphics.FromImage(imgH); g.CompositingQuality = CompositingQuality.HighQuality; g.DrawLine(SystemPens.ButtonShadow, new Point(, ), new Point(, h - ));
g.DrawLine(SystemPens.ButtonFace, new Point(, ), new Point(, h)); g.FillPolygon(fBrush, new Point[] { new Point(mw - , mh - ),
new Point(mw + , mh - ),
new Point(mw, mh + ) }); g.Dispose(); Bitmap imgC = new Bitmap(w, h);
g = Graphics.FromImage(imgC); g.CompositingQuality = CompositingQuality.HighQuality; g.DrawLine(SystemPens.ButtonShadow, new Point(, ), new Point(, h - ));
g.DrawLine(SystemPens.ButtonFace, new Point(, ), new Point(, h)); g.FillPolygon(fBrush, new Point[] { new Point(mw - , mh - ),
new Point(mw + , mh - ),
new Point(mw, mh + ) }); g.Dispose(); Bitmap imgD = new Bitmap(w, h);
g = Graphics.FromImage(imgD); g.CompositingQuality = CompositingQuality.HighQuality; g.DrawLine(SystemPens.GrayText, new Point(, ), new Point(, h - )); g.FillPolygon(new SolidBrush(SystemColors.GrayText), new Point[] { new Point(mw - , mh - ),
new Point(mw + , mh - ),
new Point(mw, mh + ) }); g.Dispose(); Bitmap imgF = new Bitmap(w, h);
g = Graphics.FromImage(imgF); g.CompositingQuality = CompositingQuality.HighQuality; g.DrawLine(SystemPens.ButtonShadow, new Point(, ), new Point(, h - ));
g.DrawLine(SystemPens.ButtonFace, new Point(, ), new Point(, h)); g.FillPolygon(fBrush, new Point[] { new Point(mw - , mh - ),
new Point(mw + , mh - ),
new Point(mw, mh + ) }); g.Dispose(); fPen.Dispose();
fBrush.Dispose(); _DefaultSplitImages.Images.Add(_NormalImage, imgN);
_DefaultSplitImages.Images.Add(_HoverImage, imgH);
_DefaultSplitImages.Images.Add(_ClickedImage, imgC);
_DefaultSplitImages.Images.Add(_DisabledImage, imgD);
_DefaultSplitImages.Images.Add(_FocusedImage, imgF);
}
catch
{ }
}
} protected override void OnMouseMove(MouseEventArgs mevent)
{
if (_AlwaysDropDown || _AlwaysHoverChange || MouseInSplit())
{
if (Enabled)
{
SetSplit(_HoverImage);
}
}
else
{
if (Enabled)
{
SetSplit(_NormalImage);
}
} base.OnMouseMove(mevent);
} protected override void OnMouseLeave(EventArgs e)
{
if (Enabled)
{
SetSplit(_NormalImage);
} base.OnMouseLeave(e);
} protected override void OnMouseDown(MouseEventArgs mevent)
{
if (_AlwaysDropDown || MouseInSplit())
{
if (Enabled)
{
SetSplit(_ClickedImage); if (this.ContextMenuStrip != null && this.ContextMenuStrip.Items.Count > )
{
this.ContextMenuStrip.Show(this, new Point(, Height));
}
}
}
else
{
if (Enabled)
{
SetSplit(_NormalImage);
}
} base.OnMouseDown(mevent);
} protected override void OnMouseUp(MouseEventArgs mevent)
{
if (_AlwaysDropDown || _AlwaysHoverChange || MouseInSplit())
{
if (Enabled)
{
SetSplit(_HoverImage);
}
}
else
{
if (Enabled)
{
SetSplit(_NormalImage);
}
} base.OnMouseUp(mevent);
} protected override void OnEnabledChanged(EventArgs e)
{
if (!Enabled)
{
SetSplit(_DisabledImage);
}
else
{
if (MouseInSplit())
{
SetSplit(_HoverImage);
}
else
{
SetSplit(_NormalImage);
}
} base.OnEnabledChanged(e);
} protected override void OnGotFocus(EventArgs e)
{
if (Enabled)
{
SetSplit(_FocusedImage);
} base.OnGotFocus(e);
} protected override void OnLostFocus(EventArgs e)
{
if (Enabled)
{
SetSplit(_NormalImage);
} base.OnLostFocus(e);
} protected override void OnClick(EventArgs e)
{
base.OnClick(e); if (!MouseInSplit() && !_AlwaysDropDown)
{
if (ButtonClick != null)
{
ButtonClick(this, e);
}
}
} protected override void OnDoubleClick(EventArgs e)
{
if (_DoubleClickedEnabled)
{
base.OnDoubleClick(e); if (!MouseInSplit() && !_AlwaysDropDown)
{
if (ButtonClick != null)
{
ButtonDoubleClick(this, e);
}
}
}
} private void SetSplit(String imageName)
{
if (imageName != null && ImageList != null && ImageList.Images.ContainsKey(imageName))
{
this.ImageKey = imageName;
}
} public bool MouseInSplit()
{
return PointInSplit(PointToClient(MousePosition));
} public bool PointInSplit(Point pt)
{
Rectangle splitRect = GetImageRect(_NormalImage); if (!_CalculateSplitRect)
{
splitRect.Width = _SplitWidth;
splitRect.Height = _SplitHeight;
} return splitRect.Contains(pt);
} public Rectangle GetImageRect(String imageKey)
{
Image currImg = GetImage(imageKey); if (currImg != null)
{
int x = ,
y = ,
w = currImg.Width+,
h = currImg.Height+; if (w > this.Width)
{
w = this.Width;
} if (h > this.Width)
{
h = this.Width;
} switch (ImageAlign)
{
case ContentAlignment.TopLeft:
{
x = ;
y = ; break;
}
case ContentAlignment.TopCenter:
{
x = (this.Width - w) / ;
y = ; if ((this.Width - w) % > )
{
x += ;
} break;
}
case ContentAlignment.TopRight:
{
x = this.Width - w;
y = ; break;
}
case ContentAlignment.MiddleLeft:
{
x = ;
y = (this.Height - h) / ; if ((this.Height - h) % > )
{
y += ;
} break;
}
case ContentAlignment.MiddleCenter:
{
x = (this.Width - w) / ;
y = (this.Height - h) / ; if ((this.Width - w) % > )
{
x += ;
}
if ((this.Height - h) % > )
{
y += ;
} break;
}
case ContentAlignment.MiddleRight:
{
x = this.Width - w;
y = (this.Height - h) / ; if ((this.Height - h) % > )
{
y += ;
} break;
}
case ContentAlignment.BottomLeft:
{
x = ;
y = this.Height - h; if ((this.Height - h) % > )
{
y += ;
} break;
}
case ContentAlignment.BottomCenter:
{
x = (this.Width - w) / ;
y = this.Height - h; if ((this.Width - w) % > )
{
x += ;
} break;
}
case ContentAlignment.BottomRight:
{
x = this.Width - w;
y = this.Height - h; break;
}
} if (_FillSplitHeight && h < this.Height)
{
h = this.Height;
} if (x > )
{
x -= ;
}
if (y > )
{
y -= ;
} return new Rectangle(x, y, w, h);
} return Rectangle.Empty;
} private Image GetImage(String imageName)
{
if (this.ImageList != null && this.ImageList.Images.ContainsKey(imageName))
{
return this.ImageList.Images[imageName];
} return null;
} #endregion /// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Component Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
} #endregion
}
}

[转]下拉按钮 C#_Winform 自定义控件的更多相关文章

  1. Qt中QComboBox中自定义界面使用stylesheet实现下拉按钮独立效果

    使用QSS自定义控件界面时,QT中控件QCombobox含有两个子控件drop-down和down-arrow.一般而言,当改变QCombox时,很多效果都会出来,但是,针对下拉按钮和下拉图标的自定义 ...

  2. 修改select下拉框的下拉按钮

    ie上的下拉框下拉按钮真是太丑了,如何把他自定义一下呢? 首先,把浏览器自带的下拉框去掉:  select::-ms-expand { display: none; } 接下来,用自己喜欢的下拉图片去 ...

  3. 微信小程序下拉按钮动画

    有些时候要求下拉按钮需要动画效果,但又不需要引入插件. 这时需要手动写一个动画. 主要思路: 动态切换class 默认与动画转向的样式编写 上图是默认给出的按钮向下的样式, 上图是动画转向后的样式 上 ...

  4. (四十八)c#Winform自定义控件-下拉按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  5. 安卓系统浏览器中select下拉按钮无法弹出选择面板奇怪问题解决

    今天遇到个让人崩溃的问题: 平台: 安卓 4.0 描述: 使用 appcan 开发 hybrid 应用,手机上点击下拉选框按钮无法弹出选择面板. 说明: 发现 webkit 内核 position:f ...

  6. element-ui下拉按钮的用法

    <el-dropdown class="avatar-container" trigger="click"> <div class=" ...

  7. 如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox) [转]

    原文地址:http://bbs.csdn.net/topics/390135022 http://blog.csdn.net/scsdn/article/details/4363299 想使用winf ...

  8. Bootstrap框架(基础篇)之按钮,网格,导航栏,下拉菜单

    一,按钮 注意:虽然在Bootstrap框架中使用任何标签元素都可以实现按钮风格,但个人并不建议这样使用,为了避免浏览器兼容性问题,个人强烈建议使用button或a标签来制作按钮. 框架中提供了基础按 ...

  9. jQuery打造智能提示插件二(可编辑下拉框)

    在上一篇 jQuery打造智能提示插件 上改进,增加下拉按钮,修复点击下拉区域外不隐藏BUG 效果 下拉按钮素材: js封装,注意红色部分为BUG修复,然后传入boxwidth不带px: /* /// ...

随机推荐

  1. 《Python》 while循环、运算符和编码初识

    一.while 循环 while也叫无限循环 while 条件: 循环体 判断条件: 条件为真,进入循环体,循环体执行到底部,返回,继续判断条件. 终止循环: 1.改变条件(标志位的概念) 2.bre ...

  2. 驱动程序多线程 PsCreateSystemThread

    内核函数PsCreateSystemThread负责创建新线程.该函数可以创建两种线程,一种是用户线程,它属于当前进程中的线程.另一种是系统线程,系统线程不属于当前用户进程,而是属于系统进程,一般PI ...

  3. 解决Ubuntu下添加Log却无法输出(高通平台)

    1.首先考虑到的是你所添加Log的位置确实没有被调用或者在调用前发生异常终止掉了. 2.你修改后没有进行编译或者没有push/install 3.如果你当前的系统版本是User版本,你在push的时候 ...

  4. cobbler网络装机

    cobbler网络装机 原理分析 cobbler简介 Cobbler通过将设置和管理一个安装服务器所涉及的任务集中在一起,从而简化了系统配置.相当于Cobbler封装了DHCP.TFTP.XINTED ...

  5. SSH 获取GET/POST参数

    在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...

  6. 配置阿里云ECS支持IPv6,解决苹果app审核失败问题

    前几天iOS的App提交给苹果审核没通过,给出的原因是:该应用在 IPv6 的环境下无法使用.检查发现:阿里云优化过的系统没有启用IPv6协议,需要配置启用一下,但是只单独启用IPv6也是无法直接提供 ...

  7. C++ 和 Java 对类继承的差异

    #include <iostream> using namespace std; class Base { public: int i; Base() { i = ; fun(); } v ...

  8. matlab repmat()

    一.B = repmat(A,m,n) 将矩阵 A 复制 m×n 块,即把 A 作为 B 的元素,B 由 m×n 个 A 平铺而成.B 的维数是 [size(A,1)*m, size(A,2)*n] ...

  9. Unity 3D游戏-NPC对话系统With XML

    用XML做的Unity NPC对话系统 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Create X ...

  10. 对比dfs与bfs的存储机制以及bfs与队列的关系

    dfs由于是利用递归进行遍历,所以每种情况在时空上不会出现冲突,所以可以利用数组将每种情况的各个元素的值进行存储(即存储当前位) 而bfs由于并不是利用递归,不能将每种情况的值进行不冲突地存储,但由于 ...