最佳解答者
vb2008請教如何在不規則圖形上填色

問題
解答
-
可以用這種方法:
試試看囉
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
或是:
Dim p1 As New Point(100, 50)
Dim p2 As New Point(10, 80)
Dim p3 As New Point(160, 90)
Dim p4 As New Point(100, 50)
Dim ps(3) As Point
ps(0) = p1
ps(1) = p2
ps(2) = p3
ps(3) = p4
Dim g As Graphics = Me.CreateGraphics
g.FillPolygon(Brushes.Black, ps)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p1 As New Point(100, 50) Dim p2 As New Point(10, 80) Dim p3 As New Point(160, 90) Dim p4 As New Point(100, 50) Dim ps(3) As Point ps(0) = p1 ps(1) = p2 ps(2) = p3 ps(3) = p4 Dim g As Graphics g = Me.PictureBox1.CreateGraphics() g.FillPolygon(Brushes.Black, ps) End Sub
這種方法是先宣告一個陣列,包含4個點,再用FillPolygon方法填滿圖形
其中點和顏色可以自行決定
如果是曲線,可以改用FillColsedCurve
注意:如果不在PAINT事件寫程式,視窗最小化了以後圖形就會消失
如果你對這方面沒有興趣,那就不要強迫自己做這方面的事. -
參考MSDN
GraphicsPath
http://msdn.microsoft.com/zh-tw/library/system.drawing.drawing2d.graphicspath.addpath.aspx
Graphics.FillPath
http://msdn.microsoft.com/zh-tw/library/system.drawing.graphics.fillpath.aspx
for examplePrivate Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint ' Creates a symetrical triangle and adds an inverted triangle. ' Create the first path - right side up triangle. Dim myArray As Point() = {New Point(30, 30), New Point(60, 60), _ New Point(0, 60), New Point(30, 30)} Dim myPath As New GraphicsPath myPath.AddLines(myArray) ' Create the second path - inverted triangle. Dim myArray2 As Point() = {New Point(30, 30), New Point(0, 0), _ New Point(60, 0), New Point(30, 30)} Dim myPath2 As New GraphicsPath myPath2.AddLines(myArray2) ' Add the second path to the first path. myPath.AddPath(myPath2, True) ' Draw the combined path to the screen. Dim myPen As New Pen(Color.Black, 2) e.Graphics.FillPath(Brushes.Red, myPath) End Sub
請注意討論區相關規則,良好的討論環境需要大家共同努力、遵守
所有回覆
-
可以用這種方法:
試試看囉
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
或是:
Dim p1 As New Point(100, 50)
Dim p2 As New Point(10, 80)
Dim p3 As New Point(160, 90)
Dim p4 As New Point(100, 50)
Dim ps(3) As Point
ps(0) = p1
ps(1) = p2
ps(2) = p3
ps(3) = p4
Dim g As Graphics = Me.CreateGraphics
g.FillPolygon(Brushes.Black, ps)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p1 As New Point(100, 50) Dim p2 As New Point(10, 80) Dim p3 As New Point(160, 90) Dim p4 As New Point(100, 50) Dim ps(3) As Point ps(0) = p1 ps(1) = p2 ps(2) = p3 ps(3) = p4 Dim g As Graphics g = Me.PictureBox1.CreateGraphics() g.FillPolygon(Brushes.Black, ps) End Sub
這種方法是先宣告一個陣列,包含4個點,再用FillPolygon方法填滿圖形
其中點和顏色可以自行決定
如果是曲線,可以改用FillColsedCurve
注意:如果不在PAINT事件寫程式,視窗最小化了以後圖形就會消失
如果你對這方面沒有興趣,那就不要強迫自己做這方面的事. -
參考MSDN
GraphicsPath
http://msdn.microsoft.com/zh-tw/library/system.drawing.drawing2d.graphicspath.addpath.aspx
Graphics.FillPath
http://msdn.microsoft.com/zh-tw/library/system.drawing.graphics.fillpath.aspx
for examplePrivate Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint ' Creates a symetrical triangle and adds an inverted triangle. ' Create the first path - right side up triangle. Dim myArray As Point() = {New Point(30, 30), New Point(60, 60), _ New Point(0, 60), New Point(30, 30)} Dim myPath As New GraphicsPath myPath.AddLines(myArray) ' Create the second path - inverted triangle. Dim myArray2 As Point() = {New Point(30, 30), New Point(0, 0), _ New Point(60, 0), New Point(30, 30)} Dim myPath2 As New GraphicsPath myPath2.AddLines(myArray2) ' Add the second path to the first path. myPath.AddPath(myPath2, True) ' Draw the combined path to the screen. Dim myPen As New Pen(Color.Black, 2) e.Graphics.FillPath(Brushes.Red, myPath) End Sub
請注意討論區相關規則,良好的討論環境需要大家共同努力、遵守