将一组实线虚线笔添加到组合框中

时间:2012-05-30 04:43:24

标签: c# winforms

这是这个问题的延伸 C# override OnDrawItem 我已经制作了这个自定义组合框类

class LineStyleComboBox: System.Windows.Forms.ComboBox
{
    Pen SolidPen;
    Pen DashedPen;
    public LineStyleComboBox()
    {
        this.DropDownStyle = ComboBoxStyle.DropDownList;
        this.DrawMode = DrawMode.OwnerDrawFixed;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        float[] dashValues = { 5, 2, 15, 4 };

         if (e.Index > -1) 
         {

             int startX = e.Bounds.Left + 5;
             int startY = (e.Bounds.Y + e.Bounds.Height / 2);

             int endX = e.Bounds.Right - 5;
             int endY = (e.Bounds.Y + e.Bounds.Height / 2);

             using ( SolidPen = new Pen(Color.Blue, (Int32)this.Items[e.Index]))
             {
                 e.Graphics.DrawLine(SolidPen, new Point(startX, startY), new Point(endX, endY));
             }

             using ( DashedPen = new Pen(Color.Blue, (Int32)this.Items[e.Index]))
             {
                 e.Graphics.DrawLine(DashedPen, new Point(startX, startY), new Point(endX, endY));
                 DashedPen.DashPattern = dashValues;
             }
        }
        base.OnDrawItem(e);
    }
}

在我需要使用此LineStyleComboBox的另一种形式中,如何将笔样式集合添加为项目。 这样我就可以创建一个带有线条样式的组合框作为项目(实心笔,虚线笔)。

private void frmDlgGraphOptions_Load(object sender, EventArgs e)
{
    lineStyleComboBox2.Items.Add(solidpen,dashed pen)
}

1 个答案:

答案 0 :(得分:0)

您应该查看使用WinForms应用程序中嵌入的WPF控件。您可以使用WPF控件执行任何操作。

这个例子就在你的小巷里。它是一个带有自定义图形项目的组合框。

http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms