Public?a(8)?As?OvalShape
創(chuàng)新互聯(lián)建站專注于袁州企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。袁州網(wǎng)站建設(shè)公司,為袁州等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
Dim?canvas=?New?Microsoft.VisualBasic.PowerPacks.ShapeContainer
ConstCircleSize?As?Integer=?20
Const?Dist?As?Integer?=?10
Const?Gap?As?Integer?=?50
Dim?i?As?Integer
canvas.Parent?=?Me.Panel1
For?i?=0?To?8
a(i)?=?NewOvalShape
a(i).Size?=?New?Size(CircleSize,CircleSize)
a(i).Location?=?New?Point(Dist?+Gap?*?(i?Mod?3),?Dist?+?Gap?*?(i?\?3))
a(i).BorderWidth?=?3
a(i).Enabled?=?True
a(i).Visible?=?True
a(i).Parent?=?canvas
Next
Dim myGraphics As Graphics = Me.CreateGraphics '聲明并創(chuàng)建一個(gè)Graphics對(duì)象
Dim myPen As Pen = New Pen(Drawing.Color.Black, 3) '聲明一個(gè)畫筆,并設(shè)定顏色和粗細(xì)
myPen.DashStyle = Drawing.Drawing2D.DashStyle.Solid '線型,Solid是實(shí)線
myGraphics.DrawEllipse(myPen, 200, 200, 100, 100) '畫圓,數(shù)值依次是:橫坐標(biāo)、縱坐標(biāo)、寬度和高度(寬高相同為正圓,否則為橢圓)
myGraphics.Dispose() '釋放Graphics占用的資源
在PictureBox1上畫紅色的實(shí)心圓:
Private Sub DrawCircle(ByVal cp As Point, ByVal radius As Integer, ByVal color As Brush)
Dim gr As Graphics
gr = PictureBox1.CreateGraphics
Dim rect As Rectangle = New Rectangle(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius)
gr.DrawEllipse(Pens.Black, rect)
gr.FillEllipse(color, rect)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DrawCircle(New Point(120, 100), 80, Brushes.Red)
End Sub
窗體上添加一個(gè)按鈕,在該按鈕的單擊事件里編寫代碼如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'聲明窗體的Me.CreateGraphics對(duì)象
Dim MyGraphics As Graphics = Me.CreateGraphics
Dim Cx As Integer = 150 '圓心的X坐標(biāo)
Dim Cy As Integer = 150 '圓心的Y坐標(biāo)
Dim R As Integer '圓的半徑
'繪制半徑為R的圓
For R = 5 To 100 Step 5
MyGraphics.DrawEllipse(Pens.Black, New Rectangle(Cx - R, Cy - R, 2 * R, 2 * R))
Next
End Sub
‘用黑色畫筆繪制一組同心圓,半徑從5開始,增量為5。
說明:
DrawEllipse是VB.Net的Graphics類的繪制橢圓的方法;他有幾種格式,上面使用的是一種;
DrawEllipse(畫筆的顏色,繪制橢圓所需要的矩形區(qū)域)
其中:繪制橢圓所需要的矩形區(qū)域,如果被定義為正方形,就演變成繪制圓,定義該區(qū)域由死個(gè)數(shù)值確定,第1個(gè)數(shù)值,確定該區(qū)域左上角的X坐標(biāo),第2個(gè)數(shù)值,確定該區(qū)域左上角的Y坐標(biāo),第3個(gè)數(shù)值,確定該區(qū)域的寬度,第4個(gè)數(shù)值,確定該區(qū)域的高度。
例如1:
DrawEllipse(Pens.Black, New Rectangle(150, 150, 50, 50))
就是以圓心坐標(biāo)為(100,100),繪制半徑為50 的圓。其實(shí)在VB.NET中,是告訴系統(tǒng)在以左上角坐標(biāo)(150,150),邊長(zhǎng)為50的正方形里繪制內(nèi)切圓。理解了是在正方形里繪制內(nèi)切圓,就可以通過數(shù)學(xué)計(jì)算,知道如何繪制了。
同理例如2:
DrawEllipse(Pens.Black, New Rectangle(150, 150, 100, 50))
就是以圓心坐標(biāo)為(100,100),繪制半徑為50 的圓。其實(shí)在VB.NET中,是告訴系統(tǒng)在以左上角坐標(biāo)(150,150),長(zhǎng)軸為100,短軸為50的內(nèi)切橢圓。