// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-10-09
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace HZH_Controls.Controls
{
///
/// Class UCSplitLabel.
/// Implements the
///
///
public class UCSplitLabel : Label
{
///
/// Gets or sets the text.
///
/// The text.
[Localizable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
ResetMaxSize();
}
}
///
/// 获取或设置控件显示的文字的字体。
///
/// The font.
///
///
///
///
///
///
[Localizable(true)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
ResetMaxSize();
}
}
///
/// 获取或设置大小,该大小是 可以指定的下限。
///
/// The minimum size.
[Localizable(true)]
public override Size MinimumSize
{
get
{
return base.MinimumSize;
}
set
{
base.MinimumSize = value;
ResetMaxSize();
}
}
///
/// 获取或设置大小,该大小是 可以指定的上限。
///
/// The maximum size.
[Localizable(true)]
public override Size MaximumSize
{
get
{
return base.MaximumSize;
}
set
{
base.MaximumSize = value;
ResetMaxSize();
}
}
///
/// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容。
///
/// true if [automatic size]; otherwise, false.
///
///
///
///
///
///
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}
///
/// The line color
///
private Color lineColor = LineColors.Light;
///
/// Gets or sets the color of the line.
///
/// The color of the line.
public Color LineColor
{
get { return lineColor; }
set
{
lineColor = value;
Invalidate();
}
}
///
/// Resets the maximum size.
///
private void ResetMaxSize()
{
using (var g = this.CreateGraphics())
{
var _width = Width;
var size = g.MeasureString(string.IsNullOrEmpty(Text) ? "A" : Text, Font);
if (MinimumSize.Height != (int)size.Height)
MinimumSize = new Size(base.MinimumSize.Width, (int)size.Height);
if (MaximumSize.Height != (int)size.Height)
MaximumSize = new Size(base.MaximumSize.Width, (int)size.Height);
this.Width = _width;
}
}
///
/// Initializes a new instance of the class.
///
public UCSplitLabel()
: base()
{
if (ControlHelper.IsDesignMode())
{
Text = "分割线";
Font = new Font("微软雅黑", 8f);
}
this.AutoSize = false;
Padding = new Padding(20, 0, 0, 0);
MinimumSize = new System.Drawing.Size(150, 10);
PaddingChanged += UCSplitLabel_PaddingChanged;
this.Width = 200;
}
///
/// Handles the PaddingChanged event of the UCSplitLabel control.
///
/// The source of the event.
/// The instance containing the event data.
void UCSplitLabel_PaddingChanged(object sender, EventArgs e)
{
if (Padding.Left < 20)
{
Padding = new Padding(20, Padding.Top, Padding.Right, Padding.Bottom);
}
}
///
/// Handles the event.
///
/// 包含事件数据的 。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
var size = g.MeasureString(Text, Font);
g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2), new PointF(Padding.Left - 2, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));
g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(Padding.Left + size.Width + 1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2), new PointF(Width - Padding.Right, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));
}
}
}