![](/img/trans.png)
[英]Get paired list of name and type from System.Drawing.Brushes
[英]Creating filled Squares inside Picturboxes with customly made brushes and extracted from a list
所以為了解釋我的問題是什么,我制作了一個 PictureBox,我需要在其中填充許多 FILLED 方塊。 但是,為此我需要創建一個畫筆,我在網上找到的所有解決方案都被 Visual Studio 2019 作為錯誤返回。我不知道該怎么做了。
這是畫筆聲明的示例:
SolidBrush shadowBrush = new SolidBrush(customColor) (returns error)
Brush randomBrush = new brush(customColor) (returns error)
按照 GDI+ 繪圖的方式,您應該將表示繪圖的所有數據存儲在一個或多個字段中,然后在適當控件的Paint
事件處理程序中讀取該數據以進行繪圖。 在您的情況下,您需要信息來表示一個正方形及其繪制的顏色,並且您需要其中的多個。 在這種情況下,您應該定義一個具有Rectangle
屬性和Color
屬性的類型,並存儲該類型的通用List
。 然后您可以遍歷該列表,創建一個帶有Color
的SolidBrush
並調用FillRectangle
。
Public Class Form1
Private ReadOnly boxes As New List(Of Box)
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
For Each box In boxes
Using b As New SolidBrush(box.Color)
e.Graphics.FillRectangle(b, box.Bounds)
End Using
Next
End Sub
End Class
Public Class Box
Public Property Bounds As Rectangle
Public Property Color As Color
End Class
現在,要添加一個正方形,您只需創建一個新的Box
object,將其添加到List
,然后對PictureBox
調用Invalidate
。 為簡單起見,您可以在沒有 arguments 的情況下調用Invalidate
,整個PictureBox
將被重新繪制。 如果您可以指定已更改或可能已更改的區域,則更好,因為這樣可以將緩慢的重繪部分保持在最低限度。 由於您已經有一個描述已更改區域的Rectangle
,因此您可以傳遞它,例如
Dim boxBounds As New Rectangle(10, 10, 100, 100)
boxes.Add(New Box With {.Bounds = boxBounds, .Color = Color.Black})
PictureBox1.Invalidate(boxBounds)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.