2012-10-31 42 views
0

的左上部分這裏是我的代碼的DrawString只適用於形式vb.net

Public Class Form1 

Public MyFormObject As Graphics = Me.CreateGraphics 
Public objFont = New System.Drawing.Font("arial", 20) 
Public a, b As Integer 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Randomize() 
    For i = 1 To 10 
     a = CInt(Int(Rnd() * Me.Width)) 
     b = CInt(Int(Rnd() * Me.Height)) 
     MyFormObject.DrawString("text", objFont, System.Drawing.Brushes.Black, a, b) 
    Next 
End Sub 
End Class 

正如你所看到的,我有一個按鈕,繪製字符串「文本」隨機形式的10倍。我的問題是,它只會在窗體的左上角繪製字符串,大約260x260從0,0開始。如果超出範圍,它會切斷文本。爲什麼是這樣?它應該不適用於整個表單嗎?

回答

1

您需要將CreateGraphics移動到您的子文件夾中。從Microsoft's documentation

的Graphics對象,你可以通過的createGraphics 方法通常不應當前的Windows 消息已被處理後保留取回,因爲任何與該對象 將與下一個WM_PAINT消息被刪除畫。 因此,您不能 緩存Graphics對象以供重用

Public Class Form1 

    Public objFont = New System.Drawing.Font("arial", 20) 
    Public a, b As Integer 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

     Dim MyFormObject As Graphics = Me.CreateGraphics 

     Randomize() 
     For i = 1 To 10 
      a = CInt(Int(Rnd() * Me.Width)) 
      b = CInt(Int(Rnd() * Me.Height)) 
      MyFormObject.DrawString("text", objFont, System.Drawing.Brushes.Black, a, b) 
     Next 

     MyFormObject.Dispose 

    End Sub 

End Class 
+0

謝謝soooo多了,這讓我感到困擾不已。 – WillumMaguire

+0

@WillumMaguire沒問題 – K3N