C # Winform: how to set TabControl base color (not tab) And use icon

I applied the solution provided by the user to solve the problem, but this is another problem. I used to have an icon along with text, but after I used the code described here, my icon no longer displays. What could it be?

And how can I use the icon when using a color other than the default value in the tab title?

The solution I used is:

private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { TabPage CurrentTab = tabControl1.TabPages[e.Index]; Rectangle ItemRect = tabControl1.GetTabRect(e.Index); SolidBrush FillBrush = new SolidBrush(Color.Red); SolidBrush TextBrush = new SolidBrush(Color.White); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.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.White; TextBrush.Color = Color.Red; ItemRect.Inflate(2, 2); } //Set up rotation for left and right aligned tabs if (tabControl1.Alignment == TabAlignment.Left || tabControl1.Alignment == TabAlignment.Right) { float RotateAngle = 90; if (tabControl1.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(); } 
+4
source share
1 answer

You also need to draw an icon. An example would be to add something like the following after drawing the Background tab in your code (I assume that a list of images was used here)

 int imageLeftOffset = 4; Point imagePos = new Point(imageLeftOffset, ItemRect.Top + (ItemRect.Height - tabControl1.ImageList.ImageSize.Height + 1) / 2); tabControl1.ImageList.Draw(e.Graphics, imagePos, CurrentTab.ImageIndex); 

You may need to re-draw the text so that the text and image do not overlap.

0
source

Source: https://habr.com/ru/post/1481508/


All Articles