2009-04-10 4 views
0

J'ai le code suivant sous un événement TabConttrols DrawItem que j'essaie d'extraire dans un fichier de classe. J'ai des problèmes parce que c'est lié à un événement. Tous les conseils ou les pointeurs seraient grandement appréciés.Extraction d'un événement (tabControl_DrawItem) dans la bibliothèque de classes

 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(); 
    } 

Répondre

1

Dépend de ce que vous essayez d'atteindre. Vous pouvez toujours classer sous TabControl ou vous pouvez encapsuler le code de dessin dans une classe à laquelle vous passez un TabControl.

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... 
    } 
} 
Questions connexes