之后也有一个动态的操作与显示。现在我遇到一些问题:如图
这个是在自定义控件预览时,但没有出现想要的那个圆,只有当鼠标移到BUTTON上时才出现,我想要的是这个圆与窗体是同时出现的。
我的代码如下:
public partial class TestControl : UserControl {
public TestControl() {
InitializeComponent();
g = this.pictureBox1.CreateGraphics();
testPaint();
Refresh();
}
Graphics g;
private void testPaint() {
Pen MyPen = new Pen(Color.Blue, 3);
Rectangle rg = new Rectangle(50, 50, 200, 200);
g.DrawEllipse(MyPen, rg);
MyPen.Dispose();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);//引发Paint事件处理(处理该事件时候调用Form1_Paint方法)
Console.WriteLine("Write In Test onPaint");
testPaint();
}
private void button1_Click(object sender, EventArgs e) {
Console.WriteLine("Write In Test pb button");
testPaint();
}
private void TestControl_Load(object sender, EventArgs e) {
Console.WriteLine("Write In Test pb LOAD");
testPaint();
}
}
4 个解决方案
#1
至多这样就可以
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);//引发Paint事件处理(处理该事件时候调用Form1_Paint方法)
var g = e.Graphics;
Pen MyPen = new Pen(Color.Blue, 3);
Rectangle rg = new Rectangle(50, 50, 200, 200);
g.DrawEllipse(MyPen, rg);
MyPen.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
Invalidate();
}
}
#2
还是不行,你可能理解错了我的意思:
我要的是这个圆,在这个自定义的控件,添加到某个窗体上,这个窗体运行的时候不经过任何操作就显示出来
#3
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var t = new TestControl();
t.Show();
t.Location = new Point(20, 20);
t.Name = "t";
Controls.Add(t);
}
private void button2_Click(object sender, EventArgs e)
{
//var t = Controls.Find("TestControl", false);
Controls.RemoveByKey("t");
}
}
public partial class TestControl : UserControl
{
public TestControl()
{
//InitializeComponent();
Width = 200;
Height = 200;
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
Pen MyPen = new Pen(Color.Blue, 3);
Rectangle rg = new Rectangle(1, 1, Width - 3, Height - 3);
g.DrawEllipse(MyPen, rg);
MyPen.Dispose();
}
}
#4
控件加到窗体上自动会出现的, 可能是楼主的代码写错地方了.
#1
至多这样就可以
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);//引发Paint事件处理(处理该事件时候调用Form1_Paint方法)
var g = e.Graphics;
Pen MyPen = new Pen(Color.Blue, 3);
Rectangle rg = new Rectangle(50, 50, 200, 200);
g.DrawEllipse(MyPen, rg);
MyPen.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
Invalidate();
}
}
#2
还是不行,你可能理解错了我的意思:
我要的是这个圆,在这个自定义的控件,添加到某个窗体上,这个窗体运行的时候不经过任何操作就显示出来
#3
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var t = new TestControl();
t.Show();
t.Location = new Point(20, 20);
t.Name = "t";
Controls.Add(t);
}
private void button2_Click(object sender, EventArgs e)
{
//var t = Controls.Find("TestControl", false);
Controls.RemoveByKey("t");
}
}
public partial class TestControl : UserControl
{
public TestControl()
{
//InitializeComponent();
Width = 200;
Height = 200;
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
Pen MyPen = new Pen(Color.Blue, 3);
Rectangle rg = new Rectangle(1, 1, Width - 3, Height - 3);
g.DrawEllipse(MyPen, rg);
MyPen.Dispose();
}
}
#4
控件加到窗体上自动会出现的, 可能是楼主的代码写错地方了.