2013-09-21 3 views
0

DrawMode의 tabcontrol을 OwnerDrawFixed으로 설정했습니다. 나는 그 탭을 그리고 그것을 색칠 할 수 있었다. Black 내가하고 싶은 것은 선택된 탭을위한 별도의 페인트를 그려서 색을 Gray으로하고 싶다. 내 Draw_Item 이벤트입니다.선택한 탭만 페인트하는 방법

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     //This is the code i want to use to color the selected tab (e.Graphics.FillRectangle(Brushes.Gray, e.Bounds.X, e.Bounds.Y, 200, 32); 
     e.Graphics.FillRectangle(Brushes.Black, e.Bounds.X, e.Bounds.Y, 200, 32); 
     e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right-17, e.Bounds.Top+4); 
     e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4); 
     e.DrawFocusRectangle(); 
     if (e.Index == activeButton) 
      ControlPaint.DrawBorder(e.Graphics, new Rectangle(e.Bounds.Right - 22, e.Bounds.Top + 4, 20, 20), Color.Blue, ButtonBorderStyle.Inset); 
    } 

나는 내가 원하는 전역 변수 TabPage current 현재 탭 페이지를 저장하는 데 사용할 만들었으며 SelectedIndexChanged 이벤트에 변수 i 선택한 탭을 할당 탭의 재 페인트를 강제로 Invalidate();라고했다. 내가 붙어있어 이제

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     current = tabControl1.SelectedTab; 
     tabControl1.Invalidate(); 
    } 

DrawItem 이벤트 만 선택한 탭 색상을하는 방법입니다.

내 질문 지금은 DrawItem 이벤트에서 선택한 탭을 확인하고 선택한 탭만 페인트하는 방법입니다.

+0

음, 친숙한 것으로 보이지만 행복한 기억이 아닙니다. Invalidate() 메서드에 대한 MSDN 기사에서 많은 오버로드를 살펴보십시오. 그것이 차이를 만든다면 당신은 그것을 잘못하고 있습니다. –

+0

@HansPassant Mehn. 아직도 배워야 할 것이 많습니다. 방금 사각형을 사용하기 시작했습니다. –

+0

@HansPassant 많은 인터넷 검색 및 연구 후 마침내 내 질문에 대한 답변을 발견 http://stackoverflow.com/questions/18937905/how-to-paint-only-selected-tab/18938631#18938631 –

답변

0

나는 내 질문에 대한 대답을 마침내 발견했다.

전역 변수를 int 데이터 유형으로 수정 한 다음 SelectedIndexChanged에 인덱스를 할당 한 다음 DrawItem에서 확인했습니다.

int current; 
    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     current = tabControl1.SelectedIndex; 
     tabControl1.Invalidate(); 
    } 

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.Graphics.FillRectangle(Brushes.Black, e.Bounds.X, e.Bounds.Y, 200, 32); 
     e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right-17, e.Bounds.Top+4); 
     e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4); 
     if (e.Index == activeButton) 
      ControlPaint.DrawBorder(e.Graphics, new Rectangle(e.Bounds.Right - 22, e.Bounds.Top + 4, 20, 20), Color.Blue, ButtonBorderStyle.Inset); 

     if (e.Index == current) 
     { 
      e.Graphics.FillRectangle(Brushes.Gray, e.Bounds.X, e.Bounds.Y, 200, 32); 
      e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 17, e.Bounds.Top + 4); 
      e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4);  
     } 
    } 

저에게 잘 맞습니다.