2013-08-27 78 views
0

我想繪製一個矩形,該矩形具有黃色和綠色兩種顏色,使用中間帶文字的diaganol分割。如何在使用GDI繪圖時遮擋區域+

問題是我想讓中間的文字變成白色,當它在黃色上方時,它在綠色和黑色之上。

這裏有一個例子:

enter image description here

我可以得出黃色,綠色,黑色或白色的文字,但我怎麼能屏蔽掉文本,以獲得預期的效果?

這裏是到目前爲止我的代碼:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim colour1Brush As New SolidBrush(Color.Yellow) 
    Dim colour2Brush As New SolidBrush(Color.Green) 
    Dim g As Graphics = Me.CreateGraphics 
    Dim rect As New Rectangle(New Point(50, 50), New Size(100, 50)) 
    With g 
     'draw the background yellow 
     .FillRectangle(colour1Brush, rect) 
     'put in the black text 
     DrawText(g, rect, "12", Brushes.Black) 
     'draw the green triangle 
     Dim triPoints(2) As Point 
     triPoints(0) = New Point(rect.Left + rect.Width, rect.Top) 
     triPoints(1) = New Point(rect.Left + rect.Width, rect.Top + rect.Height) 
     triPoints(2) = New Point(rect.Left, rect.Top + rect.Height) 
     .FillPolygon(colour2Brush, triPoints) 
     'now we need white text that doesn't overwrite the existing black text 
     '?????? DrawText(g, rect, "12", Brushes.White) 
    End With 
End Sub 

Private Sub DrawText(ByRef g As Graphics, ByVal rect As Rectangle, ByVal text As String, ByVal brush As Brush) 
    Dim fnt As New Font("Segoe UI", 8) 
    Dim textSize As SizeF = g.MeasureString(text, fnt) 
    Dim x As Integer = CInt(rect.Left + ((rect.Width/2) - (textSize.Width/2))) 
    Dim y As Integer = CInt(rect.Height + ((rect.Height/2) - (textSize.Height/2))) 
    g.DrawString(text, fnt, brush, New Point(x, y)) 
End Sub 

回答

0

我已經設法做到這一點:

  • 創建包含有白色文字綠色的背景顏色的臨時位圖。
  • 以特定顏色繪製一半的三角形
  • 然後使用位圖上的MakeTransparent()移除剛剛創建的三角形。
  • 然後,我將新的位圖覆蓋到原始的正方形上。