|
using DevExpress.Utils.Extensions;
|
using DevExpress.XtraCharts.Native;
|
using System;
|
using System.ComponentModel.DataAnnotations;
|
using System.Drawing;
|
|
namespace IStation.WinFrmUI
|
{
|
public class TimeSmallBlock
|
{
|
/// <summary>
|
/// 绘制类型
|
/// </summary>
|
public enum eBlockType
|
{
|
[Display(Name = "常规")]
|
Default
|
}
|
|
#region 变量
|
/// <summary>
|
/// 是否是边界的盒子
|
/// </summary>
|
public bool IsLineStartBlck { get; set; } = false;
|
public bool IsLineEndBlck { get; set; } = false;
|
/// <summary>
|
/// 下标
|
/// </summary>
|
public int Index
|
{
|
get { return _index; }
|
}
|
private int _index;
|
|
/// <summary>
|
/// 是否可选
|
/// </summary>
|
public bool IsSelectedAble { get; set; } = true;
|
/// <summary>
|
///
|
/// </summary>
|
private int _hour;
|
public int Hour
|
{
|
get { return _hour; }
|
}
|
/// <summary>
|
///
|
/// </summary>
|
private int _minute;
|
public int Minute
|
{
|
get { return _minute; }
|
}
|
/// <summary>
|
/// 矩形左上角坐标
|
/// </summary>
|
public Point LocPoint { get { return _loc_point; } }
|
private Point _loc_point;
|
|
|
private int _width;
|
private int _height;
|
public int Width { get { return _width; } }
|
public int Height { get { return _height; } }
|
|
/// <summary>
|
/// 矩形
|
/// </summary>
|
public RectangleF Rectangle
|
{
|
get { return _rect; }
|
}
|
private RectangleF _rect;
|
|
/// <summary>
|
/// 边框宽度
|
/// </summary>
|
public int BorderWidth
|
{
|
get => _borderWidth;
|
}
|
private int _borderWidth = 1;
|
|
/// <summary>
|
/// 默认颜色
|
/// </summary>
|
private Color _defaultColor = Color.White;
|
|
|
|
//
|
public static Color GetColor(int count)
|
{
|
if (count == 0)
|
return Color.White;
|
if (count == 1)
|
return Color.FromArgb(152, 245, 255);
|
if (count == 2)
|
return Color.FromArgb(122, 197, 205);
|
if (count == 3)
|
return Color.FromArgb(83, 134, 139);
|
if (count == 4)
|
return Color.FromArgb(0, 0, 205);
|
|
return Color.FromArgb(50, Color.Blue);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public int PumpCount
|
{
|
get => _pumpCount;
|
set
|
{
|
_pumpCount = value;
|
_defaultColor = GetColor(value);
|
}
|
}
|
private int _pumpCount;
|
|
/// <summary>
|
/// 边框色
|
/// </summary>
|
public Color BorderColor
|
{
|
get => _borderColor;
|
}
|
private Color _borderColor = Color.Black;
|
|
/// <summary>
|
/// 字体色
|
/// </summary>
|
public Color FontColor
|
{
|
get => _fontColor;
|
}
|
private Color _fontColor = Color.Black;
|
|
/// <summary>
|
/// 选中状态
|
/// </summary>
|
public bool IsSelected
|
{
|
get => _isSelected;
|
set
|
{
|
_isSelected = value;
|
}
|
}
|
private bool _isSelected = false;
|
|
|
/// <summary>
|
/// 文本信息
|
/// </summary>
|
public string Text
|
{
|
get => _text;
|
set => _text = value;
|
}
|
private string _text;
|
|
/// <summary>
|
/// 文本可见性
|
/// </summary>
|
public bool TextVisible
|
{
|
get => _textVisible;
|
set => _textVisible = value;
|
}
|
private bool _textVisible = false ;
|
|
/// <summary>
|
/// 绘制类型
|
/// </summary>
|
public eBlockType BlockType
|
{
|
get { return _blckType; }
|
set { _blckType = value; }
|
}
|
private eBlockType _blckType;
|
|
|
|
#endregion
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="time"></param>
|
public TimeSmallBlock(int index, int hour,int minute)
|
{
|
this._index = index;
|
this._hour = hour;
|
this._minute = minute;
|
|
if (_minute == 0)
|
{
|
if (_hour > 0)
|
{
|
this._text = _hour.ToString();
|
this._textVisible = true;
|
}
|
}
|
|
this._height = 20;
|
if (_textVisible || index == 0)
|
{
|
this._width = 20;
|
}
|
else
|
{
|
this._width = 10;
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 调整大小
|
/// </summary>
|
public void Resize(int x, int y )
|
{
|
_loc_point = new Point(x,y);
|
_rect = new Rectangle(_loc_point, new Size(_width, _height));
|
}
|
|
|
/// <summary>
|
/// 绘制
|
/// </summary>
|
public void Draw(Graphics g)
|
{
|
Color backColor;
|
|
if (this._isSelected)
|
{
|
backColor = Color.DarkBlue;
|
_fontColor = Color.White;
|
}
|
else
|
{
|
backColor = _defaultColor;
|
_fontColor = Color.DarkBlue;
|
if(PumpCount == 4)
|
{
|
_fontColor = Color.White;
|
}
|
}
|
|
|
using (Pen dashpen = new Pen(_borderColor, _borderWidth))
|
using (Brush gpBrush = new SolidBrush(backColor))
|
using (Pen pen = new Pen(_borderColor, _borderWidth))
|
{
|
|
|
//g.DrawRectangle(pen, _rect);
|
g.DrawLine(pen, _rect.LeftTop(), _rect.RightTop());
|
g.DrawLine(pen, _rect.LeftBottom(), _rect.RightBottom());
|
|
|
if (IsLineStartBlck)
|
{
|
g.DrawLine(pen, _rect.LeftTop(), _rect.LeftBottom());
|
}
|
else
|
{
|
dashpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
|
dashpen.DashPattern = new float[] { 2, 2 };
|
g.DrawLine(dashpen, _rect.LeftTop(), _rect.LeftBottom());
|
}
|
|
if (IsLineEndBlck)
|
{
|
g.DrawLine(pen, _rect.RightTop(), _rect.RightBottom());
|
}
|
|
g.FillRectangle(gpBrush, _rect);
|
|
DrawTxt(g, _text);
|
|
|
|
if (!IsSelectedAble)
|
{
|
g.DrawLine(pen, _loc_point, new PointF(_rect.Right, _rect.Bottom));
|
}
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
/// 绘制文本
|
/// </summary>
|
private void DrawTxt(Graphics g, string txt)
|
{
|
if (!_textVisible || string.IsNullOrEmpty(txt))
|
return;
|
using (var font = new Font("微软雅黑", 6))//字体
|
using (var brush = new SolidBrush(_fontColor))//笔刷
|
{
|
//var format = new StringFormat();//文本格式
|
//format.LineAlignment = StringAlignment.Center;//垂直居中
|
//format.Alignment = StringAlignment.Center;//水平居中
|
|
//var scaleFont = ScaleFont(g, txt, _rect.Size, font);//缩放后字体
|
//g.DrawString(txt, font, brush, _rect, format);//绘制文本(文本,字体,笔刷,矩形,文本格式)
|
g.DrawString(txt, font, brush, _loc_point);
|
}
|
}
|
|
/// <summary>
|
/// 缩放后字体
|
/// </summary>
|
private Font ScaleFont(Graphics g, string longString, Size Rec, Font PreferedFont)//画布,文本,矩形,字体
|
{
|
SizeF RealSize = g.MeasureString(longString, PreferedFont);//文本大小
|
float HeightScaleRatio = Rec.Height / RealSize.Height;//高度缩放比例
|
float WidthScaleRatio = Rec.Width / RealSize.Width;//宽度缩放比例
|
float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;//按高度缩放||按宽度缩放
|
float ScaleFontSize = PreferedFont.Size * ScaleRatio;//缩放后的字体大小
|
return new Font(PreferedFont.FontFamily, ScaleFontSize);
|
}
|
|
|
/// <summary>
|
/// 包含点
|
/// </summary>
|
public bool ContainsPoint(Point point)
|
{
|
return _rect.Contains(point);
|
}
|
|
|
|
|
}
|
}
|