0 导论

支持Office Xp/2003样式的扁平ComboBox控件_Windows

首先,在网上可以找到很多这样实现扁平效果的ComboBox代码.我试着实现了一个比较简单的ComboBox控件,但是它却有很多的功能.

正如截图所示,你会发现在此控件中,我集成有如下的功能:支持大号字体,从右至左显示文字,两种下拉样式,当然还包括支持Office Xp/2003两种样式.

1 两种不同样式下的状态

支持Office Xp/2003样式的扁平ComboBox控件_控件_02


    

   从上到下依次是:正常状态,输入焦点时,不起作用,下拉状态.

2 如何使用代码

FlatComboBox是一个继承于.NetFramework中Windows.Forms.ComboBox的用户控件
通过重写类库中的WinProc来激活MouseMove等事件

1Protected Overrides Sub WndProc()Sub WndProc(ByRef m As _
  2                       System.Windows.Forms.Message)
  3        MyBase.WndProc(m)
  4        Select Case m.Msg
  5
  6            Case &HF
  7                'WM_PAINT
  8
  9                '"simple" is not currently supported
 10                If Me.DropDownStyle = _
 11                           ComboBoxStyle.Simple Then Exit Sub
 12
 13                '==========START DRAWING===========
 14                g = Me.CreateGraphics
 15                'clear everything
 16                If Me.Enabled Then
 17                    g.Clear(Color.White)
 18                Else
 19                    g.Clear(Color.FromName("control"))
 20                End If
 21                'call the drawing functions
 22                DrawButton(g)
 23                DrawArrow(g)
 24                DrawBorder(g)
 25                DrawText(g)
 26                '===========STOP DRAWING============
 27
 28            Case 7, 8, &H7, &H8, &H200, &H2A3
 29                'CMB_DROPDOWN, CMB_CLOSEUP, WM_SETFOCUS, 
 30                'WM_KILLFOCUS, WM_MOUSEMOVE,  
 31                'WM_MOUSELEAVE (if you move the mouse fast over
 32                'the combobox, mouseleave doesn't always react)
 33
 34                UpdateState()
 35        End Select
 36End Sub
 37

通过Public属性来切换两种样式(Xp/2003):

1'Property to let the user change the style
  2    Public Property FlatComboStyle()Property FlatComboStyle() As styles
  3        Get
  4            Return style
  5        End Get
  6        Set(ByVal Value As styles)
  7            style = Value
  8        End Set
  9    End Property


10ps:本控件由vb.net实现.  可以通过在线转化工具很容易的转化成C#代码