2009-04-10 17 views
0

我试图提取到类文件中的TabConttrols DrawItem事件下有以下代码。由于它与事件有关,所以我遇到了麻烦。任何提示或指针将不胜感激。提取事件(tabControl_DrawItem)到类库

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

回答

1

取决于你想要达到的目标。您总是可以分类为TabControl,或者您可以将绘图代码封装到您将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... 
    } 
}