IvenWong 2021-06-28 11:27 采纳率: 50%
浏览 208
已结题

关于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);
        }
  • 写回答

4条回答 默认 最新

  • IvenWong 2021-06-29 08:30
    关注
    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();
                }
            }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月28日
  • 已采纳回答 11月20日

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)