110,570
社区成员
发帖
与我相关
我的任务
分享
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
PaintSomething(e.Graphics); // 把Graphics参数传到画图的方法里
}
// 这是具体画图的方法,可以在同一个类里,也可以在其它类或DLL里。
private void PaintSomething(Graphics g)
{
// 在这里画图
}
class MyPictureBox:PictureBox
{
public Rectangle Rect { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red,1);
g.DrawRectangle(p,Rect);
p.Dispose();
}
}
public partial class Form1 : Form
{
static public Graphics lg;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
lg = e.Graphics;
}
public void test()
{
lg.DrawLine(ipen, point1,point2);//错误提示参数无效 Parameter is not valid.
}
}
1,我是这样传的,但不行,
2,pictureBox只有 pictureBox1_Paint,没有OnPaint,但是我想在pictureBox1上作图呀!