123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- 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 Image _titleBackground = Properties.Resources.tab02;
- private Color _baseColor = Color.White;
- private Color _backColor = Color.Transparent;
- private Color _borderColor = Color.Gray;
- 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), "209, 205, 209")]
- 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)
- {
- Image selectImage = null;
- Image notSelected = null;
- this._pageColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBgColor;//关闭选项卡页面颜色属性
- this._baseColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBgColor;
- this._borderColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBorderColor;//关闭边框颜色属性
- using (SolidBrush brush = new SolidBrush(this._pageColor))
- {
- int x = 0;
- int y = 0;
- int width = 0;
- int height = 0;
-
- switch (this.Alignment) {
- case TabAlignment.Top:
- x = 0;
- y = base.ItemSize.Height+2;
- width = base.Width;
- height = base.Height - base.ItemSize.Height-2;
- selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_top_select;
- notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_top;
- break;
- case TabAlignment.Bottom:
- x = 0;
- y = 0;
- width = base.Width;
- height = base.Height - base.ItemSize.Height-2;
- selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_bottom_select;
- notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_bottom;
- break;
- case TabAlignment.Left:
- x = base.ItemSize.Height+2;
- y = 0;
- width = base.Width - base.ItemSize.Height-2;
- height = base.Height;
- selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_left_select;
- notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_left;
- break;
- case TabAlignment.Right:
- x = 0;
- y = 0;
- width = base.Width - base.ItemSize.Height-2;
- height = base.Height;
- selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_right_select;
- notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_right;
- break;
- }
- 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);
- /* if (base.TabPages.Count > 0)
- {
- for (int i = 0; i < base.TabPages.Count; i++)
- {
- base.TabPages[i].BackColor = this._pageColor;
- }
- }*/
- 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 =notSelected ; //未选时状态
- Image btnArrowImage = null;
- if (base.SelectedIndex == i)
- {
- baseTabHeaderImage = selectImage;// Resources.tab_dots_down;当前选项卡
- 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; //Resources.tab_dots_mouseover;鼠标移上时状态
- }
-
- }*/
- if (baseTabHeaderImage != null)
- {
- if (base.SelectedIndex == i)
- {
- switch (this.Alignment)
- {
- case TabAlignment.Top:
- tabRect.Inflate(0, 1);
- tabRect.Location = new Point(tabRect.Location.X, 2);
- break;
- case TabAlignment.Bottom:
- tabRect.Inflate(0, 1);
- tabRect.Location = new Point(tabRect.Location.X, base.Height - base.ItemSize.Height - 4);
- break;
- case TabAlignment.Left:
- tabRect.Inflate(1, 0);
- tabRect.Location = new Point(2, tabRect.Location.Y);
- break;
- case TabAlignment.Right:
- tabRect.Inflate(1, 0);
- tabRect.Location = new Point(base.Width - base.ItemSize.Height - 4, tabRect.Location.Y);
- break;
- }
- //关闭选项卡页面颜色属性
- if (page.BackColor != this._pageColor)
- {
- page.BackColor = this._pageColor;
- }
- }
- else
- {
- if (this.Alignment == TabAlignment.Top)
- {
- if (this.Appearance != TabAppearance.Normal)
- {
- tabRect.Location = new Point(tabRect.Location.X, 2);
- }
- }
- }
- 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);
- }
- }
- }
|