标签: ondrawitem

如何使自定义ComboBox(OwnerDrawFixed)看起来像标准的ComboBox 3D?

我正在制作一个自定义的ComboBox,继承自Winforms的标准ComboBox.对于我的自定义组合框,我定DrawModeOwnerDrawFixedDropDownStyleDropDownList.然后我写自己的OnDrawItem方法.但我这样结束了:

标准与自定义组合框

如何使我的自定义组合框看起来像标准组合?


更新1:ButtonRenderer

在四处搜索之后,我找到了这ButtonRenderer堂课.它提供了一个DrawButton静态/共享方法 - 顾名思义 - 绘制正确的3D按钮.我现在正在试验它.


更新2:什么覆盖了我的控件?

我尝试使用我能想到的各种对象的图形属性,但我总是失败.最后,我尝试了表单的图形,显然有些东西覆盖了我的按钮.

这是代码:

Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
  Dim TextToDraw As String = _DefaultText
  __Brush_Window.Color = Color.FromKnownColor(KnownColor.Window)
  __Brush_Disabled.Color = Color.FromKnownColor(KnownColor.GrayText)
  __Brush_Enabled.Color = Color.FromKnownColor(KnownColor.WindowText)
  If e.Index >= 0 Then
    TextToDraw = _DataSource.ItemText(e.Index)
  End If
  If TextToDraw.StartsWith("---") Then TextToDraw = StrDup(3, ChrW(&H2500)) ' U+2500 is "Box Drawing Light Horizontal"
  If (e.State And DrawItemState.ComboBoxEdit) > 0 Then
    'ButtonRenderer.DrawButton(e.Graphics, …
Run Code Online (Sandbox Code Playgroud)

vb.net combobox custom-controls visual-studio-2010 ondrawitem

8
推荐指数
1
解决办法
2万
查看次数

覆盖ListBox的DrawItem - 未重绘未选择的项目

这是一个C#桌面应用程序.DrawStyle我的属性ListBox设置为OwnerDrawFixed.

问题:我重写DrawItem以不同的字体绘制文本,它的工作原理.但是当我在运行时开始调整表单大小时,正确绘制了所选项目,但其余部分未重绘,导致文本看起来已损坏未选中的项目.

这是我的代码:

private void listDevices_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();

    string textDevice = ((ListBox)sender).Items[e.Index].ToString();

    e.Graphics.DrawString(textDevice,
        new Font("Ariel", 15, FontStyle.Bold), new SolidBrush(Color.Black), 
        e.Bounds, StringFormat.GenericDefault);


    // Figure out where to draw IP
    StringFormat copy = new StringFormat(
        StringFormatFlags.NoWrap |
        StringFormatFlags.MeasureTrailingSpaces
    );
    copy.SetMeasurableCharacterRanges(new CharacterRange[] {new CharacterRange(0, textDevice.Length)});

    Region[] regions = e.Graphics.MeasureCharacterRanges(
        textDevice, new Font("Ariel", 15, FontStyle.Bold), e.Bounds, copy);

    int width = (int)(regions[0].GetBounds(e.Graphics).Width);
    Rectangle rect = e.Bounds;
    rect.X += width;
    rect.Width -= width;

    // draw IP …
Run Code Online (Sandbox Code Playgroud)

c# listbox winforms ondrawitem

4
推荐指数
2
解决办法
8554
查看次数