2012-09-28 218 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 –