关于winform 的标签页控件tabControl设置选项卡垂直左停靠后,标签页被隐藏不显示问题?

关于winform 的标签页控件tabControl设置选项卡垂直左停靠后,标签页被隐藏不显示问题?

问题描述:

 protected override void OnDrawItem(DrawItemEventArgs e)
        {
            TabPage tabPage = this.TabPages[e.Index];
            tabPage.UseVisualStyleBackColor = true;
            Graphics g = e.Graphics;
            Brush backtabcontrol;//整个tab控件背景色
            Brush backbrush;//标签页背景色
            Brush textBrush;//标签字体颜色
            Font tabFont;//标签字体
            if (e.State == DrawItemState.Selected)
            {
                textBrush = new SolidBrush(Color.FromArgb(255, 29, 168, 255));
            }
            else
            {
                textBrush = new SolidBrush(e.ForeColor);
            }
            //绘制整个tab控件背景
            backtabcontrol = new SolidBrush(Color.FromArgb(255, 252, 252, 252));
            g.FillRectangle(backtabcontrol, this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Size.Width, this.ClientRectangle.Height);
            //绘制激活标签页标题区域的背景
            //tabPage.BackColor =Color.FromArgb(255, 250, 250, 250);
            Rectangle backrect = this.GetTabRect(e.Index);//标签页区域
            backbrush = new SolidBrush(Color.FromArgb(255, 250, 250, 250));
            g.FillRectangle(backbrush, backrect);
            //绘制标签标题字体
            tabFont = new Font("微软雅黑", 13, FontStyle.Bold, GraphicsUnit.Pixel);
            StringFormat strFlags = new StringFormat(StringFormatFlags.DisplayFormatControl);
            strFlags.Alignment = StringAlignment.Center;
            strFlags.LineAlignment = StringAlignment.Center;
            g.DrawString(tabPage.Text, tabFont, textBrush, backrect, strFlags);
            tabPage.ToolTipText = tabPage.Text;
            backtabcontrol.Dispose();
            backbrush.Dispose();
            tabFont.Dispose();
            textBrush.Dispose();
            base.OnDrawItem(e);
        }

protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            TabPage tabPage = this.TabPages[e.Index];
            Graphics g = e.Graphics;
            Brush tabBackColor;//整个tab控件背景色
            Brush tabTitleBackColor;//标签页标题区域背景色
            Brush textColor;//标签字体颜色
            //绘制整个tab控件背景,继承父控件背景色
            tabBackColor = new SolidBrush(this.Parent.BackColor);
            g.FillRectangle(tabBackColor, this.ClientRectangle);
            tabBackColor.Dispose();
            //文本显示方式
            StringFormat textFormat = new StringFormat(StringFormatFlags.DisplayFormatControl);
            textFormat.Alignment = StringAlignment.Center;
            textFormat.LineAlignment = StringAlignment.Center;
            for(int i = 0; i < this.TabPages.Count; i++)
            {
                //标签页区域
                Rectangle tabTitleRect = this.GetTabRect(i);
                if (e.State== DrawItemState.Selected&&tabPage==this.TabPages[i])
                {
                    tabTitleBackColor = new SolidBrush(Color.White);
                    textColor = new SolidBrush(Color.FromArgb(255, 29, 168, 255));
                }
                else
                {
                    tabTitleBackColor = new SolidBrush(this.TabPages[i].BackColor);
                    textColor = new SolidBrush(this.TabPages[i].ForeColor);
                }
                tabTitleBackColor = new SolidBrush(Color.Transparent);
                //绘制激活标签页标题区域的背景
                g.FillRectangle(tabTitleBackColor, tabTitleRect);
                tabTitleBackColor.Dispose();
                //绘制标签标题字体
                g.DrawString(this.TabPages[i].Text, this.TabPages[i].Font, textColor, tabTitleRect, textFormat);
                this.TabPages[i].ToolTipText = this.TabPages[i].Text;
                textColor.Dispose();
            }
        }

计算一下坐标,生成 一列 Button或者Lable遮挡住标签页,tag可用于记录对应的页签index,完成click事件的修改样式及 tabControl的页签切换

 

找到问题原因了,就是下图方框的重绘整个tab控件背景色导致的,注释掉就能标签标题就显示出来了。为什么重绘背景色后未激活的tab标题就不显示了????有没有大神帮忙解释下???z