110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
Form1()
{
for (int i = 0; i < 5; i++)
{
MyButton button = new MyButton();
button.Parent = this;
button.Text = "按钮&" + (char)('A' + i);
button.Top = 50;
button.Left = i * 60;
button.Width = 48;
}
}
static void Main()
{
Application.Run(new Form1());
}
}
class MyButton : RadioButton
{
public MyButton()
{
Appearance = Appearance.Button;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Checked)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, Width - 8, Height - 8);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
bool DrawLine = false;
Form1()
{
Button button1 = new Button();
button1.Parent = this;
button1.Text = "画线(&D)";
button1.Click += delegate { DrawLine ^= true; };
button1.Paint += delegate(object o, PaintEventArgs e)
{
if (DrawLine)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, button1.Width - 8, button1.Height - 8);
}
};
}
static void Main()
{
Application.Run(new Form1());
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
bool DrawLine = false;
Button button1;
Form1()
{
button1 = new Button();
button1.Parent = this;
button1.Text = "画线(&D)";
button1.Click += new EventHandler(button1_Click);
button1.Paint += new PaintEventHandler(button1_Paint);
}
private void button1_Click(object sender, EventArgs e)
{
DrawLine ^= true;
}
private void button1_Paint(object sender, PaintEventArgs e)
{
if (DrawLine)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, button1.Width - 8, button1.Height - 8);
}
}
static void Main()
{
Application.Run(new Form1());
}
}
private bool DrawLine = false;
private void button1_Click(object sender, EventArgs e)
{
DrawLine ^= true;
}
在每次窗体Paint重显的时候:
if (DrawLine)
{
Graphics g = button1.CreateGraphics();
Pen pen = new Pen(Color.Blue);
g.DrawRectangle(pen, 3, 3, button1.Width - 8, button1.Height - 8);
}