2012-06-05 6 views
2

이것은 내가 이전에했던 것과는 다른 접근법입니다. 내 콤보 소유자 그린 콤보 상자에서 줄이있는 C# 콤보 상자

3 개 라인 (고체, 대시, dashdot)를 그릴

여기
this.DrawMode = DrawMode.OwnerDrawVariable; 
      this.DropDownStyle = ComboBoxStyle.DropDownList; 
    protected override void OnDrawItem(DrawItemEventArgs e) 
     { 
      e.DrawBackground(); 
      int startX = e.Bounds.Left + 5; 
      int startY = (e.Bounds.Y); 
      Point p1=new Point(startX,startY); 
      int endX = e.Bounds.Right - 5; 
      int endY = (e.Bounds.Y); 
      ComboBoxItem item = (ComboBoxItem)this.Items[e.Index]; 
      Point p2=new Point(endX,endY); 
      base.OnDrawItem(e); 
      Pen SolidmyPen = new Pen(item.foreColor, 1); 
      Pen DashedPen = new Pen(item.foreColor, 1); 
      DashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
      Pen DashDot = new Pen(item.foreColor, 1); 
      DashDot.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot; 
      // Pen DashedPen = new Pen(item.foreColor, (Int32)this.Items[e.Index]); 

      Bitmap myBitmap = new Bitmap(item.Picture); 
      Graphics graphicsObj; 
      graphicsObj = Graphics.FromImage(myBitmap); 
      switch (e.Index) 
      { 
       case 0: 
        graphicsObj.DrawLine(SolidmyPen, p1, p2); 
        break; 
       case 1: 
        graphicsObj.DrawLine(DashedPen, p1, p2); 
        break; 
       case 2: 
        graphicsObj.DrawLine(DashDot, p1, p2); 
        break; 


      } 

내가 할 노력하고있어 무엇 드롭 다운 이전 colpr 선택기에서 선택한 색상으로 그려 질 . 콤보 상자에 3 선 (솔리드, 대시, 대시 돗)을 그립니다.

나는

+0

winforms로 태그 된 WPF를 사용하고 있습니까? ComboboxItem은 winforms에 존재하지 않습니다? 어쨌든 내가 알고있는 것이 아닙니다. –

답변

2

이 시도

u를 감사 선택한 색상을 일부 파란색을 제외하고 콤보 상자의 모든 라인을 볼 수 없습니다.

새로운 winforms 응용 프로그램을 시작했습니다. ComboBox를 기반으로 클래스를 만들고 코드를 추가하고 조금 수정했습니다. 나는 당신이 중요한 문제가 비트 맵 부분에 있다고 생각합니다. 새 비트 맵을 만든 다음 그 위에 그려지지만 만든 비트 맵은 사용하지 마십시오. 만든 코드를 유지하려면 item.Picture = myBitmap 메서드 끝에 추가해야합니다. 그러나 나는 그것이 ondrawitem을 다시 호출 할 것이고 여러분은 무한 루프에 빠질 것이라고 생각합니다. item.Picture를 기반으로 그래픽 객체를 만드는 대신 DrawItemEventArgs에서 생성 한 그래픽 객체 만 사용하면됩니다.

e.Graphics 

여기는 내가 한 것입니다. 그리고 당신은 이미 알고 있을지 모르지만 필자는 필자가 아래에 설명했던 것처럼 {....}을 사용하여 항상 펜, 브러쉬 및 그래픽을 포장해야합니다.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
} 

public class MyComboBox : ComboBox 
{ 
    public MyComboBox() 
    { 
    this.DrawMode = DrawMode.OwnerDrawVariable; 
     this.DropDownStyle = ComboBoxStyle.DropDownList; 
    } 
    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     //I removed you int startX... endy... stuff, unless you want to keep it for readability there is no need 
     Point p1 = new Point(e.Bounds.Left + 5, e.Bounds.Y + 5); 
     Point p2 = new Point(e.Bounds.Right - 5, e.Bounds.Y + 5); 

     //I am not sure why you would want to call the base.OnDrawItem, feel free to uncomment it if you wish though 
     //base.OnDrawItem(e); 

     switch (e.Index) 
     { 
      case 0: 
       using (Pen SolidmyPen = new Pen(e.ForeColor, 1)) 
       e.Graphics.DrawLine(SolidmyPen, p1, p2); 
       break; 
      case 1: 
       using (Pen DashedPen = new Pen(e.ForeColor, 1)) 
       { 
        DashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
        e.Graphics.DrawLine(DashedPen, p1, p2); 
       } 
       break; 
      case 2: 
       using (Pen DashDot = new Pen(e.ForeColor, 1)) 
       { 
        DashDot.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot; 
        e.Graphics.DrawLine(DashDot, p1, p2); 
       } 
       break; 


     } 
    } 
} 
+0

그게 .... 감사합니다. – user575219

+0

기꺼이 도와 드리겠습니다. –

+0

@ K'Leg : 한 번 더 확장하십시오. 나는 새로운 토론으로 게시하고있다. – user575219

1

비슷한 방법으로 콤보 박스를 그립니다. 그러나 GDI +는 계속 예외를 던집니다. Windows XP에서는 잘 작동하지만 Windows 7에서는 작동하지 않습니다.

그래서 해킹으로 해결해야했습니다.

이벤트를 시작하는 타이머를 폼에 처음 표시 한 후 100ms를 추가했습니다. 이 이벤트는 콤보 상자 목록의 첫 번째 항목을 표시합니다.

private void timer1_Tick(object sender, EventArgs e) 
{ 
    // Use a short 100 ms delay before showing the default items 
    // in the dropdown lists 
    predefinedComboBox.SelectedIndex = 0; 

    // Disable the timer 
    timer1.Enabled = false; 
} 

레이아웃 이벤트가 너무 일 렀습니다. 컨트롤이 아직 준비되지 않았습니다. 따라서 예외가 발생했습니다. 원하는 효과를 얻기 위해 어떤 다른 이벤트를 사용할 수 있는지 잘 모르겠습니다.