摘要:WPF學習日誌 第一彈 ~Vary the background~
最近要開始K書,練習WPF相關的東西,書是K這本
雖然Code的部分都是C#不過應該是不會差太多的~剛開始沒多久,按照書上給的範例練習了一下,順便把練習的程式碼貼出來,想看看WPF的朋友也可以測看看。
首先當然要先建立我們的環境,如果你還沒有安裝.Net framework3.0的話可以參考這一篇。
設定好環境之後,建立一個新的專案(這邊我還是用VS2005+延伸套件來做)
之後會看到一個空空的Form,可以看看左邊的工具箱,這時候跟一般做WIndows Form應用程式的時候已經是不一樣了~接下來在方案總管的地方選擇View Code
換到程式碼檢視的部分貼上下面的程式碼(這個範例主要是當滑鼠移動的時候會改變背景的顏色,越靠近中心點顏色會越偏白色)
' Interaction logic for Window1.xaml
Partial Public Class Window1
Inherits System.Windows.Window
Dim Brush As New SolidColorBrush(Colors.Black)
Public Sub New()
InitializeComponent()
End Sub
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Me.Title = "Vary the background"
Me.Background = Brush
End Sub
Private Sub Window1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove
Dim H As Double = ActualHeight - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight
Dim W As Double = ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth
Dim ptMouse As New Point
ptMouse = e.GetPosition(Me)
Dim ptCenter As New Point(W / 2, H / 2)
Dim vectMouse As Vector = ptMouse - ptCenter
Dim Angle As Double = Math.Atan2(vectMouse.Y, vectMouse.X)
Dim vectEllipse As New Vector(W / 2 * Math.Cos(Angle), H / 2 * Math.Sin(Angle))
Dim byLevel As Byte = (255 * (1 - Math.Min(1, vectMouse.Length / vectEllipse.Length)))
Dim Clr As Color = Brush.Color
Clr.R = byLevel
Clr.G = byLevel
Clr.B = byLevel
Brush.Color = Clr
End Sub
End Class