0

我正在尝试获取 Segoe UI 符号字体的所有字符。

我得到了它们,转换为字符,转换为十六进制值并作为项目添加到列表视图中。

因此,其他人可以将其用于 XAML 项目的十六进制值用作图标。

但问题是代码中的这个:我总是OverFlowException在使用函数Convert.ToChar

代码运行正确,但是当索引变量大于最大字符值 65535 时,我得到了溢出异常。

但是如果你运行代码,你会看到,在 Segoe UI Symbol fontfamily 中有更多大于 65535 的字符。

也许我的方法是错误的,你可以建议我另一种方法。

MainWindow.xaml 文件:

<Grid Loaded="Grid_Loaded">
    <ListView x:Name="listview">
        <ListView.View>
            <GridView> 
                <GridViewColumn Header="HexValue"  />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

MainWindow.xaml.vb 文件

Class MainWindow

    Public glyph As GlyphTypeface
    Dim characterMap As IDictionary(Of Integer, UShort)

    Private Sub Grid_Loaded(sender As Object, e As RoutedEventArgs)
        SymbolleriGetir()
    End Sub

    Public Sub SymbolleriGetir()
        Dim segoeUiSymbol As FontFamily
        For Each font As FontFamily In Fonts.SystemFontFamilies
            Dim fontName As String
            fontName = font.Source
            If fontName = "Segoe UI Symbol" Then
                segoeUiSymbol = font
            End If
        Next

        For Each typeFace As Typeface In segoeUiSymbol.GetTypefaces
            typeFace.TryGetGlyphTypeface(glyph)
            If glyph IsNot Nothing Then
                characterMap = glyph.CharacterToGlyphMap
            Else
                Continue For
            End If
        Next

        For i As Integer = 0 To characterMap.Keys.Count
            Dim index As Integer = characterMap.Keys.ElementAt(i)
            Dim c As Char = Nothing
            c = Convert.ToChar(index)
            Dim charText As String = c.ToString()
            listview.Items.Add(String.Format("&#x{0:x2};", System.Convert.ToUInt32(c)))
        Next

    End Sub
End Class
4

1 回答 1

2

CharacterToGlyphMap 是一个查找映射 (IDictionary(Of Integer, UShort)),其中 UShort 是 unicode 字符,因此无需转换。

我不是 VB 开发人员,但我只是将其编码并测试了它枚举字符,并在每个十六进制值旁边创建一个图像字形:

在此处输入图像描述

机翼:

在此处输入图像描述

您加载的事件处理程序:(由于加载时间,我在 100 后退出)

    Private Sub Grid_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim glyph As GlyphTypeface
        Dim glyphIndex As UShort
        Dim typeface As System.Windows.Media.Typeface = New System.Windows.Media.Typeface("Segoe UI Symbol")
        If (typeface.TryGetGlyphTypeface(glyph)) Then
            Dim glyphLookupMap As IDictionary(Of Integer, UShort) = glyph.CharacterToGlyphMap
            Dim x As Integer = 0
            For Each kvp As KeyValuePair(Of Integer, UShort) In glyphLookupMap
                Dim c As Char = Convert.ToChar(kvp.Value)
                Dim glyphImage As ImageSource = Nothing
                If (glyphLookupMap.TryGetValue(kvp.Key, glyphIndex)) Then
                    glyphImage = Me.CreateGlyph(glyph, glyphIndex, kvp.Value, Brushes.Blue)
                End If
                Me._listview.Items.Add(Me.CreateGlyphListboxEntry(kvp.Key, glyphImage))
                Dim num As Integer = x + 1
                x = num
                If (num > 100) Then
                    Exit For
                End If
            Next
        End If
    End Sub

这里将是 Glyph 图像创建器

    Private Function CreateGlyph(ByVal glyphTypeface As System.Windows.Media.GlyphTypeface, ByVal glyphIndex As UShort, ByVal charUShortVal As UShort, ByVal foreground As Brush) As System.Windows.Media.ImageSource
        Dim imageSource As System.Windows.Media.ImageSource
        Dim flag As Boolean = False
        Dim drawingImage As System.Windows.Media.DrawingImage = Nothing
        Try
            Dim glyphIndexes As IList(Of UShort) = New List(Of UShort)() From
            {
                charUShortVal 
            }
            Dim advanceWidths As IList(Of Double) = New List(Of Double)() From
            {
                glyphTypeface.AdvanceWidths(glyphIndex)
            }
            Dim glyphRun As System.Windows.Media.GlyphRun = New System.Windows.Media.GlyphRun(glyphTypeface, 0, False, 1, glyphIndexes, New Point(0, 0), advanceWidths, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
            drawingImage = New System.Windows.Media.DrawingImage(New System.Windows.Media.GlyphRunDrawing(foreground, glyphRun))
        Catch exception As System.Exception
            imageSource = Nothing
            flag = True
        End Try
        If (Not flag) Then
            imageSource = drawingImage
        End If
        flag = False
        Return imageSource
    End Function

最后是 Listbox Entry 创建者:

Private Function CreateGlyphListboxEntry(ByVal charIntValue As Integer, ByVal glyphImage As ImageSource) As FrameworkElement
    Dim result As StackPanel = New StackPanel() With
    {
        .Orientation = Orientation.Horizontal
    }
    Dim text As TextBlock = New TextBlock() With
    {
        .Text = String.Format("{0:X}", charIntValue),
        .Foreground = Brushes.Black,
        .FontSize = 17,
        .Margin = New Thickness(10, 0, 10, 0)
    }
    result.Children.Add(text)
    If (glyphImage IsNot Nothing) Then
        Dim image As System.Windows.Controls.Image = New System.Windows.Controls.Image()
        Dim num As Double = 32
        Dim num1 As Double = num
        image.Height = num
        image.Width = num1
        image.Stretch = Stretch.Uniform
        image.Source = glyphImage
        result.Children.Add(image)
    End If
    Return result
End Function

希望这可以帮助!

于 2014-02-14T19:30:28.090 回答