/* 作者:Starts_2000
* 日期:2009-07-30
* 网站:http://www.csharpwin.com CS 程序员之窗。
* 你可以免费使用或修改以下代码,但请保留版权信息。
* 具体请查看 CS程序员之窗开源协议(http://www.csharpwin.com/csol.html)。
*/ [ToolboxBitmap(typeof(CheckBox))]
public class CheckBoxEx : CheckBox
{
private Color _baseColor = Color.FromArgb(, , );
private ControlState _controlState; private static readonly ContentAlignment RightAlignment =
ContentAlignment.TopRight |
ContentAlignment.BottomRight |
ContentAlignment.MiddleRight;
private static readonly ContentAlignment LeftAligbment =
ContentAlignment.TopLeft |
ContentAlignment.BottomLeft |
ContentAlignment.MiddleLeft; public CheckBoxEx()
: base()
{
SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
} public Color BaseColor
{
get { return _baseColor; }
set
{
_baseColor = value;
base.Invalidate();
}
} protected virtual int DefaultCheckButtonWidth
{
get { return ; }
} internal ControlState ControlState
{
get { return _controlState; }
set
{
if (_controlState != value)
{
_controlState = value;
base.Invalidate();
}
}
} protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
ControlState = ControlState.Hover;
} protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
ControlState = ControlState.Normal;
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left && e.Clicks == )
{
ControlState = ControlState.Pressed;
}
} protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left && e.Clicks == )
{
if (ClientRectangle.Contains(e.Location))
{
ControlState = ControlState.Hover;
}
else
{
ControlState = ControlState.Normal;
}
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
base.OnPaintBackground(e); Graphics g = e.Graphics;
Rectangle checkButtonRect;
Rectangle textRect; CalculateRect(out checkButtonRect, out textRect);
g.SmoothingMode = SmoothingMode.AntiAlias; Color backColor = ControlPaint.Light(_baseColor);
Color borderColor;
Color innerBorderColor;
Color checkColor;
bool hover = false; if (Enabled)
{
switch (ControlState)
{
case ControlState.Hover:
borderColor = _baseColor;
innerBorderColor = _baseColor;
checkColor = GetColor(_baseColor, , , , );
hover = true;
break;
case ControlState.Pressed:
borderColor = _baseColor;
innerBorderColor = GetColor(_baseColor, , -, -, -);
checkColor = GetColor(_baseColor, , -, -, -);
hover = true;
break;
default:
borderColor = _baseColor;
innerBorderColor = Color.Empty;
checkColor = _baseColor;
break;
}
}
else
{
borderColor = SystemColors.ControlDark;
innerBorderColor = SystemColors.ControlDark;
checkColor = SystemColors.ControlDark;
} using (SolidBrush brush = new SolidBrush(Color.White))
{
g.FillRectangle(brush, checkButtonRect);
} if (hover)
{
using (Pen pen = new Pen(innerBorderColor, 2F))
{
g.DrawRectangle(pen, checkButtonRect);
}
} switch (CheckState)
{
case CheckState.Checked:
DrawCheckedFlag(
g,
checkButtonRect,
checkColor);
break;
case CheckState.Indeterminate:
checkButtonRect.Inflate(-, -);
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(checkButtonRect);
using (PathGradientBrush brush = new PathGradientBrush(path))
{
brush.CenterColor = checkColor;
brush.SurroundColors = new Color[] { Color.White };
Blend blend = new Blend();
blend.Positions = new float[] { 0f, 0.4f, 1f };
blend.Factors = new float[] { 0f, 0.3f, 1f };
brush.Blend = blend;
g.FillEllipse(brush, checkButtonRect);
}
}
checkButtonRect.Inflate(, );
break;
} using (Pen pen = new Pen(borderColor))
{
g.DrawRectangle(pen, checkButtonRect);
} Color textColor = Enabled ? ForeColor : SystemColors.GrayText;
TextRenderer.DrawText(
g,
Text,
Font,
textRect,
textColor,
GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
} private void CalculateRect(
out Rectangle checkButtonRect, out Rectangle textRect)
{
checkButtonRect = new Rectangle(
, , DefaultCheckButtonWidth, DefaultCheckButtonWidth);
textRect = Rectangle.Empty;
bool bCheckAlignLeft = (int)(LeftAligbment & CheckAlign) != ;
bool bCheckAlignRight = (int)(RightAlignment & CheckAlign) != ;
bool bRightToLeft = RightToLeft == RightToLeft.Yes; if ((bCheckAlignLeft && !bRightToLeft) ||
(bCheckAlignRight && bRightToLeft))
{
switch (CheckAlign)
{
case ContentAlignment.TopRight:
case ContentAlignment.TopLeft:
checkButtonRect.Y = ;
break;
case ContentAlignment.MiddleRight:
case ContentAlignment.MiddleLeft:
checkButtonRect.Y = (Height - DefaultCheckButtonWidth) / ;
break;
case ContentAlignment.BottomRight:
case ContentAlignment.BottomLeft:
checkButtonRect.Y = Height - DefaultCheckButtonWidth - ;
break;
} checkButtonRect.X = ; textRect = new Rectangle(
checkButtonRect.Right + ,
,
Width - checkButtonRect.Right - ,
Height);
}
else if ((bCheckAlignRight && !bRightToLeft)
|| (bCheckAlignLeft && bRightToLeft))
{
switch (CheckAlign)
{
case ContentAlignment.TopLeft:
case ContentAlignment.TopRight:
checkButtonRect.Y = ;
break;
case ContentAlignment.MiddleLeft:
case ContentAlignment.MiddleRight:
checkButtonRect.Y = (Height - DefaultCheckButtonWidth) / ;
break;
case ContentAlignment.BottomLeft:
case ContentAlignment.BottomRight:
checkButtonRect.Y = Height - DefaultCheckButtonWidth - ;
break;
} checkButtonRect.X = Width - DefaultCheckButtonWidth - ; textRect = new Rectangle(
, , Width - DefaultCheckButtonWidth - , Height);
}
else
{
switch (CheckAlign)
{
case ContentAlignment.TopCenter:
checkButtonRect.Y = ;
textRect.Y = checkButtonRect.Bottom + ;
textRect.Height = Height - DefaultCheckButtonWidth - ;
break;
case ContentAlignment.MiddleCenter:
checkButtonRect.Y = (Height - DefaultCheckButtonWidth) / ;
textRect.Y = ;
textRect.Height = Height;
break;
case ContentAlignment.BottomCenter:
checkButtonRect.Y = Height - DefaultCheckButtonWidth - ;
textRect.Y = ;
textRect.Height = Height - DefaultCheckButtonWidth - ;
break;
} checkButtonRect.X = (Width - DefaultCheckButtonWidth) / ; textRect.X = ;
textRect.Width = Width - ;
}
} private void DrawCheckedFlag(Graphics graphics, Rectangle rect, Color color)
{
PointF[] points = new PointF[];
points[] = new PointF(
rect.X + rect.Width / 4.5f,
rect.Y + rect.Height / 2.5f);
points[] = new PointF(
rect.X + rect.Width / 2.5f,
rect.Bottom - rect.Height / 3f);
points[] = new PointF(
rect.Right - rect.Width / 4.0f,
rect.Y + rect.Height / 4.5f);
using (Pen pen = new Pen(color, 2F))
{
graphics.DrawLines(pen, points);
}
} private Color GetColor(Color colorBase, int a, int r, int g, int b)
{
int a0 = colorBase.A;
int r0 = colorBase.R;
int g0 = colorBase.G;
int b0 = colorBase.B; if (a + a0 > ) { a = ; } else { a = Math.Max(a + a0, ); }
if (r + r0 > ) { r = ; } else { r = Math.Max(r + r0, ); }
if (g + g0 > ) { g = ; } else { g = Math.Max(g + g0, ); }
if (b + b0 > ) { b = ; } else { b = Math.Max(b + b0, ); } return Color.FromArgb(a, r, g, b);
} internal static TextFormatFlags GetTextFormatFlags(
ContentAlignment alignment,
bool rightToleft)
{
TextFormatFlags flags = TextFormatFlags.WordBreak |
TextFormatFlags.SingleLine;
if (rightToleft)
{
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
} switch (alignment)
{
case ContentAlignment.BottomCenter:
flags |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.BottomLeft:
flags |= TextFormatFlags.Bottom | TextFormatFlags.Left;
break;
case ContentAlignment.BottomRight:
flags |= TextFormatFlags.Bottom | TextFormatFlags.Right;
break;
case ContentAlignment.MiddleCenter:
flags |= TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter;
break;
case ContentAlignment.MiddleLeft:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;
case ContentAlignment.MiddleRight:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;
case ContentAlignment.TopCenter:
flags |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.TopLeft:
flags |= TextFormatFlags.Top | TextFormatFlags.Left;
break;
case ContentAlignment.TopRight:
flags |= TextFormatFlags.Top | TextFormatFlags.Right;
break;
}
return flags;
}
} /// <summary>
/// 控件的状态。
/// </summary>
internal enum ControlState
{
/// <summary>
/// 正常。
/// </summary>
Normal,
/// <summary>
/// 鼠标进入。
/// </summary>
Hover,
/// <summary>
/// 鼠标按下。
/// </summary>
Pressed,
/// <summary>
/// 获得焦点。
/// </summary>
Focused,
}

重写Checkbox 改写选择框的大小的更多相关文章

  1. php获取checkbox复选框的内容

    php获取checkbox复选框的内容   由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取chec ...

  2. php怎么获取checkbox复选框的内容?

    由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取checkbox复选框的值哦. <form n ...

  3. php checkbox复选框值的获取与checkbox默认值输出方法

    php获取 checkbox复选框值的方法,checkbox在php读取值时要用数组形式哦,我们读取这些值用php post获取是以一个array形式哦. php获取 checkbox复选框值的方法 ...

  4. Swift - 选择框(UIPickerView)的用法

    1,选择框可以让用户以滑动的方式选择值.示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  5. cocos2dx2.2.2登录场景中Checkbox选择框的实现

    在前两篇文章中,我们介绍了在注册场景中需要用到的输入框及弹出框的实现方式,这两篇文章中介绍的内容在登录场景同样会用到.而我们经常会在登录场景中见到的另一种元素就是自动登录或者记住密码的Checkbox ...

  6. js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false

    用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...

  7. 用jquery修改默认的单选框radio或者复选框checkbox选择框样式

    默认的radio和checkbox选框很难看.我去看了一下qq注册的页面.发现单选和复选框并没有用<input>,居然是用是A标签.然后用css背景图片展示选择框,用JavaScript控 ...

  8. 转:zTree树控件入门之checkbox:如何动态设置节点的checkbox选择框启用与禁用状态(chkDisabled)

    当一棵树的部分节点根据登入用户角色不同而决定是否启用节点前的checkbox选择框的时候,我们应该如何做呢?也或者如何在页面加载的时候动态根据当前登入用户角色动态切换节点前的checkbox的禁用状态 ...

  9. Selenium(七):选择框(radio框、checkbox框、select框)

    1. 选择框 本章使用的html代码: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

随机推荐

  1. Android 混合式开发AppCan介绍

    Android程序员开发已从最早的异常火热到现在已经逐渐趋向稳定,目前企业针对Android开发工程师的要求要求已逐步提高,现在想从众多的面试者中脱颖而出,必须打好坚实的代码基础. 今天为大家介绍一款 ...

  2. php7安装mongoDB扩展

    本文我们使用pecl命令来安装 首先来到php7的安装目录 $ /usr/local/php7/bin/pecl install mongodb 回车,执行成功后,会输出以下结果: …… Build ...

  3. Axure-如何设置圆形组件

    --打开Axure软件,在[选择部件库]窗口中找到如图中所示的矩形. --将矩形拖到设计布局中 --将鼠标悬停在矩形上就能看到右上角有一个小圆点 --点击小圆点就能选择不同的形状,如:圆形

  4. Mongoose的分页功能

    来自: https://github.com/edwardhotchkiss/mongoose-paginate   拷贝如下: Note: This plugin will only work wi ...

  5. DOM元素尺寸offsetWidth,scrollWidth,clientWidth等具体解释

    样例: <div id="div" style="height: 200px;width: 200px;border:solid 50px red;overflow ...

  6. OpenGL ES 3.0之VertexAttributes,Vertex Arrays,and Buffer Objects(九)

    顶点数据,也称为顶点属性,指每一个顶点数据.指能被用来描述每个顶点的数据,或能被所有顶点使用的常量值.例如你想绘制一个具有颜色的立方体三角形.你指定一个恒定的值用于三角形的所有三个顶点颜色.但三角形的 ...

  7. OpenWRT使用wifidog实现强制认证的WIFI热点

    首先安装wifidog到OpenWRT的路由器: opkg update opkg install wifidog wifidog依赖下面这些模块: iptables-mod-extra iptabl ...

  8. 生日日期联动选择birthday.js

    实例下载

  9. SpringMVC的页面几种返回方式

    package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...

  10. javascript string replace 正则替换

    利用正则式实现首字母大写,丧心病狂是不是?好好的substr不用. JavaScript replace() 方法 r = /^(.)(?=.*)/; str = 'abc'; var str2 = ...