2009-04-10 17 views
0

Ich habe den folgenden Code unter einem TabConttrols DrawItem-Ereignis, das ich versuche, in eine Klassendatei zu extrahieren. Ich habe Probleme, da es an ein Ereignis gebunden ist. Alle Hinweise oder Hinweise würden sehr geschätzt werden.Extrahieren eines Ereignisses (tabControl_DrawItem) in der Klassenbibliothek

 private void tabCaseNotes_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     TabPage currentTab = tabCaseNotes.TabPages[e.Index]; 
     Rectangle itemRect = tabCaseNotes.GetTabRect(e.Index); 
     SolidBrush fillBrush = new SolidBrush(Color.Linen); 
     SolidBrush textBrush = new SolidBrush(Color.Black); 
     StringFormat sf = new StringFormat 
           { 
            Alignment = StringAlignment.Center, 
            LineAlignment = StringAlignment.Center 
           }; 

     //If we are currently painting the Selected TabItem we'll 
     //change the brush colors and inflate the rectangle. 
     if (System.Convert.ToBoolean(e.State & DrawItemState.Selected)) 
     { 
      fillBrush.Color = Color.LightSteelBlue; 
      textBrush.Color = Color.Black; 
      itemRect.Inflate(2, 2); 
     } 

     //Set up rotation for left and right aligned tabs 
     if (tabCaseNotes.Alignment == TabAlignment.Left || tabCaseNotes.Alignment == TabAlignment.Right) 
     { 
      float rotateAngle = 90; 
      if (tabCaseNotes.Alignment == TabAlignment.Left) 
       rotateAngle = 270; 
      PointF cp = new PointF(itemRect.Left + (itemRect.Width/2), itemRect.Top + (itemRect.Height/2)); 
      e.Graphics.TranslateTransform(cp.X, cp.Y); 
      e.Graphics.RotateTransform(rotateAngle); 
      itemRect = new Rectangle(-(itemRect.Height/2), -(itemRect.Width/2), itemRect.Height, itemRect.Width); 
     } 

     //Next we'll paint the TabItem with our Fill Brush 
     e.Graphics.FillRectangle(fillBrush, itemRect); 

     //Now draw the text. 
     e.Graphics.DrawString(currentTab.Text, e.Font, textBrush, (RectangleF)itemRect, sf); 

     //Reset any Graphics rotation 
     e.Graphics.ResetTransform(); 

     //Finally, we should Dispose of our brushes. 
     fillBrush.Dispose(); 
     textBrush.Dispose(); 
    } 

Antwort

1

Hängt davon ab, was Sie erreichen möchten. Sie können TabControl immer mit einer Unterklasse versehen oder den Zeichencode in eine Klasse kapseln, an die Sie ein TabControl übergeben.

public class TabRenderer 
{ 
    private TabControl _tabControl; 

    public TabRenderer(TabControl tabControl) 
    { 
     _tabControl = tabControl; 
     _tabControl.DrawMode = TabDrawMode.OwnerDrawFixed; 
     _tabControl.DrawItem += TabControlDrawItem; 
    } 

    private void TabControlDrawItem(object sender, DrawItemEventArgs e) 
    { 
     // Your drawing code... 
    } 
} 
Verwandte Themen