Han*_*ant 36
复选框大小在Windows窗体内是硬编码的,你不能搞砸它.一种可能的解决方法是在现有复选框的顶部绘制一个复选框.这不是一个很好的解决方案,因为自动调整大小不再可以工作,文本对齐混乱,但它是可用的.
在项目中添加一个新类并粘贴下面显示的代码.编译.将新控件从工具箱顶部拖放到表单上.调整控件的大小,以便获得所需的盒子大小,并确保其宽度足以适合文本.
using System;
using System.Drawing;
using System.Windows.Forms;
class MyCheckBox : CheckBox {
public MyCheckBox() {
this.TextAlign = ContentAlignment.MiddleRight;
}
public override bool AutoSize {
get { return base.AutoSize; }
set { base.AutoSize = false; }
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
int h = this.ClientSize.Height - 2;
Rectangle rc = new Rectangle(new Point(0, 1), new Size(h, h));
ControlPaint.DrawCheckBox(e.Graphics, rc,
this.Checked ? ButtonState.Checked : ButtonState.Normal);
}
}
Run Code Online (Sandbox Code Playgroud)
ASe*_*aya 17
窗户有一个AutoSize
选项Properties
; 如果你通过改变它来关闭它False
,你将能够修改你的大小CheckBox
.
C#版本,来自forum.codecall.net主题:
class BigCheckBox : CheckBox
{
public BigCheckBox()
{
this.Text = "Approved";
this.TextAlign = ContentAlignment.MiddleRight;
}
public override bool AutoSize
{
set { base.AutoSize = false; }
get { return base.AutoSize; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.Height = 100;
this.Width = 200;
int squareSide = 80;
Rectangle rect = new Rectangle(new Point(0, 1), new Size(squareSide, squareSide));
ControlPaint.DrawCheckBox(e.Graphics, rect, this.Checked ? ButtonState.Checked : ButtonState.Normal);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
49292 次 |
最近记录: |