我的问题是如何在C#中的子窗口中绘制一条简单的行,即:
好吧,我该怎么做?这是我的子窗口代码:
public partial class Form2 : Form
{
Pen pen;
public Form2()
{
InitializeComponent();
pen = new Pen(Color.Black);
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
e.Graphics.DrawLine(pen, 10, 10, 100, 100);
}
}
感谢。
答案 0 :(得分:4)
以下代码更好:
private void Form2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (Pen p = new Pen(Color.Black))
{
g.DrawLine(pen, 10, 10, 100, 100);
}
}
使用paint事件中的图形实例,使用语法自动配置笔对象
记住在父窗口中调用显示方法,如下所示:
private void button_Click(object sender, EventArgs e)
{
Form form2 = new Form2();
form2.ShowDialog();
}