2012-09-28 220 views
1

早安社区VB.net绘制矩形形式

我想画一个矩形正是在形式的中心的中心。另外,我想在这个矩形下画一些文字。

随着我认为我没有问题的文本,我使用下面的代码:

Dim sf As New StringFormat 
     sf.LineAlignment = StringAlignment.Center 
     sf.Alignment = StringAlignment.Center 

     ' Line with the problem 
     e.Graphics.FillRectangle(Brushes.Beige, CInt(Local_Form.Width/2), CInt(Local_Form.Height/2), 200, 100) 

     e.Graphics.DrawString(Local_Text, _ 
           New Font(MyCloud.Settings.Settings_Forms.Font.Name, 30), _ 
           Brushes.GreenYellow, _ 
           Local_Form.Width/2, Local_Form.Height/2, sf) 

enter image description here

但是,我有一个矩形的问题。有人可以帮助我吗?

回答

3

有两件事情,第一件是你将你的矩形的左上角设置到中心,你需要从你的顶部和左边位置中减去一半的宽度和一半的高度。另外,您应该使用ClientRectangle来获取没有Chrome的实际工作表面。

e.Graphics.FillRectangle(Brushes.Beige, CInt(Local_Form.ClientRectangle.Width/2) - 100, CInt(Local_Form.ClientRectangle.Height/2) - 50, 200, 100) 
+0

完美!,谢谢马克......很可爱。 我有另一个相关的问题:我可以计算字符串的宽度?我想根据字符串调整矩形的大小。 – MiBol

+1

@MiBol使用e.graphics.measurestring –