在C#中,可以通过继承Form类并重写其OnPaint方法来实现自定义绘制。以下是一些常用的自定义绘制技巧:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
string text = "Hello, World!";
Font font = new Font("Arial", 12);
Brush brush = new SolidBrush(Color.Black);
g.DrawString(text, font, brush, 10, 10);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Red, 2);
g.DrawRectangle(pen, 20, 20, 100, 50);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Image image = Image.FromFile("image.jpg");
g.DrawImage(image, 50, 50);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 2);
g.DrawEllipse(pen, 50, 50, 100, 100);
g.DrawLine(pen, 50, 100, 150, 100);
}
通过以上技巧,可以实现自定义绘制各种图形、文本和图像,从而使应用程序界面更加丰富和个性化。