Winform 中tabcontrol 美化
需要对tabcontrol按照美工出的图进行美化
对tabpage页进行标题设置,首先对整个tabcontrol的DrawMode设置为OwnerDrawFixed,由于需要对标题宽度有要求,设置sizemode为fixed,可以对itemsize调整,设置标题的宽和高,然后进行写事件就能实现
private void tb_DrawItem(object sender, DrawItemEventArgs e) { StringFormat sf = new StringFormat(); #region 头背景 sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; Rectangle rec = tb.ClientRectangle; //获取背景图片,我的背景图片在项目资源文件中。 Image backImage = Properties.Resources.pop_tab1_2; e.Graphics.DrawImage(backImage, 0, 2, tb.Width, tb.ItemSize.Height + 2); #endregion #region 设置选择的标签的背景 if (e.Index == tb.SelectedIndex) e.Graphics.DrawImage(Properties.Resources.pop_tab1_1, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); else e.Graphics.DrawImage(Properties.Resources.pop_tab1_2, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); e.Graphics.DrawString(((TabControl)sender).TabPages[e.Index].Text, System.Windows.Forms.SystemInformation.MenuFont, new SolidBrush(Color.Black), e.Bounds, sf); #endregion #region 重写标签名 ColorConverter colorConverter = new ColorConverter(); Color cwhite = (Color)colorConverter.ConvertFromString("#2178ba"); SolidBrush white = new SolidBrush(cwhite); Rectangle rect0 = tb.GetTabRect(0); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("申请单号", new Font("微软雅黑", 12), white, rect0, stringFormat);
#endregion }