VB 來 立 李 龍老 年 參 車 令 度 綠 車 不 不 靈 了 來 令來 了老 利 來 練 念 邏 念 數 度 念 狀 不 度 令 數 更 參 VB VB VB 理 VB 類 數 (x,y) (0,0) x y x,y 數 (0,0) (x, 0) (0, y) (x, y) VB 裡 來 VB 來 1
Graphics VB Graphics Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics g Graphics 數 g g = Me.CreateGraphics Me( Form1) Graphics g g Form1 來 g.drawline(pens.black, 10, 10, 20, 20) g (10,10) (20,20) g.dispose( ) g 行 練 Graphics 例 RadioButton VB 來 2
RadioButton1 (Name) rbtred Text RadioButton2 (Name) rbtgreen Text 綠 RadioButton3 (Name) rbtblue Text 藍 列數 1 2 3 4 5 6 7 8 9 10 11 12 13 Dim Down As Boolean Private Sub Form1_MouseDown( ) Handles Me.MouseDown Down = True Private Sub Form1_MouseUp( ) Handles Me.MouseUp Down = False Private Sub Form1_MouseMove( ) Handles Me.MouseMove If Down Then Dim Color If rbtred.checked Then Color = Pens.Red ElseIf rbtgreen.checked Then VB 來 3
14 15 16 17 18 19 20 21 22 23 24 Color = Pens.Green Else Color = Pens.Blue End If Dim g As Graphics g = Me.CreateGraphics g.drawline(color, e.x, e.y, e.x + 1, e.y + 1) g.dispose( ) End If 說 1 列 Boolean 數 Down 錄 滑 False 2~4 列 滑 理 Down True 5~7 列 滑 理 Down False 8~24 列 滑 理 若滑 了 Down True If If 來 綠 藍 不 狀 Color 數 19~22 列 滑 19 列 g Graphics 20 列 Me Form1 來 Graphics g g 便 Graphics 21 列 g DrawLine 來 e.x 滑 X e.y 滑 Y 5 參數 X Y X Y 22 列 Dispose g 行 VB 來 4
練 Graphics 2 說 y = x 數 ( ) VB ( ) 異 (0,0) (0,0) (a,b) Step1 (a, b) (x, y) VB (a+x, b+y) Step2 VB y 數 y y (a+x, b y) a, b 例 Button VB 來 5
Button1 Text 列數 1 2 3 4 5 6 7 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics Dim a As Integer = Me.ClientSize.Width \ 2 Dim b As Integer = Me.ClientSize.Height \ 2 Dim x, y As Integer Dim lastx, lasty As Integer VB 來 6
8 9 10 11 12 13 14 15 16 17 g.drawline(pens.red, 0, b, 2 * a, b) ' X g.drawline(pens.red, a, 0, a, 2 * b) ' Y For x = a To a ' Y=X^2 y = x ^ 2 If x > a Then g.drawline(pens.black, lastx + a, b lasty, x + a, b y) lastx = x lasty = y Next g.dispose( ) 說 2 列 g Graphics Me(Form1 ) CreateGraphics 來 3 4 列 a b 度 度 裡 Me.ClientSize 度 (Width) 度 (Height) Me.ClientSize 列 Me.Size Me.ClientSize.Width Me.Size.Height Me.ClientSize.Height Me.Size.Width 5 6 列 x y 數 數來 lastx lasty 數 數 來 留 x y 8 9 列 X Y 10~15 列 x a a 數 11 列 x y 12 列 x a 不 1 x a lastx lasty VB 來 7
來 便 (lastx, lasty) (x,y) 連 y=x 2 16 列 Graphics g 行 例 若 復 便 了 Paint 行 1. Me.Paint 理 Form1 (Form1 ) Paint VB 來 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 2. 列 Me.CreateGraphics e.graphics Paint 理 Graphics 不 Form1 Graphics Private Sub Form1_Paint( ) Handles Me.Paint Dim g As Graphics = e.graphics Dim a As Integer = Me.ClientSize.Width \ 2 Dim b As Integer = Me.ClientSize.Height \ 2 Dim x, y As Integer Dim lastx, lasty As Integer g.drawline(pens.red, 0, b, 2 * a, b) g.drawline(pens.red, a, 0, a, 2 * b) For x = a To a y = x ^ 2 If x > a Then g.drawline(pens.black, lastx + a, b lasty, x + a, b y) lastx = x lasty = y Next e.graphics.dispose( ) Graphics DrawLine 數 來 說 VB 數 類 VB 來 9
1. DrawLine : g.drawline(pen, x1, y1, x2, y2) (x1,y1) (x2,y2) 說 DrawLine 數 來 (x1,y1) (x2,y2) Pen Pens 例 Pens.Blue 藍 Pens.Black 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.drawline(pens.blue, 80, 50, 200, 100) g.dispose( ) (80,50) (200,100) 行 2. DrawRectangle g.drawrectangle (Pen, x1, y1, 度, 度 ) 度 (x1,y1) 度 VB 來 10
例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.drawrectangle(pens.blue, 20, 30, 100, 80) g.dispose( ) (20, 30) 100 80 行 類 1. DrawEllipse g.drawellipse (Pen, x1, y1, 度, 度 ) 度 (x1,y1) 度 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.drawellipse(pens.blue, 20, 30, 100, 80) g.dispose( ) VB 來 11
切 (20, 30) 100 80 行 2. DrawArc g.drawarc (Pen, x1, y1, 度, 度, 度, 度 ) 度 度 不 了 度 度 兩 度 0~359 若 0 度 數 X 度 度 90 度 了 1/4 180 度 度 度 270 度 (x1,y1) 度 度 180 度 0 度 例 度 0 度 90 90 度 270 度 180 度 0 度 90 度 VB 來 12
例 度 270 度 90 270 度 180 度 0 度 90 度 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.drawarc(pens.blue, 20, 30, 100, 80, 45, 90) g.drawarc(pens.blue, 20, 30, 100, 80, 45, 90) g.dispose( ) 兩 45~135 度 225~315 度 ( 135~ 45 ) 切 (20, 30) 100 80 行 3. DrawPie VB 來 13
g.drawpie (Pen, x1, y1, 度, 度, 度, 度 ) 令 了 例 度 0 度 90 270 度 180 度 0 度 90 度 例 度 270 度 90 270 度 180 度 0 度 90 度 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.drawpie(pens.blue, 20, 30, 100, 80, 45, 90) g.drawpie(pens.blue, 20, 30, 100, 80, 45, 90) g.dispose( ) 兩 45~135 度 225~315 度 ( 135~ 45 ) 切 (20, 30) 100 80 了 了 行 VB 來 14
練 DrawEllipse DrawLine 數 例 例不 例不 列數 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Dim Turn As Integer = 1 Dim w, h As Integer Private Sub Form1_Paint( ) Handles Me.Paint w = Me.ClientSize.Width \ 3 h = Me.ClientSize.Height \ 3 Dim g As Graphics = Me.CreateGraphics g.drawline(pens.gray, 0, h, w * 3, h) g.drawline(pens.gray, 0, h * 2, w * 3, h * 2) g.drawline(pens.gray, w, 0, w, h * 3) g.drawline(pens.gray, w * 2, 0, w * 2, h * 3) g.dispose( ) Private Sub Form1_MouseClick( ) Handles Me.MouseClick Dim g As Graphics = Me.CreateGraphics Dim x1, y1 As Integer VB 來 15
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 If e.x < w Then 'e.x 滑 X x1 = 0 ElseIf e.x < w * 2 Then x1 = w Else x1 = w * 2 End If If e.y < h Then 'e.y 滑 Y y1 = 0 ElseIf e.y < h * 2 Then y1 = h Else y1 = h * 2 End If If Turn = 1 Then g.drawellipse(pens.blue, x1, y1, w, h) ' Turn = 2 Else g.drawline(pens.red, x1, y1, x1 + w, y1 + h) ' g.drawline(pens.red, x1 + w, y1, x1, y1 + h) ' Turn = 1 End If 說 1 列 Turn 數來 錄 O X 1 2 2 列 w h 1/3 1/3 4~13 列 Form1 Paint 5 6 列 w h 數 Me.ClientSize.Width \ 3 1/3 \ 數 數 理 Me.ClientSize.Height \ 3 1/3 VB 來 16
8~11 列 兩 兩 (w,0) (2w,0) (0,h) (3w, h) (0,2h) (3w,2h) (w,3h) (2w,3h) 12 列 Dispose g 14~39 列 x1 y1 滑 e.x e.y 17~23 列 滑 X If 0~w 欄 令 x1=0 w~2w 欄 令 x1=w 2w~3w 欄 令 x1=2*w 24~30 列 滑 Y If 0~h 列 令 y1=0 h~2h 列 令 y1=h 2w~3w 欄 令 y1=2*h 31~38 列 輪 O X Turn 錄 1 輪 O DrawEllipse (x1,y1) Turn 2 Turn 2 輪 X 兩 DrawLine X 行 VB 來 17
DrawRectangle( ) DrawEllipse( ) DrawPie( ) Fill FillRectangle FillEllipse FillPie Pen Brush Brush Pen Brush 來 Pen 1. FillRectangle g.fillrectangle (Brush, x1, y1, 度, 度 ) 度 (x1,y1) 度 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.fillrectangle(brushes.blue, 20, 30, 100, 80) g.dispose( ) (20, 30) 100 80 行 VB 來 18
2. FillEllipse g.fillellipse (Brush, x1, y1, 度, 度 ) 度 (x1,y1) 度 例 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.fillellipse(brushes.blue, 20, 30, 100, 80) g.dispose( ) 切 (20, 30) 100 80 行 3. FillPie g.fillpie (Brush, x1, y1, 度, 度, 度, 度 ) 例 度 0 度 90 270 度 180 度 0 度 VB 來 19 90 度
例 度 270 度 90 270 度 180 度 0 度 例 90 度 Private Sub Button1_Click( ) Handles Button1.Click Dim g As Graphics = Me.CreateGraphics g.fillpie(brushes.blue, 20, 30, 100, 80, 45, 90) g.fillpie(brushes.blue, 20, 30, 100, 80, 45, 90) g.dispose( ) 兩 45~135 度 225~315 度 ( 135~ 45 ) 切 (20, 30) 100 80 行 練 例 FillPie 數 VB 來 20
Button TextBox Label Form1 Text Button1 Text Button2 Text Button3 Text Label1 Text 0.00% BackColor Blue Label2 Text 0.00% BackColor Red Label3 Text 0.00% BackColor Yellow VB 來 21
列數 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Dim vote(2) As Integer Dim rate(2) As Single Private Sub Button_Click(ByVal sender As System.Object, ) Handles _ Button1.Click, Button2.Click, Button3.Click If (sender.equals(button1)) Then vote(0) += 1 ElseIf (sender.equals(button2)) Then vote(1) += 1 Else vote(2) += 1 End If Dim sum As Integer = 0 For i As Integer = 0 To 2 sum += vote(i) Next For i As Integer = 0 To 2 rate(i) = vote(i) / sum Next Dim g As Graphics = Me.CreateGraphics VB 來 22
21 22 23 24 25 26 27 28 29 30 31 32 33 34 g.fillpie(brushes.blue, 50, 10, 150, 150, 0, 360 * rate(0)) g.fillpie(brushes.red, 50, 10, 150, 150, 360 * rate(0), 360 * rate(1)) g.fillpie(brushes.yellow, 50, 10, 150, 150, 0, 360 * rate(2)) g.drawpie(pens.black, 50, 10, 150, 150, 0, 360 * rate(0)) g.drawpie(pens.black, 50, 10, 150, 150, 360 * rate(0), 360 * rate(1)) g.drawpie(pens.black, 50, 10, 150, 150, 0, 360 * rate(2)) g.dispose( ) TextBox1.Text = vote(0) TextBox2.Text = vote(1) TextBox3.Text = vote(2) Label1.Text = Format(rate(0), "0.00%") Label2.Text = Format(rate(1), "0.00%") Label3.Text = Format(rate(2), "0.00%") 說 1 列 vote 數 列 數 2 列 rate 數 列 率 3~34 列 Button1 Button2 Button3 理 3 4 列 Handles Button1.Click Button2.Click Button3.Click 5~11 列 Button 略 sender 3 列 理 sender 例 Click Button sender Button 利 sender.equals( ButtonX ) 來 sender ButtonX 了 True 不 False 例 了 Button1 vote(0) 1 14~16 列 數 17~19 列 [ 數 ]/[ 數 ] 3 率 21~23 列 行 150*150 50, 10 VB 來 23
Vote(2) 0 度 Vote(1) Vote(0) 21 列 藍 0 度, vote(0) 度數 (360 度 * 率 ) 來 22 列 vote(0) 度 度數 23 列 0 度, 360 24~26 列 參數 DrawPie 數 3 28~33 列 數 率 31~33 列 Format 數 料 來 例 Format(0.3516, "0.00%") 0.3516 100 35.16% 串 行 10 8 16 VB 來 24
練 ( )1. VB 列 (a) Graphics (b) Images (c) Pictures (d) Drawer ( )2. g Graphics 列 g.dispose (a) (b) Dispose Graphics (c) 行 g.drawrectangle (d) 數 g 行 g.dispose ( )3. g Graphics g.drawpie(pens.black, 0, 0, 20, 20, 150, 270) (a) (b) (c) (d) ( )4. VB (a) Y (b) Y VB 來 25
(c) Y (d) Y ( )5. Dim x as Double = 0.875 Format(x, 0.00% ) (a) 0.88% (b) 0.87% (c) 88.00% (d) 87.50% ( )6. W, H (x,y) 列 X Y (a) (x,y) = (x + W/2, y + H/2) (b) (x,y) = (x + W/2, H/2 y) (c) (x,y) = (x W/2, H/2 y) (d) (x,y) = (W/2 x, y + H/2) Graphics VB 參 行 令 VB 列 樂 VB VB 念 邏 念 數 度 念 了 行 VB 來 26
參 路 VB 來 27