复选框及字体风格CheckBoxDemo

Code:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace CsDev
{
    class CheckBoxDemo:Form
    {
        public static void Main()
        {
            Application.Run(new CheckBoxDemo());
        }

        public CheckBoxDemo()
        {
            Text = "复选框Demo";
            Font = new Font("Arial",12);
            CheckBox[] achkbox = new CheckBox[4];
            int cyText = Font.Height;
            int cxText = cyText / 2;
            string[] astr = { "Bold","Italic","Underline","Strikout"};

            for (int i = 0; i < 4; i++)
            {
                achkbox[i] = new CheckBox();
                achkbox[i].Text = astr[i];
                achkbox[i].Location = new Point(2*cxText,(4+3*i)*cyText/2);
                achkbox[i].Size = new Size(12*cxText,cyText);
                achkbox[i].CheckedChanged+=new EventHandler(CheckBoxDemo_CheckedChanged);
            }

            Controls.AddRange(achkbox);//批量添加控件
        }

        void CheckBoxDemo_CheckedChanged(object obj, EventArgs e)
        {
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics grph = e.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 = new Font(Font,fs);
            grph.DrawString("简单文本", font, new SolidBrush(ForeColor),0, 0);
        }
    }
}

效果图:

复选框及字体风格CheckBoxDemo_第1张图片

你可能感兴趣的:(C#)