Graphics对象用于进行绘制;
从哪个对象获取的Graphics,然后进行绘制,就绘制到该对象上;
从位图获取Graphics,然后进行绘制,绘制到该位图上;
从某个控件获取Graphics,然后绘制,绘制到该控件上;
在form上添加5个控件,
private void button2_Click(object sender, EventArgs e)
{
Rectangle rect1 = new Rectangle(20, 20, 30, 30);
Graphics g1 = Graphics.FromHwnd(button1.Handle);
g1.FillRectangle(Brushes.Green, rect1);
Graphics g2 = Graphics.FromHwnd(textBox1.Handle);
g2.FillRectangle(Brushes.Red, rect1);
Graphics g3 = Graphics.FromHwnd(pictureBox1.Handle);
g3.FillRectangle(Brushes.Blue, rect1);
Rectangle rect2 = new Rectangle(10, 10, 30, 30);
Graphics g4 = Graphics.FromHwnd(comboBox1.Handle);
g4.FillRectangle(Brushes.Black, rect2);
Graphics g5 = Graphics.FromHwnd(label1.Handle);
g5.FillRectangle(Brushes.Black, rect2);
}
从这5个控件获取Graphics,然后分别绘制一个小矩形;
从控件获取Graphics使用FromHwnd方法,参数是该控件的句柄;
Graphics.FromHwnd(button1.Handle);