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

博主在网上参考代码,用C#实现了一个简单却功能丰富的扁平效果ComboBox控件。该控件集成了支持大号字体、从右至左显示文字、两种下拉样式等功能,还介绍了两种样式下的状态及使用代码,通过重写WinProc激活事件,用Public属性切换样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


0 导论

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

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

1 两种不同样式下的状态

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

2 如何使用代码


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

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

37 None.gif


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

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

10 None.gif
ps:本控件由vb.net实现.  可以通过在线转化工具很容易的转化成C#代码
原文出处:http://www.codeproject.com/vb/net/FlatComboBox.asp
  源代码下载    Demo下载 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值