0 导论

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

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

1 两种不同样式下的状态

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

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近日在网上搜到一些如何在WinForm的DataGrid上显示行号的资料,因此自己也写了一段代码,因为是在前人实践的基础上做了些修改,所以不敢独享。如下: 

using System.ComponentModel;
 /** <summary> 
 /// 可以显示行号的DataGrid 
 /// </summary> 
 public class HDataGrid : System.Windows.Forms.DataGrid 
 { 
     public HDataGrid():base() 
     {} 
  
     private bool _DisplayRowNumber = false; 
     /** <summary> 
     /// 控制是否显示行号 
     /// </summary> 
     [Browsable(true),DefaultValue(false),Description("是否显示行号")] 
     public bool DisplayRowNumber 
     { 
         get { return _DisplayRowNumber; } 
         set {  
             _DisplayRowNumber = value;  
             this.Invalidate(); 
              
         } 
     } 
  
  
     /** <summary> 
     /// 重载OnPaint方法显示行号 
     /// </summary> 
     /// <param name="e"></param> 
     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
     { 
         base.OnPaint (e); 
  
         //显示行号 
         if (DisplayRowNumber && this.RowHeadersVisible && this.VisibleColumnCount > 0) 
         { 
             if (this.DataSource == null) return; 
              
             int iRowNumStart = this.VertScrollBar.Value; 
             int yPos = 0; 
             int iRowNumEnd = iRowNumStart + this.VisibleRowCount; 
  
             while(iRowNumStart < iRowNumEnd) 
             { 
                 yPos = this.GetCellBounds(iRowNumStart++,0).Y + 2; 
  
                 string strRowNum = string.Format(" {0}",iRowNumStart); 
  
                 e.Graphics.DrawString(strRowNum,this.Font ,new System.Drawing.SolidBrush(System.Drawing.Color.Black), 6, yPos); 
             } 
         } 
  
          
     } 
  
 } 
  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#代码