123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- using UAS_MES_NEW;
- using System.Runtime.InteropServices;
- namespace UAS_MES_NEW.CustomControl
- {
- [ToolboxBitmap(typeof(TabControl))]
- public partial class CustomTabControl : TabControl
- {
- const int CLOSE_SIZE = 16;
- // Bitmap image = new Bitmap("../../Resources/close.png");
- public CustomTabControl() : base()
- {
- //首先判断选择的是什么样式
- if (this._DisplayManager.Equals(TabControlDisplayManager.Custom))
- {
- //是否由系统绘制,true表示自行绘制
- this.SetStyle(ControlStyles.UserPaint, true);
- this.ItemSize = new Size(0, 15);
- this.Padding = new Point(9, 0);
- }
- this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- this.SetStyle(ControlStyles.ResizeRedraw, true);
- this.ResizeRedraw = true;
- }
- //用户选择样式
- TabControlDisplayManager _DisplayManager = TabControlDisplayManager.Custom;
- [System.ComponentModel.DefaultValue(typeof(TabControlDisplayManager), "Custom")]
- public TabControlDisplayManager DisplayManager
- {
- get
- {
- return this._DisplayManager;
- }
- set
- {
- if (this._DisplayManager != value)
- {
- if (this._DisplayManager.Equals(TabControlDisplayManager.Custom))
- {
- this.SetStyle(ControlStyles.UserPaint, true);
- this.ItemSize = new Size(0, 15);
- this.Padding = new Point(9, 0);
- }
- else
- {
- this.ItemSize = new Size(0, 0);
- this.Padding = new Point(6, 3);
- this.SetStyle(ControlStyles.UserPaint, false);
- }
- }
- }
- }
- protected override void OnPaintBackground(PaintEventArgs pevent)
- {
- //是否绘制该控件
- if (this.DesignMode == true)
- {
- LinearGradientBrush backBrush = new LinearGradientBrush(
- this.Bounds,
- SystemColors.ControlLightLight,
- SystemColors.ControlLight,
- LinearGradientMode.Vertical);
- pevent.Graphics.FillRectangle(backBrush, this.Bounds);
- backBrush.Dispose();
- }
- else
- {
- this.PaintTransparentBackground(pevent.Graphics, this.ClientRectangle);
- }
- }
- protected void PaintTransparentBackground(Graphics g, Rectangle clipRect)
- {
- if ((this.Parent != null))
- {
- clipRect.Offset(this.Location);
- PaintEventArgs e = new PaintEventArgs(g, clipRect);
- GraphicsState state = g.Save();
- g.SmoothingMode = SmoothingMode.HighSpeed;
- try
- {
- g.TranslateTransform((float)-this.Location.X, (float)-this.Location.Y);
- this.InvokePaintBackground(this.Parent, e);
- this.InvokePaint(this.Parent, e);
- }
- finally
- {
- g.Restore(state);
- clipRect.Offset(-this.Location.X, -this.Location.Y);
- }
- }
- else
- {
- LinearGradientBrush backBrush = new LinearGradientBrush(this.Bounds, SystemColors.ControlLightLight, SystemColors.ControlLight, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
- g.FillRectangle(backBrush, this.Bounds);
- backBrush.Dispose();
- }
- }
- //将组件绘制到界面,加载的时候执行
- protected override void OnPaint(PaintEventArgs e)
- {
- // Paint the Background
- this.PaintTransparentBackground(e.Graphics, this.ClientRectangle);
- //绘制所有的tabPage
- this.PaintAllTheTabs(e);
- //绘制tabPage的边框
- this.PaintTheTabPageBorder(e);
- //选中的tabPage去除边框
- this.PaintTheSelectedTab(e);
- }
- //也是调用PaintTab循环绘制出来的
- private void PaintAllTheTabs(System.Windows.Forms.PaintEventArgs e)
- {
- if (this.TabCount > 0)
- {
- for (int index = 0; index < this.TabCount; index++)
- {
- this.PaintTab(e, index);
- }
- }
- }
- //绘制tab
- private void PaintTab(PaintEventArgs e, int index)
- {
- GraphicsPath path = this.GetPath(index);
- this.PaintTabBackground(e.Graphics, index, path);
- this.PaintTabBorder(e.Graphics, index, path);
- this.PaintTabText(e.Graphics, index);
- this.PaintTabImage(e.Graphics, index);
- }
- private void PaintTabBackground(Graphics graph, int index, GraphicsPath path)
- {
- Rectangle rect = this.GetTabRect(index);
- Brush buttonBrush =
- new LinearGradientBrush(
- rect,
- SystemColors.ControlLightLight,
- SystemColors.ControlLight,
- LinearGradientMode.Vertical);
- if (index == this.SelectedIndex)
- {
- buttonBrush = new SolidBrush(SystemColors.ControlLightLight);
- }
- graph.FillPath(buttonBrush, path);
- buttonBrush.Dispose();
- }
- //绘制边框
- private void PaintTabBorder(Graphics graph, int index, GraphicsPath path)
- {
- Pen borderPen = new Pen(SystemColors.ControlDark);
- if (index == this.SelectedIndex)
- {
- borderPen = new Pen(ThemedColors.ToolBorder);
- }
- graph.DrawPath(borderPen, path);
- borderPen.Dispose();
- }
- private void PaintTabImage(System.Drawing.Graphics graph, int index)
- {
- Image tabImage = null;
- if (this.TabPages[index].ImageIndex > -1 && this.ImageList != null)
- {
- tabImage = this.ImageList.Images[this.TabPages[index].ImageIndex];
- }
- else if (this.TabPages[index].ImageKey.Trim().Length > 0 && this.ImageList != null)
- {
- tabImage = this.ImageList.Images[this.TabPages[index].ImageKey];
- }
- if (tabImage != null)
- {
- Rectangle rect = this.GetTabRect(index);
- graph.DrawImage(tabImage, rect.Right - rect.Height + 5, 4, rect.Height - 2, rect.Height - 2);
- }
- }
- //绘制文字
- private void PaintTabText(Graphics graph, int index)
- {
- Rectangle rect = this.GetTabRect(index);
- Rectangle rect2 = new Rectangle(rect.Left + 8, rect.Top + 1, rect.Width - 6, rect.Height);
- if (index == 0)
- {
- rect2 = new Rectangle(rect.Left + rect.Height, rect.Top + 1, rect.Width - rect.Height, rect.Height);
- }
- else
- {
- rect2 = new Rectangle(rect.Left + rect.Height - 5, rect.Top + 1, rect.Width - rect.Height + 5, rect.Height);
- }
- string tabtext = this.TabPages[index].Text;
- StringFormat format = new StringFormat();
- //字符串的水平和垂直对齐方式
- format.Alignment = StringAlignment.Near;
- format.LineAlignment = StringAlignment.Center;
- format.Trimming = StringTrimming.EllipsisCharacter;
- Brush forebrush = null;
- if (this.TabPages[index].Enabled == false)
- {
- forebrush = SystemBrushes.ControlDark;
- }
- else
- {
- forebrush = SystemBrushes.ControlText;
- }
- Font tabFont = this.Font;
- //选中了之后文本加粗
- if (index == this.SelectedIndex)
- {
- tabFont = new Font(this.Font, FontStyle.Bold);
- if (index == 0)
- {
- //第一个的时候做特殊处理,调整文字的位置
- rect2 = new Rectangle(rect.Left + rect.Height, rect.Top + 1, rect.Width - rect.Height + 5, rect.Height);
- }
- }
- graph.DrawString(tabtext, tabFont, forebrush, rect2, format);
- }
- //绘制边框
- private void PaintTheTabPageBorder(System.Windows.Forms.PaintEventArgs e)
- {
- if (this.TabCount > 0)
- {
- Rectangle borderRect = this.TabPages[0].Bounds;
- borderRect.Inflate(1, 1);
- ControlPaint.DrawBorder(e.Graphics, borderRect, ThemedColors.ToolBorder, ButtonBorderStyle.Solid);
- }
- }
- //选中tabPage的时候去除下边框
- private void PaintTheSelectedTab(System.Windows.Forms.PaintEventArgs e)
- {
- Rectangle selrect;
- int selrectRight = 0;
- switch (this.SelectedIndex)
- {
- case -1:
- break;
- case 0:
- selrect = this.GetTabRect(this.SelectedIndex);
- selrectRight = selrect.Right;
- e.Graphics.DrawLine(SystemPens.ControlLightLight, selrect.Left + 2, selrect.Bottom + 1, selrectRight + 8, selrect.Bottom + 1);
- break;
- default:
- selrect = this.GetTabRect(this.SelectedIndex);
- selrectRight = selrect.Right;
- e.Graphics.DrawLine(SystemPens.ControlLightLight, selrect.Left + 11, selrect.Bottom + 1, selrectRight + 5, selrect.Bottom + 1);
- break;
- }
- }
- //绘制tabPage的形状
- private GraphicsPath GetPath(int index)
- {
- GraphicsPath path = new GraphicsPath();
- path.Reset();
- Rectangle rect = this.GetTabRect(index);
- if (index == 0)
- { //画线的地方,采用两个点相连的方式,需要设置x1,y1,x2,y2四个坐标
- path.AddLine(rect.Left + 1, rect.Bottom + 1, rect.Left + rect.Height, rect.Top + 2);
- path.AddLine(rect.Left + rect.Height + 4, rect.Top, rect.Right + 8, rect.Top);
- path.AddLine(rect.Right + 10, rect.Top + 2, rect.Right + 10, rect.Bottom + 1);
- }
- else
- {
- if (index == this.SelectedIndex)
- {
- path.AddLine(rect.Left + 12, rect.Top + 5, rect.Left + 15, rect.Top + 2);
- path.AddLine(rect.Left + 19, rect.Top, rect.Right + 8, rect.Top);
- path.AddLine(rect.Right + 10, rect.Top + 2, rect.Right + 10, rect.Bottom + 1);
- path.AddLine(rect.Right + 10, rect.Bottom + 1, rect.Left + 11, rect.Bottom + 1);
- }
- else
- {
- path.AddLine(rect.Left + 12, rect.Top + 5, rect.Left + 15, rect.Top + 2);
- path.AddLine(rect.Left + 19, rect.Top, rect.Right + 8, rect.Top);
- path.AddLine(rect.Right + 10, rect.Top + 2, rect.Right + 10, rect.Bottom + 1);
- path.AddLine(rect.Right + 10, rect.Bottom + 1, rect.Left + 11, rect.Bottom + 1);
- }
- }
- return path;
- }
- //枚举类型 多个风格的界面
- public enum TabControlDisplayManager
- {
- Default,
- Custom
- }
- [DllImport("user32.dll")]
- private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
- private const int WM_SETFONT = 0x30;
- private const int WM_FONTCHANGE = 0x1d;
- //在启动时
- protected override void OnCreateControl()
- {
- base.OnCreateControl();
- this.OnFontChanged(EventArgs.Empty);
- }
- //设置字体后自适应改变宽高
- protected override void OnFontChanged(EventArgs e)
- {
- base.OnFontChanged(e);
- IntPtr hFont = this.Font.ToHfont();
- SendMessage(this.Handle, WM_SETFONT, hFont, (IntPtr)(-1));
- SendMessage(this.Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
- this.UpdateStyles();
- this.ItemSize = new Size(0, this.Font.Height + 2);
- }
- }
- }
|