123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.ComponentModel;
- namespace LYFZ.ComponentLibrary
- {
- [ToolboxBitmap(typeof(TabControl))]
- public class TabControlEx : System.Windows.Forms.TabControl
- {
-
- private Color _baseColor = Color.White;
- private Color _backColor = Color.Transparent;
- private Color _borderColor = Color.White;
- private Color _pageColor = Color.White;
- private bool _isFocus;
- private Rectangle _btnArrowRect = Rectangle.Empty;
- private Image Icon;
- [Category("组件扩展属性"), DefaultValue(typeof(Color), "102, 180, 228")]
- public Color BaseColor
- {
- get
- {
- return this._baseColor;
- }
- set
- {
- this._baseColor = value;
- base.Invalidate(true);
- }
- }
- [Browsable(true), Category("组件扩展属性"), DefaultValue(typeof(Color), "Transparent")]
- public override Color BackColor
- {
- get
- {
- return this._backColor;
- }
- set
- {
- this._backColor = value;
- base.Invalidate(true);
- }
- }
- [Category("组件扩展属性"), DefaultValue(typeof(Color), "102, 180, 228")]
- public Color BorderColor
- {
- get
- {
- return this._borderColor;
- }
- set
- {
- this._borderColor = value;
- base.Invalidate(true);
- }
- }
- [Category("组件扩展属性"), Description("所有TabPage的背景颜色")]
- public Color PageColor
- {
- get
- {
- return this._pageColor;
- }
- set
- {
- this._pageColor = value;
- if (base.TabPages.Count > 0)
- {
- for (int i = 0; i < base.TabPages.Count; i++)
- {
- base.TabPages[i].BackColor = this._pageColor;
- }
- }
- base.Invalidate(true);
- }
- }
- public TabControlEx()
- {
- base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
- base.SizeMode = TabSizeMode.Fixed;
- base.ItemSize = new Size(70, 36);
- base.UpdateStyles();
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics g = e.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.InterpolationMode = InterpolationMode.HighQualityBilinear;
- g.TextRenderingHint = TextRenderingHint.AntiAlias;
- this.DrawBackground(g);
- this.DrawTabPages(g);
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- if (!base.DesignMode && e.Button == MouseButtons.Left && this._btnArrowRect.Contains(e.Location))
- {
- this._isFocus = true;
- base.Invalidate(this._btnArrowRect);
- }
- }
- protected override void WndProc(ref Message m)
- {
- if (m.Msg != 123)
- {
- base.WndProc(ref m);
- }
- }
- private void DrawBackground(Graphics g)
- {
- int arg_0E_0 = base.ClientRectangle.Width;
- int arg_1D_0 = base.ClientRectangle.Height;
- int arg_2D_0 = this.DisplayRectangle.Height;
- Color backColor = base.Enabled ? this._backColor : SystemColors.Control;
- using (SolidBrush brush = new SolidBrush(backColor))
- {
- g.FillRectangle(brush, base.ClientRectangle);
- }
- }
- private void DrawImage(Graphics g, Image image, Rectangle rect)
- {
- g.DrawImage(image, new Rectangle(rect.X, rect.Y, 5, rect.Height), 0, 0, 5, image.Height, GraphicsUnit.Pixel);
- g.DrawImage(image, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, rect.Height), 5, 0, image.Width - 10, image.Height, GraphicsUnit.Pixel);
- g.DrawImage(image, new Rectangle(rect.X + rect.Width - 5, rect.Y, 5, rect.Height), image.Width - 5, 0, 5, image.Height, GraphicsUnit.Pixel);
- }
- private void DrawTabPages(Graphics g)
- {
- using (SolidBrush brush = new SolidBrush(this._pageColor))
- {
- int x = 2;
- int y = base.ItemSize.Height;
- int width = base.Width - 2;
- int height = base.Height - base.ItemSize.Height;
- g.FillRectangle(brush, x, y, width, height);
- g.DrawRectangle(new Pen(this._borderColor), x, y, width - 1, height - 1);
- }
- Rectangle tabRect = Rectangle.Empty;
- Point cursorPoint = base.PointToClient(Control.MousePosition);
- for (int i = 0; i < base.TabCount; i++)
- {
- TabPage page = base.TabPages[i];
- tabRect = base.GetTabRect(i);
- Color arg_AB_0 = Color.Yellow;
- this.Icon = ((base.TabPages[i].ImageIndex != -1 && base.ImageList != null) ? base.ImageList.Images[base.TabPages[i].ImageIndex] : null);
- Image baseTabHeaderImage = Properties.Resources.tab02;
- Image btnArrowImage = null;
- if (base.SelectedIndex == i)
- {
- baseTabHeaderImage =Properties.Resources.tab01;
- Point contextMenuLocation = base.PointToScreen(new Point(this._btnArrowRect.Left, this._btnArrowRect.Top + this._btnArrowRect.Height + 5));
- ContextMenuStrip contextMenuStrip = base.TabPages[i].ContextMenuStrip;
- if (contextMenuStrip != null)
- {
- contextMenuStrip.Closed -= new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
- contextMenuStrip.Closed += new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
- if (contextMenuLocation.X + contextMenuStrip.Width > Screen.PrimaryScreen.WorkingArea.Width - 20)
- {
- contextMenuLocation.X = Screen.PrimaryScreen.WorkingArea.Width - contextMenuStrip.Width - 50;
- }
- if (tabRect.Contains(cursorPoint))
- {
- if (this._isFocus)
- {
- btnArrowImage = Properties.Resources.tab01; ;
- contextMenuStrip.Show(contextMenuLocation);
- }
- else
- {
- btnArrowImage = Properties.Resources.tab01; ;
- }
- this._btnArrowRect = new Rectangle(tabRect.X + tabRect.Width - btnArrowImage.Width, tabRect.Y, btnArrowImage.Width, btnArrowImage.Height);
- }
- else
- {
- if (this._isFocus)
- {
- btnArrowImage = Properties.Resources.tab01; ;
- contextMenuStrip.Show(contextMenuLocation);
- }
- }
- }
- }
- else
- {
- if (tabRect.Contains(cursorPoint))
- {
- baseTabHeaderImage = Properties.Resources.tab02; ;
- }
- }
- if (baseTabHeaderImage != null)
- {
- if (base.SelectedIndex == i)
- {
- if (base.SelectedIndex == base.TabCount - 1)
- {
- tabRect.Inflate(2, 0);
- }
- else
- {
- tabRect.Inflate(1, 0);
- }
- }
- this.DrawImage(g, baseTabHeaderImage, tabRect);
- if (btnArrowImage != null)
- {
- g.DrawImage(btnArrowImage, this._btnArrowRect);
- }
- }
- if (this.Icon != null)
- {
- g.DrawImage(this.Icon, tabRect.X + (tabRect.Width - this.Icon.Width) / 2, tabRect.Y + (tabRect.Height - this.Icon.Height) / 2);
- }
- TextRenderer.DrawText(g, page.Text, page.Font, tabRect, page.ForeColor);
- }
- }
- private void contextMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e)
- {
- this._isFocus = false;
- base.Invalidate(this._btnArrowRect);
- }
- }
- }
|