none
实现Word选择字体的下拉框时一片空白 RRS feed

  • 问题

  • Dim lypfont As System.Drawing.Text.InstalledFontCollection = New System.Drawing.Text.InstalledFontCollection
        Dim lypname As List(Of String) = New List(Of String)
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
            Dim fontnumber As System.Drawing.FontFamily
            For Each fontnumber In lypfont.Families
                ComboBox1.Items.Add(fontnumber.Name.ToString)
                lypname.Add(fontnumber.Name.ToString)
    
            Next
    
        End Sub
    
    Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
            For i As Integer = 0 To ComboBox1.Items.Count - 1
                Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10)
                e.ItemHeight = lyps.Height
            Next
        End Sub
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
            Dim lypc As Color = e.ForeColor
            Dim lypB As Brush = New SolidBrush(lypc)
            For i As Integer = 0 To ComboBox1.Items.Count - 1
                Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10)
    
                Dim Clocation As PointF
                Clocation = e.Bounds.Location
                e.Graphics.DrawString(ComboBox1.Items.Item(i).ToString, lyps,lypB, Clocation)
                e.DrawBackground()
            Next
    
        End Sub

    不知道哪里有问题啊   ComboBox一片空白  但是滑动ComboBox时隐隐约约还会看见字体名称但是若隐若现..

    


    Visual Basic 初学者 望关照!

    2013年1月24日 13:58

答案

  • Dim lypfont As System.Drawing.Text.InstalledFontCollection = New System.Drawing.Text.InstalledFontCollection Dim lypname As List(Of String) = New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim fontnumber As System.Drawing.FontFamily For Each fontnumber In lypfont.Families ComboBox1.Items.Add(fontnumber.Name.ToString) lypname.Add(fontnumber.Name.ToString) Next Console.WriteLine("") End Sub Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem 'For i As Integer = 0 To ComboBox1.Items.Count - 1 ' Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10) ' e.ItemHeight = lyps.Height ' Console.WriteLine(ComboBox1.Items(i).ToString) ' Console.WriteLine(lyps.Height) 'Next Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(e.Index), 10) e.ItemHeight = lyps.Height End Sub Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem '背景色要提前画 e.DrawBackground() Dim lypc As Color = e.ForeColor '这里要修改掉,因为ForeColor是白色,用它来初始化Brush就是白色字体,就看不到了 Dim lypB As Brush = New SolidBrush(Color.Aqua) 'For i As Integer = 0 To ComboBox1.Items.Count - 1 ' Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10) ' Dim Clocation As PointF ' Clocation = e.Bounds.Location ' e.Graphics.DrawString(ComboBox1.Items.Item(i).ToString, lyps, lypB, Clocation) ' e.DrawBackground() 'Next '这里要修改,原来的是获取不到字体的,但是这里会引发异常- 字体"xx"不支持“regular”

    '可以在使用字体前判断一下,如果不支持,则使用默认字体 Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.Item(e.Index), 10) Dim Clocation As PointF Clocation = e.Bounds.Location Console.WriteLine(lypname.Item(e.Index)) Console.WriteLine(e.Index) e.Graphics.DrawString(ComboBox1.Items.Item(e.Index).ToString, lyps, lypB, e.Bounds, StringFormat.GenericDefault) End Sub



    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月25日 16:08
    版主

全部回复

  • 代码我还没有修改,但是你这里用For循环来绘制Item是不合适的。 这样的话就是每一个Item绘制都会触发一次循环,合理的情况应该是每触发一次只绘制一个Item,请使用DrawItemEventArgsh和MeasureItemEventArgs的Index属性来获取Item索引,根据索引绘制触发这个时间的Item。

    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月25日 15:35
    版主
  • Dim lypfont As System.Drawing.Text.InstalledFontCollection = New System.Drawing.Text.InstalledFontCollection Dim lypname As List(Of String) = New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim fontnumber As System.Drawing.FontFamily For Each fontnumber In lypfont.Families ComboBox1.Items.Add(fontnumber.Name.ToString) lypname.Add(fontnumber.Name.ToString) Next Console.WriteLine("") End Sub Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem 'For i As Integer = 0 To ComboBox1.Items.Count - 1 ' Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10) ' e.ItemHeight = lyps.Height ' Console.WriteLine(ComboBox1.Items(i).ToString) ' Console.WriteLine(lyps.Height) 'Next Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(e.Index), 10) e.ItemHeight = lyps.Height End Sub Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem '背景色要提前画 e.DrawBackground() Dim lypc As Color = e.ForeColor '这里要修改掉,因为ForeColor是白色,用它来初始化Brush就是白色字体,就看不到了 Dim lypB As Brush = New SolidBrush(Color.Aqua) 'For i As Integer = 0 To ComboBox1.Items.Count - 1 ' Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.IndexOf(i), 10) ' Dim Clocation As PointF ' Clocation = e.Bounds.Location ' e.Graphics.DrawString(ComboBox1.Items.Item(i).ToString, lyps, lypB, Clocation) ' e.DrawBackground() 'Next '这里要修改,原来的是获取不到字体的,但是这里会引发异常- 字体"xx"不支持“regular”

    '可以在使用字体前判断一下,如果不支持,则使用默认字体 Dim lyps As System.Drawing.Font = New System.Drawing.Font(lypname.Item(e.Index), 10) Dim Clocation As PointF Clocation = e.Bounds.Location Console.WriteLine(lypname.Item(e.Index)) Console.WriteLine(e.Index) e.Graphics.DrawString(ComboBox1.Items.Item(e.Index).ToString, lyps, lypB, e.Bounds, StringFormat.GenericDefault) End Sub



    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月25日 16:08
    版主
  • '这里要修改,原来的是获取不到字体的,但是这里会引发异常- 字体"xx"不支持“regular”

    什么意思?


    Visual Basic 初学者 望关照!

    2013年1月26日 12:21
  • 在一些没有这个字体库的机器上,就会有这样的异常,比如我的。

    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月26日 12:28
    版主
  • 还有几个问题啊

    ComboBox1.SelectedItem = 0

    1这个代码为什么不起作用(加在Form1 Load中)

    2 为什么有的是乱码--


    Visual Basic 初学者 望关照!

    2013年1月26日 12:35
  • 1. 使用Combobox.Text

    2. 我觉得还是因为字体库的原因引起的


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月26日 13:15
    版主
  • 谢谢了啊

    Visual Basic 初学者 望关照!

    2013年1月27日 15:12