I have the following code under a TabConttrols DrawItem event that I am trying to extract into a class file. I am having trouble since it is tied to an event. Any hints or pointers would be greatly appreciated.
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(); }
Depends on what you’re trying to achieve. You could always sub class TabControl or you could encapsulate the drawing code in a class that you pass a TabControl to.