所有控件(Control类的派生类)都有CreateGraphics方法提供画板,所以你可以:
//假设panel1是将直线盖住的控件
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(BackColor);panel1.CreateGraphics().Clear(panel1.BackColor);
e.Graphics.DrawLine(Pens.Black, new Point(0, 0), new Point(ClientRectangle.Width, ClientRectangle.Height));
//注意坐标系变换。
panel1.CreateGraphics().DrawLine(Pens.Black, new Point(-panel1.Left, -panel1.Top), new Point(ClientRectangle.Width - panel1.Left, ClientRectangle.Height - panel1.Top));
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Form1_Paint(this, new PaintEventArgs(CreateGraphics(), ClientRectangle));
}
C#在Panel上画图
最新推荐文章于 2024-09-20 17:25:06 发布