支持Office Xp/2003样式的扁平ComboBox控件
0 导论
首先,在网上可以找到很多这样实现扁平效果的ComboBox代码.我试着实现了一个比较简单的ComboBox控件,但是它却有很多的功能.
正如截图所示,你会发现在此控件中,我集成有如下的功能:支持大号字体,从右至左显示文字,两种下拉样式,当然还包括支持Office Xp/2003两种样式.
1 两种不同样式下的状态
从上到下依次是:正常状态,输入焦点时,不起作用,下拉状态.
2 如何使用代码
FlatComboBox是一个继承于.NetFramework中Windows.Forms.ComboBox的用户控件
通过重写类库中的WinProc来激活MouseMove等事件
1
Protected Overrides Sub WndProc(ByRef m As _
2
System.Windows.Forms.Message)
3
MyBase.WndProc(m)
4
Select Case m.Msg
5![](/Images/OutliningIndicators/InBlock.gif)
6
Case &HF
7
'WM_PAINT
8![](/Images/OutliningIndicators/InBlock.gif)
9
'"simple" is not currently supported
10
If Me.DropDownStyle = _
11
ComboBoxStyle.Simple Then Exit Sub
12![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
34
UpdateState()
35
End Select
36
End Sub
37![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
37
![](/Images/OutliningIndicators/None.gif)
通过Public属性来切换两种样式(Xp/2003):
1
'Property to let the user change the style
2
Public 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
10![](/Images/OutliningIndicators/None.gif)
ps:本控件由vb.net实现. 可以通过在线转化工具很容易的转化成C#代码![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](/Images/OutliningIndicators/None.gif)
原文出处:http://www.codeproject.com/vb/net/FlatComboBox.asp
源代码下载 Demo下载