2013-03-26 15 views
0

我有一个派生类ComboBox并覆盖OnDrawItem自定义绘制下拉列表项。如何制作组合框的编辑部分(在关闭下拉菜单时显示的部分或打开下拉菜单时显示的部分)继续以默认ComboBox的工作方式绘制?有没有办法调用基本的ComboBox功能来绘制编辑区域部分,或者在OwnerDraw模式下不可用?如果不可用,如何模拟DropDownDropDownList样式的编辑区域部分的外观?如何恢复到在OwnerDraw组合框中以默认方式绘制组合框编辑区域?

+0

可能重复(http://stackoverflow.com/questions/2891981/net-ownerdraw-combobox-vista-7-themed-dropdownlist) – 2013-03-26 17:56:45

回答

0

这个问题实际上涵盖了两个问题。

  1. 为了目标是什么数据显示在组合框中的编辑区域,使用的事件参数的国家财产中的OnDrawItem事件,像这样:

    Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs) 
        ' Draw the background of the item. 
        e.DrawBackground() 
    
        ' Skip doing anything else if the item doesn't exist. 
        If e.Index = -1 Then Exit Sub 
    
        If (e.State And DrawItemState.ComboBoxEdit) = DrawItemState.ComboBoxEdit Then 
         ' Draw the contents of the edit area of the combo box. 
        Else 
         ' Draw the contents of an item in the drop down list. 
    
         ' Draw the focus rectangle. 
         e.DrawFocusRectangle() 
        End If 
    End Sub 
    
  2. 的OwnerDraw模式将不会应用系统正在使用的任何视觉样式主题,例如在按钮表面上显示下拉箭头,在DropDownList样式中将编辑区域更改为具有按钮的面部等等。所有这些都必须手动完成OnPaint事件处理程序。 This question意味着在.NET框架内没有使用组合框视觉样式的开箱即用的方法调用,但可能有解决方法,或者您可以手工编程样式的实现。

的[.NET的OwnerDraw组合框:Vista/7的主题的DropDownList]
1

DrawItemEventArgs作为e传递给你几个工作的工具。要复制类似的东西,你可以做系统吸引您在OwnerDraw模式是什么这样的事情:

Public Class MyComboBox 
    Inherits System.Windows.Forms.ComboBox 

    Private _font As Font = New Font(FontFamily.GenericSansSerif, 9.0, _ 
                FontStyle.Regular) 

    Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs) 
     e.DrawBackground() 
     If e.Index = -1 Then Exit Sub 
     e.Graphics.DrawString(Me.Items(e.Index).ToString, _font, _ 
           System.Drawing.Brushes.Black, _ 
           New RectangleF(e.Bounds.X, e.Bounds.Y, _ 
           e.Bounds.Width, e.Bounds.Height)) 
     e.DrawFocusRectangle() 
    End Sub 
End Class 

编辑:

如果你的意思是改变显示项目的绘制行为与项目在下拉区域中绘制您可以分支基于该DroppedDown财产绘图代码:

e.DrawBackground() 
If e.Index = -1 Then Exit Sub 

If Not Me.DroppedDown Then 
    e.Graphics.DrawString(Me.Items(e.Index).ToString, _font, 
          System.Drawing.Brushes.Black, _ 
          New RectangleF(e.Bounds.X, e.Bounds.Y, _ 
          e.Bounds.Width, e.Bounds.Height)) 

Else 
    ' do whatever you want - draw something else 
    Dim rectangle As Rectangle = New Rectangle(2, e.Bounds.Top + 2, _ 
           e.Bounds.Height, e.Bounds.Height - 4) 
     e.Graphics.FillRectangle(Brushes.Blue, rectangle) 
     e.Graphics.DrawString("foo...I'm item #" & e.Index, _font, _ 
           System.Drawing.Brushes.Black, _ 
           New RectangleF(e.Bounds.X + rectangle.Width, _ 
           e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) 
End If 

e.DrawFocusRectangle() 

同样,你也可以分支基于If Me.DropDownStyle = ComboBoxStyle.DropDownList ...等。以任何你喜欢的方式处理每个案件。如果您想要渐变,主题元素或由OS绘制的组件提供的其他功能,那么您必须自己绘制它们。

+0

这看起来像是如何模拟系统会在下拉区域绘制项目。也许我的问题还不够清楚,但我正在专门寻找绘制文本框部分的方法,只有文本框部分,系统的方式(同时牢记文本框部分之间的差异DropDown和DropDownList模式)。 – 2013-03-26 17:36:57

+0

@MattHamsmith这是绝对模糊不清的。你能再试一次吗?也许张贴一张你想要发生的事情的照片。 – 2013-03-26 17:52:10

+0

@MattHamsmith我猜测过你后面的事......也许这是? – 2013-03-26 18:05:30