Commit 513941ae HZH

添加Tab控件

1 个父辈 f6221286
......@@ -210,31 +210,6 @@ namespace HZH_Controls.Controls
get { return _selectedText; }
private set
{
//if (_source == null || _source.Count <= 0)
//{
// _selectText = "";
// _selectValue = "";
// _selectIndex = -1;
// _selectItem = new KeyValuePair<string, string>();
//}
//else
//{
// _selectText = "";
// _selectValue = "";
// _selectIndex = -1;
// _selectItem = new KeyValuePair<string, string>();
// for (int i = 0; i < _source.Count; i++)
// {
// if (_source[i].Value == value)
// {
// _selectText = value;
// _selectValue = _source[i].Key;
// _selectIndex = i;
// _selectItem = _source[i];
// break;
// }
// }
//}
_selectedText = value;
lblInput.Text = _selectedText;
txtInput.Text = _selectedText;
......@@ -417,25 +392,6 @@ namespace HZH_Controls.Controls
base.FillColor = Color.White;
base.RectColor = Color.FromArgb(220, 220, 220);
}
//if (this.Parent != null && BackColor == Color.Transparent)
//{
// Control c = this;
// while (true)
// {
// if (c.BackColor == Color.Transparent)
// {
// c = c.Parent;
// }
// else
// {
// txtInput.BackColor = c.BackColor;
// base.FillColor = c.BackColor;
// base.RectColor = c.BackColor;
// break;
// }
// }
//}
}
}
}
......@@ -25,7 +25,9 @@ namespace HZH_Controls.Controls
set
{
_DataSource = value;
int intWidth = ControlHelper.GetStringWidth(value.Value, lblTitle.CreateGraphics(), lblTitle.Font);
var g=lblTitle.CreateGraphics();
int intWidth = ControlHelper.GetStringWidth(value.Value, g, lblTitle.Font);
g.Dispose();
if (intWidth < 50)
intWidth = 50;
this.Width = intWidth + 20;
......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
public class TabControlExt : TabControl
{
public TabControlExt()
: base()
{
SetStyles();
this.Multiline = true;
}
private void SetStyles()
{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
base.UpdateStyles();
}
private Color _backColor = Color.White;
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(typeof(Color), "White")]
public override Color BackColor
{
get { return _backColor; }
set
{
_backColor = value;
base.Invalidate(true);
}
}
private Color _borderColor = Color.FromArgb(232, 232, 232);
[DefaultValue(typeof(Color), "232, 232, 232")]
[Description("TabContorl边框色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate(true);
}
}
private Color _headSelectedBackColor = Color.FromArgb(255, 85, 51);
[DefaultValue(typeof(Color), "255, 85, 51")]
[Description("TabPage头部选中后的背景颜色")]
public Color HeadSelectedBackColor
{
get { return _headSelectedBackColor; }
set { _headSelectedBackColor = value; }
}
private Color _headSelectedBorderColor = Color.FromArgb(232, 232, 232);
[DefaultValue(typeof(Color), "232, 232, 232")]
[Description("TabPage头部选中后的边框颜色")]
public Color HeadSelectedBorderColor
{
get { return _headSelectedBorderColor; }
set { _headSelectedBorderColor = value; }
}
private Color _headerBackColor = Color.White;
[DefaultValue(typeof(Color), "White")]
[Description("TabPage头部默认背景颜色")]
public Color HeaderBackColor
{
get { return _headerBackColor; }
set { _headerBackColor = value; }
}
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);
}
}
/// <summary>
/// TabContorl 背景色设置
/// </summary>
/// <param name="g"></param>
/// <param name="clipRect"></param>
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);
//新加片段,待测试
using (SolidBrush brush = new SolidBrush(_backColor))
{
clipRect.Inflate(1, 1);
g.FillRectangle(brush, clipRect);
}
}
}
else
{
System.Drawing.Drawing2D.LinearGradientBrush backBrush = new System.Drawing.Drawing2D.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
base.OnPaint(e);
this.PaintTransparentBackground(e.Graphics, this.ClientRectangle);
this.PaintAllTheTabs(e);
this.PaintTheTabPageBorder(e);
this.PaintTheSelectedTab(e);
}
private void PaintAllTheTabs(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > 0)
{
for (int index = 0; index < this.TabCount; index++)
{
this.PaintTab(e, index);
}
}
}
private void PaintTab(System.Windows.Forms.PaintEventArgs e, int index)
{
GraphicsPath path = this.GetTabPath(index);
this.PaintTabBackground(e.Graphics, index, path);
this.PaintTabBorder(e.Graphics, index, path);
this.PaintTabText(e.Graphics, index);
this.PaintTabImage(e.Graphics, index);
}
/// <summary>
/// 设置选项卡头部颜色
/// </summary>
/// <param name="graph"></param>
/// <param name="index"></param>
/// <param name="path"></param>
private void PaintTabBackground(System.Drawing.Graphics graph, int index, System.Drawing.Drawing2D.GraphicsPath path)
{
Rectangle rect = this.GetTabRect(index);
System.Drawing.Brush buttonBrush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, _headerBackColor, _headerBackColor, LinearGradientMode.Vertical); //非选中时候的 TabPage 页头部背景色
graph.FillPath(buttonBrush, path);
//if (index == this.SelectedIndex)
//{
// //buttonBrush = new System.Drawing.SolidBrush(_headSelectedBackColor);
// graph.DrawLine(new Pen(_headerBackColor), rect.Right+2, rect.Bottom, rect.Left + 1, rect.Bottom);
//}
buttonBrush.Dispose();
}
/// <summary>
/// 设置选项卡头部边框色
/// </summary>
/// <param name="graph"></param>
/// <param name="index"></param>
/// <param name="path"></param>
private void PaintTabBorder(System.Drawing.Graphics graph, int index, System.Drawing.Drawing2D.GraphicsPath path)
{
Pen borderPen = new Pen(_borderColor);// TabPage 非选中时候的 TabPage 头部边框色
if (index == this.SelectedIndex)
{
borderPen = new Pen(_headSelectedBorderColor); // TabPage 选中后的 TabPage 头部边框色
}
graph.DrawPath(borderPen, path);
borderPen.Dispose();
}
private void PaintTabImage(System.Drawing.Graphics g, 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);
g.DrawImage(tabImage, rect.Right - rect.Height - 4, 4, rect.Height - 2, rect.Height - 2);
}
}
private void PaintTabText(System.Drawing.Graphics graph, int index)
{
string tabtext = this.TabPages[index].Text;
System.Drawing.StringFormat format = new System.Drawing.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)
{
if (this.TabPages[index].Enabled != false)
{
forebrush = new SolidBrush(_headSelectedBackColor);
}
}
Rectangle rect = this.GetTabRect(index);
var txtSize = ControlHelper.GetStringWidth(tabtext, graph, tabFont);
Rectangle rect2 = new Rectangle(rect.Left + (rect.Width - txtSize) / 2 - 1, rect.Top, rect.Width, rect.Height);
graph.DrawString(tabtext, tabFont, forebrush, rect2, format);
}
/// <summary>
/// 设置 TabPage 内容页边框色
/// </summary>
/// <param name="e"></param>
private void PaintTheTabPageBorder(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > 0)
{
Rectangle borderRect = this.TabPages[0].Bounds;
//borderRect.Inflate(1, 1);
Rectangle rect = new Rectangle(borderRect.X - 2, borderRect.Y-1, borderRect.Width + 5, borderRect.Height+2);
ControlPaint.DrawBorder(e.Graphics, rect, this.BorderColor, ButtonBorderStyle.Solid);
}
}
/// <summary>
/// // TabPage 页头部间隔色
/// </summary>
/// <param name="e"></param>
private void PaintTheSelectedTab(System.Windows.Forms.PaintEventArgs e)
{
if (this.SelectedIndex == -1)
return;
Rectangle selrect;
int selrectRight = 0;
selrect = this.GetTabRect(this.SelectedIndex);
selrectRight = selrect.Right;
e.Graphics.DrawLine(new Pen(_headSelectedBackColor), selrect.Left, selrect.Bottom + 1, selrectRight, selrect.Bottom + 1);
}
private GraphicsPath GetTabPath(int index)
{
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.Reset();
Rectangle rect = this.GetTabRect(index);
switch (Alignment)
{
case TabAlignment.Top:
break;
case TabAlignment.Bottom:
break;
case TabAlignment.Left:
break;
case TabAlignment.Right:
break;
}
path.AddLine(rect.Left, rect.Top, rect.Left, rect.Bottom + 1);
path.AddLine(rect.Left, rect.Top, rect.Right , rect.Top);
path.AddLine(rect.Right , rect.Top, rect.Right , rect.Bottom + 1);
path.AddLine(rect.Right , rect.Bottom + 1, rect.Left, rect.Bottom + 1);
return path;
}
[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();
}
}
}
......@@ -98,6 +98,9 @@
<Compile Include="Controls\Menu\UCMenuParentItem.Designer.cs">
<DependentUpon>UCMenuParentItem.cs</DependentUpon>
</Compile>
<Compile Include="Controls\Tab\TabControlExt.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Helpers\ControlHelper.cs" />
<Compile Include="Controls\Btn\UCBtnExt.cs">
<SubType>UserControl</SubType>
......
......@@ -431,8 +431,6 @@ namespace HZH_Controls
System.Drawing.Graphics g,
System.Drawing.Font font)
{
try
{
string[] strs = strSource.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
float fltWidth = 0;
foreach (var item in strs)
......@@ -444,11 +442,6 @@ namespace HZH_Controls
return (int)fltWidth;
}
finally
{
g.Dispose();
}
}
#endregion
#region 动画特效
......
......@@ -28,20 +28,174 @@
/// </summary>
private void InitializeComponent()
{
this.tabControlExt1 = new HZH_Controls.Controls.TabControlExt();
this.tabPage7 = new System.Windows.Forms.TabPage();
this.tabPage8 = new System.Windows.Forms.TabPage();
this.tabPage9 = new System.Windows.Forms.TabPage();
this.tabPage10 = new System.Windows.Forms.TabPage();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.tabPage4 = new System.Windows.Forms.TabPage();
this.tabPage5 = new System.Windows.Forms.TabPage();
this.tabPage6 = new System.Windows.Forms.TabPage();
this.panel3.SuspendLayout();
this.tabControlExt1.SuspendLayout();
this.SuspendLayout();
//
// panel3
//
this.panel3.Controls.Add(this.tabControlExt1);
this.panel3.Size = new System.Drawing.Size(528, 321);
//
// tabControlExt1
//
this.tabControlExt1.Controls.Add(this.tabPage7);
this.tabControlExt1.Controls.Add(this.tabPage8);
this.tabControlExt1.Controls.Add(this.tabPage9);
this.tabControlExt1.Controls.Add(this.tabPage10);
this.tabControlExt1.Controls.Add(this.tabPage1);
this.tabControlExt1.Controls.Add(this.tabPage2);
this.tabControlExt1.Controls.Add(this.tabPage3);
this.tabControlExt1.Controls.Add(this.tabPage4);
this.tabControlExt1.Controls.Add(this.tabPage5);
this.tabControlExt1.Controls.Add(this.tabPage6);
this.tabControlExt1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControlExt1.ItemSize = new System.Drawing.Size(142, 50);
this.tabControlExt1.Location = new System.Drawing.Point(0, 0);
this.tabControlExt1.Multiline = true;
this.tabControlExt1.Name = "tabControlExt1";
this.tabControlExt1.SelectedIndex = 0;
this.tabControlExt1.Size = new System.Drawing.Size(528, 321);
this.tabControlExt1.SizeMode = System.Windows.Forms.TabSizeMode.FillToRight;
this.tabControlExt1.TabIndex = 0;
//
// tabPage7
//
this.tabPage7.Location = new System.Drawing.Point(4, 54);
this.tabPage7.Name = "tabPage7";
this.tabPage7.Padding = new System.Windows.Forms.Padding(3);
this.tabPage7.Size = new System.Drawing.Size(520, 263);
this.tabPage7.TabIndex = 6;
this.tabPage7.Text = "tabPage7";
this.tabPage7.UseVisualStyleBackColor = true;
//
// tabPage8
//
this.tabPage8.Location = new System.Drawing.Point(4, 54);
this.tabPage8.Name = "tabPage8";
this.tabPage8.Padding = new System.Windows.Forms.Padding(3);
this.tabPage8.Size = new System.Drawing.Size(419, 128);
this.tabPage8.TabIndex = 7;
this.tabPage8.Text = "tabPage8";
this.tabPage8.UseVisualStyleBackColor = true;
//
// tabPage9
//
this.tabPage9.Location = new System.Drawing.Point(4, 54);
this.tabPage9.Name = "tabPage9";
this.tabPage9.Padding = new System.Windows.Forms.Padding(3);
this.tabPage9.Size = new System.Drawing.Size(419, 128);
this.tabPage9.TabIndex = 8;
this.tabPage9.Text = "tabPage9";
this.tabPage9.UseVisualStyleBackColor = true;
//
// tabPage10
//
this.tabPage10.Location = new System.Drawing.Point(4, 54);
this.tabPage10.Name = "tabPage10";
this.tabPage10.Padding = new System.Windows.Forms.Padding(3);
this.tabPage10.Size = new System.Drawing.Size(419, 128);
this.tabPage10.TabIndex = 9;
this.tabPage10.Text = "tabPage10";
this.tabPage10.UseVisualStyleBackColor = true;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 54);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(520, 263);
this.tabPage1.TabIndex = 10;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 54);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(520, 263);
this.tabPage2.TabIndex = 11;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// tabPage3
//
this.tabPage3.Location = new System.Drawing.Point(4, 104);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
this.tabPage3.Size = new System.Drawing.Size(520, 213);
this.tabPage3.TabIndex = 12;
this.tabPage3.Text = "tabPage3";
this.tabPage3.UseVisualStyleBackColor = true;
//
// tabPage4
//
this.tabPage4.Location = new System.Drawing.Point(4, 104);
this.tabPage4.Name = "tabPage4";
this.tabPage4.Padding = new System.Windows.Forms.Padding(3);
this.tabPage4.Size = new System.Drawing.Size(520, 213);
this.tabPage4.TabIndex = 13;
this.tabPage4.Text = "tabPage4";
this.tabPage4.UseVisualStyleBackColor = true;
//
// tabPage5
//
this.tabPage5.Location = new System.Drawing.Point(4, 104);
this.tabPage5.Name = "tabPage5";
this.tabPage5.Padding = new System.Windows.Forms.Padding(3);
this.tabPage5.Size = new System.Drawing.Size(520, 213);
this.tabPage5.TabIndex = 14;
this.tabPage5.Text = "tabPage5";
this.tabPage5.UseVisualStyleBackColor = true;
//
// tabPage6
//
this.tabPage6.Location = new System.Drawing.Point(4, 104);
this.tabPage6.Name = "tabPage6";
this.tabPage6.Padding = new System.Windows.Forms.Padding(3);
this.tabPage6.Size = new System.Drawing.Size(520, 213);
this.tabPage6.TabIndex = 15;
this.tabPage6.Text = "tabPage6";
this.tabPage6.UseVisualStyleBackColor = true;
//
// FrmOKCancel1Test
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(427, 310);
this.ClientSize = new System.Drawing.Size(528, 445);
this.Name = "FrmOKCancel1Test";
this.Text = "FrmOKCancelTest";
this.Title = "测试修改密码";
this.panel3.ResumeLayout(false);
this.tabControlExt1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private HZH_Controls.Controls.TabControlExt tabControlExt1;
private System.Windows.Forms.TabPage tabPage7;
private System.Windows.Forms.TabPage tabPage8;
private System.Windows.Forms.TabPage tabPage9;
private System.Windows.Forms.TabPage tabPage10;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.TabPage tabPage4;
private System.Windows.Forms.TabPage tabPage5;
private System.Windows.Forms.TabPage tabPage6;
}
}
\ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!