winfrom 重绘checkbox 使前面的选着框变大

大_爱 2013-07-16 03:25:03
请问这个怎么做啊。

我在网上下了一份示例性的代码


public class SkinCheckBox:CheckBox
{
private State state=State.Normal;

public SkinCheckBox()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = System.Drawing.Color.Transparent;
}

protected override void OnMouseEnter(EventArgs e)
{
state = State.MouseOver;
this.Invalidate();
base.OnMouseEnter(e);
}

protected override void OnMouseLeave(EventArgs e)
{
state = State.Normal;
this.Invalidate();
base.OnMouseLeave(e);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return;

state = State.MouseDown;
this.Invalidate();
base.OnMouseDown(e);
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
state = State.Normal;
this.Invalidate();
base.OnMouseUp(e);
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (SkinImage.checkbox.img==null)
{
base.OnPaint(e);
return;
}

int i = (int)state;
if (!this.Enabled) i = 16;
if (this.CheckState == CheckState.Checked) i+=16;
if (this.CheckState == CheckState.Indeterminate) i+=16;

Rectangle rc = this.ClientRectangle;
Rectangle r1 = rc;;
Graphics g = e.Graphics;
base.OnPaint(e);

int cw = SystemInformation.MenuCheckSize.Width ;

if (this.CheckAlign == ContentAlignment.MiddleLeft)
{
r1=Rectangle.FromLTRB(0,(r1.Height-cw)/2,0+cw,(r1.Height+cw)/2);
}
else
{
r1=Rectangle.FromLTRB(r1.Right-cw-1,(r1.Height-cw)/2,r1.Right,(r1.Height+cw)/2);
}
SkinDraw.DrawRect1(g,SkinImage.checkbox,r1,i);
}
}


还重写了里面的事件,我可以不重写吗?

OnPaint()方法不是太懂。


...全文
584 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
思则出 2014-11-09
  • 打赏
  • 举报
回复
能把源码发给我一份吗?兄弟
大_爱 2013-07-16
  • 打赏
  • 举报
回复
看来这个比较难一点。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧