15.C#编程学习——checkbox
源码如下:usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
classCheckBoxDemo : Form
{
publicstaticvoid Main()
{
Application.Run(newCheckBoxDemo());
}
public CheckBoxDemo()
{
Text = "CheckBox Demo";
CheckBox[] achkbox = newCheckBox[4];
int cyText = Font.Height;
int cxText = cyText / 2;
string[] astrText = { "Bold", "Italic","Underline", "Strikeout" };
for (int i = 0; i < 4; i++)
{
achkbox[i] = newCheckBox();
achkbox[i].Text = astrText[i];
achkbox[i].Location = newPoint(2* cxText,
(4+ 3 * i) * cyText / 2);
achkbox[i].Size = newSize(12* cxText, cyText);
achkbox[i].CheckedChanged +=
newEventHandler(CheckBoxOnCheckedChanged);
}
Controls.AddRange(achkbox);
}
void CheckBoxOnCheckedChanged(objectobj, EventArgsea)
{
Invalidate(false);
}
protectedoverridevoid OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
FontStyle fs = 0;
FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
FontStyle.Underline, FontStyle.Strikeout };
for (int i = 0; i < 4; i++)
if (((CheckBox)Controls[i]).Checked)
fs |= afs[i];
Font font = newFont(Font, fs);
grfx.DrawString(Text, font, newSolidBrush(ForeColor), 0, 0);
}
}