Code:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CsDev
{
class CheckAndRadioCheck : Form
{
MenuItem miColor, miFill;
static void Main()
{
Application.Run(new CheckAndRadioCheck());
}
CheckAndRadioCheck()
{
Text = "Check And Radio Check";
ResizeRedraw = true;
string[] astrColor = { "Black", "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White" };
MenuItem[] ami = new MenuItem[astrColor.Length + 2];
EventHandler ehColor = new EventHandler(MenuFormatColorOnClick);
for (int i = 0; i < astrColor.Length; i++)
{
ami[i] = new MenuItem(astrColor[i], ehColor);
ami[i].RadioCheck = true;
}
miColor = ami[0];
miColor.Checked = true;
miFill = new MenuItem("&Fill", new EventHandler(MenuFormatFillOnClick));
ami[astrColor.Length] = new MenuItem("-");
ami[astrColor.Length + 1] = miFill;
MenuItem mi = new MenuItem("&Format", ami);
Menu = new MainMenu(new MenuItem[] { mi });
}
void MenuFormatColorOnClick(object obj, EventArgs e)
{
miColor.Checked = false;
miColor = (MenuItem)obj;
miColor.Checked = true;
Invalidate();
}
void MenuFormatFillOnClick(object obj, EventArgs e)
{
MenuItem mi = (MenuItem)obj;
mi.Checked ^= true;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics grph = e.Graphics;
if (miFill.Checked)
{
Brush brush = new SolidBrush(Color.FromName(miColor.Text));
grph.FillEllipse(brush, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
else
{
Pen pen = new Pen(Color.FromName(miColor.Text));
grph.DrawEllipse(pen, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
}
}
}
效果图:
说明:Radio控制绘制椭圆的线条颜色,复选按钮控制是否以Radio选项的颜色填充椭圆内部。