我想在Tabpage上使用GDI绘图,经查询,使用过下面两种方法:
一、
private void Form1_Load(object sender, EventArgs e)
{
TabPage1.Paint += TabPage1_Paint;
}
private void TabPage1_Paint(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Red, 10);
graphics.DrawLine(myPen, 0, 400, 1000, 400);
}
二、
private void Form1_Load(object sender, EventArgs e)
{
TabPage1.Paint += TabPage1_Paint;
}
protected override void OnPaint(PaintEventArgs e)//重写OnPaint方法
{
base.OnPaint(e);//先执行基类的OnPaint方法
Graphics graphics = e.Graphics;
Pen mypen = new Pen(Color.Red, 10);
graphics.DrawLine(mypen, 0, 400, 100, 400);
}
所得结果均会被遮挡,无法在tabpage上显示,请问正确的做法应是什么呢?
初学者,请大神们指教!
很明显你的Graphics对象传错了,要在tabcontrol上画图像就应该传tabcontrol的Graphics对象,第一种应该是Graphics graphics = e.Graphics;而不是this.CreateGraphics();this.CreateGraphics()指的还是当前窗体的Graphics对象,所以你一直是画在了Form上,第二种不用说了,你重写的直接就是Form的Paint事件,用的对象e.Graphics对象也是Form的,所以也是画在了Form上而不是画在TabControl上
DrawMode 属性设置为 OwnerDraw 没有