前兩篇談到矩陣的運算
這篇來談些現成的東西, .net繪圖的時候就內建圖形運算功能了
來畫個會走的傳統時鐘看看
畫面上要先佈置三個按鈕, 一個picturebox
按紐1中加入:
Dim a
As Integer
Dim G
As Graphics=
Me.PictureBox1.creategraphics

G.TranslateTransform(
Me.PictureBox1.Width \ 2,
Me.PictureBox1.Height \ 2)

G.RotateTransform(-90)
For a = 0
To 59
If a
Mod 5 = 0
Then 
G.DrawLine(
New Pen(Color.Blue, 5), 220, 0, 250, 0)
Else 
G.DrawLine(Pens.AntiqueWhite, 235, 0, 250, 0)
End If 
G.RotateTransform(6)
Next 
G.RotateTransform(90)

G.TranslateTransform(-
Me.PictureBox1.Width \ 2, -
Me.PictureBox1.Height \ 2)
裡面先利用TranslateTransform轉換繪圖起點到畫面中間, 再利用RotateTransform 旋轉負90度
從12點整開始畫線, 每格五條劃一條粗線, 用來表示小時的部份
最後畫完要記得轉換座標回原點
第二個按鈕中加入
Dim a
As Integer
Dim G
As Graphics=
Me.PictureBox1.CreateGraphics
Dim 角度
As Integer = -60
Dim TX, TY
As Integer
For a = 1
To 12

TX =
Me.PictureBox1.Width / 2 + 265 * Math.Cos(角度 * 3.14 / 180) - 10

TY =
Me.PictureBox1.Height / 2 + 265 * Math.Sin(角度 * 3.14 / 180) - 11

G.DrawString(a.ToString,
New Font(
"新細明體", 20, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, TX, TY)

角度 = (角度 + 30)
Mod 360
Next利用圓角座標計算出每一個小時的數字, 顯示出來, 這部分使用到的數學公式就請自行查詢高中國中的數學課本囉
第三個按鈕中加入

時 = Now.Hour
Mod 12
' 讀取新的時間,畫上新的線 
分 = Now.Minute

秒 = Now.Second
Dim G
as Graphics=
me.Picturebox1.creategraphics

G.ResetTransform()

G.TranslateTransform(
Me.PictureBox1.Width \ 2,
Me.PictureBox1.Height \ 2)

G.RotateTransform(-90 + 時 * 30)

G.DrawLine(
New Pen(Color.AntiqueWhite, 5), 0, 0, 100, 0)

G.RotateTransform(90 - 時 * 30)

G.RotateTransform(-90 + 分 * 6)

G.DrawLine(
New Pen(Color.Pink, 3), 0, 0, 150, 0)

G.RotateTransform(90 - 分 * 6)

G.RotateTransform(-90 + 秒 * 6)

G.DrawLine(
New Pen(Color.Orange, 2), 0, 0, 200, 0)

G.RotateTransform(90 - 秒 * 6)
最後在全域變數區加入三個變數
Dim 時
As Integer
Dim 分
As Integer
Dim 秒
As Integer這樣就可以畫出一個時鐘了, 經過一些調整和加上圖形, 這可以變的很美喔
要比windows7的美也不難哩, 但....似乎不會動?!
這是因為我希望分段介紹繪圖座標轉換的動作, 重點不在時鐘
如果想要動, 那就是把第三個按鈕的東西加入到timer物件中, enabled它, 就會動了
順著天賦做事,逆著個性做人生命, 就該浪費在美好的事物上