Run ? 這是什麼東東

在看書的過程中,好幾次看到"run"這個東西,還滿有趣的,可以將文字切割開來,而且各自擁有其對應的"事件",簡單測試一下當滑鼠移動到字的上方改變字的顏色以及大小,執行結果像是這樣

下面就來看看測試的程式碼吧
Class Window1
Dim sp As StackPanel
Dim txtBlock As TextBlock

Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
sp = New StackPanel
Me.Content = sp
txtBlock = New TextBlock
txtBlock.Height = 300
txtBlock.Width = 300
txtBlock.HorizontalAlignment = Windows.HorizontalAlignment.Center
txtBlock.Background = Brushes.AliceBlue
txtBlock.FontSize = 32
sp.Children.Add(txtBlock)
Dim strArr() As String = {"T", "H", "I", "S", " ", "A", " ", "B", "O", "O", "K"}
For Each s As String In strArr
Dim runTmp As New Run(s)
runTmp.TextDecorations = New TextDecorationCollection
txtBlock.Inlines.Add(runTmp)
txtBlock.Inlines.Add(" ")
AddHandler runTmp.MouseEnter, AddressOf Run_OnMouseEnter
AddHandler runTmp.MouseLeave, AddressOf Run_OnMouseLeave
Next
AddHandler txtBlock.MouseDown, AddressOf TxtBlock_OnClick
End Sub

Protected Sub TxtBlock_OnClick(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim tmpR As Run
tmpR = CType(e.Source, Run)
If tmpR.FontStyle = FontStyles.Italic Then
tmpR.FontStyle = FontStyles.Normal
Else
tmpR.FontStyle = FontStyles.Italic
End If
End Sub

Protected Sub Run_OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
CType(e.Source, Run).FontSize = 48
CType(e.Source, Run).Foreground = Brushes.DeepPink
End Sub

Protected Sub Run_OnMouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
CType(e.Source, Run).FontSize = 32
CType(e.Source, Run).Foreground = Me.Foreground
End Sub
End Class

留言

這個網誌中的熱門文章

開啟cshtml檔案時,出現『並未將物件參考設定為物件執行個體』的錯誤訊息

無法設定中斷點 尚未載入符號檔

DataGridView欄位計算總合