【Winform】TabControl使用汇总

 

一、改变标签颜色

            tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
            ItemTabControl.DrawItem += new System.Windows.Forms.DrawItemEventHandler(ItemTabControl_DrawItem);

 

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            //标签默认文字颜色
            SolidBrush DefaultFrontBrush = new SolidBrush(Color.Black);

            //改变的标签背景颜色
            SolidBrush BackBrush = new SolidBrush(Color.FromArgb(255, 0, 0));
            //标签默认背景颜色
            SolidBrush BackBrushDefault = new SolidBrush(Color.Transparent);

            //标签文字,选中的标签文字加粗
            Font font = new Font("宋体", 9);
            Font fontbold = new Font("宋体", 9, FontStyle.Bold);

            //设置文字对齐方式
            StringFormat StringF = new StringFormat();
            StringF.Alignment = StringAlignment.Center;
            StringF.LineAlignment = StringAlignment.Center;
            
            for (int i = 0; i < MainTabRes.Count; i++)
            {
                //获取标签头工作区域
                Rectangle Rec = tabControl1.GetTabRect(i);
                string text = tabControl1.TabPages[i].Text;
                if (MainTabRes[i] == false)//MainTabRes指示要改变背景色的标签索引,List<bool> MainTabRes = new List<bool>();
                {
                    e.Graphics.FillRectangle(BackBrush, Rec);
                }
                else
                {
                    e.Graphics.FillRectangle(BackBrushDefault, Rec);
                }
                if (i == tabControl1.SelectedIndex)
                {
                    //绘制标签头文字
                    e.Graphics.DrawString(text, fontbold, DefaultFrontBrush, Rec, StringF);
                }
                else
                {
                    e.Graphics.DrawString(text, font, DefaultFrontBrush, Rec, StringF);
                }
            }
        }

 

posted @ 2024-08-20 14:22  不溯流光  阅读(110)  评论(0编辑  收藏  举报