2011-02-01 58 views
0

这里就是我做的:自定义Windows的外观窗体组合框

Public Class ComboBox 
    Inherits System.Windows.Forms.ComboBox 

    Public Sub New() 
     SetStyle(ControlStyles.OptimizedDoubleBuffer _ 
     Or ControlStyles.UserPaint, True) 
    End Sub 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
     e.Graphics.FillRectangle(gradient, ClientRectangle) 
     // The preceding line is a simplification of about 50 lines of code. 

     If Not String.IsNullOrEmpty(Text) Then 
      Dim rect As New Rectangle(2, 0, Width - 2, Height) 

      Using format As New StringFormat() 
       format.LineAlignment = StringAlignment.Center 

       Using brush As New SolidBrush(ForeColor) 
        e.Graphics.DrawString(Text, Font, brush, rect, format) 
       End Using 
      End Using 
     End If 

     e.Graphics.FillPolygon(Brushes.Black, New Point() { _ 
     New Point(Width - 5, Height \ 2 - 1), _ 
     New Point(Width - 12, Height \ 2 - 1), _ 
     New Point(Width - 9, Height \ 2 + 3)}) 
    End Sub 
End Class 

我有两个问题:

  1. 它的高度始终是24
  2. 下拉列表的下降呈现与一个丑陋的Windows 3.1字体。

http://i56.tinypic.com/w1p4ph.png

+0

你想做什么?为什么你不能使用常规的ComboBox? – Justin 2011-02-01 14:45:18

+0

该公司聘请了一位艺术家。他画了东西。现在我必须执行它。 (之前你问:我不能使用WPF。) – 2011-02-01 14:56:17

回答

0

您的问题显然是在 “代码全” 的一部分。我建议您一次解决一个问题:

1)高度问题:会不会是你的高度设置为24,如果你改变德高度控制将相应调整?你看过Control.PreferredSize属性吗?在这里寻找这样的:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.preferredsize.aspx

2)字体问题。如果不查看代码,这是无法诊断的。我所能说的只是确保你正在使用任何你想使用的字体来绘制下拉元素。

0

高度问题几乎可以肯定,因为您是从ComboBox继承,然后使用标准API调用来绘制大部分内容,并且当Windows绘制组合框时,高度由字体大小和对高度的任何更改修复财产被忽略。

同样,Windows正在因为你不改变它或API调用的地方设置它使用默认字体。当涉及到继承和做一些小的修改

组合框是不完全友好。您可能必须完全提供您自己的自定义实现,这也不是微不足道的。您可能无法找到第三方自定义控件来呈现您想要的任何内容。你的公司犯了一个艺术家的演出没有实际考虑可用的控制等错误。对不起...