2012-05-30 54 views
0

這是這個問題 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) 
} 

回答