2012-08-15 40 views
0

这是我的代码有:如何绘制动态创建的按钮控件?

Button b = new Button(); 
Graphics g = b.CreateGraphics(); 

g.DrawString("Hello World", new Font(FontFamily.GenericSerif, 5), Brushes.Red, new PointF(1, 1)); 

b.Height = 100; 
b.Width = 100; 

this.Controls.Add(b); 

我得到一个按钮,但它没有像..

我使用下面的代码,它做什么,我需要的。感谢大家。

Button b = new Button(); 
b.Height = 100; 
b.Width = 100; 

Bitmap bitmap = new Bitmap(b.Height, b.Width); 
Graphics g = Graphics.FromImage(bitmap); 
g.DrawString("Hello World", new Font(FontFamily.GenericSerif, 5), Brushes.Red, new PointF(1, 1)); 

b.Image = bitmap; 
this.Controls.Add(b); 
+0

为什么不使用'b.Text'设置按钮的文本,而不是使用图形对象的? – 3aw5TZetdf 2012-08-15 21:11:33

+0

@MatthewRz根本没有帮助。 – carny666 2012-08-15 21:12:58

+0

@MatthewRz再次没有帮助。我不想要文字。这只是一个例子。 – carny666 2012-08-15 21:13:46

回答

0

使用Button Image属性将图像设置到按钮上。

Button b = new Button(); 
Bitmap picture = new Bitmap(); 
b.Image = picture;