using DevExpress.Utils.Extensions;
using DevExpress.XtraCharts.Native;
using Eventech.Utils.Images;
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
namespace IStation.WinFrmUI
{
public class HourSmallBlock
{
#region 变量
///
/// 是否是边界的盒子
///
public bool IsLineStartBlck { get; set; } = false;
public bool IsLineEndBlck { get; set; } = false;
///
/// 下标
///
public int Hour
{
get { return _hour; }
}
private int _hour;
///
/// 是否可选
///
public bool IsSelectedAble { get; set; } = true;
///
/// 矩形左上角坐标
///
public Point LocPoint { get { return _loc_point; } }
private Point _loc_point;
private Point _text_point;
private int _width = 35;
public int Width { get { return _width; } }
private int _height = 25;
public int Height { get { return _height; } }
///
/// 矩形
///
public RectangleF Rectangle
{
get { return _rect; }
}
private RectangleF _rect;
///
/// 边框宽度
///
public int BorderWidth
{
get => _borderWidth;
}
private int _borderWidth = 1;
///
/// 默认颜色
///
public Color DefaultColor
{
get => _defaultColor;
set => _defaultColor = value;
}
private Color _defaultColor = Color.White;
///
/// 边框色
///
public Color BorderColor
{
get => _borderColor;
}
private Color _borderColor = Color.Black;
///
/// 字体色
///
public Color FontColor
{
get => _fontColor;
}
private Color _fontColor = Color.Black;
///
/// 选中状态
///
public bool IsSelected
{
get => _selected;
set
{
_selected = value;
}
}
private bool _selected = false;
///
/// 文本信息
///
public string TextContent
{
get => _textContent;
set => _textContent = value;
}
private string _textContent;
///
/// 文本可见性
///
public bool TextVisible
{
get => _textVisible;
set => _textVisible = value;
}
private bool _textVisible = false;
public double Mark { get; set; } = -1;
#endregion
///
///
///
///
///
public HourSmallBlock(int hour)
{
this._hour = hour;
if (hour % 2 == 0)
{
this._textContent = hour.ToString();
this._textVisible = true;
this._text_point = new Point(10, 10);
}
}
///
/// 调整大小
///
public void Resize(int x, int y)
{
_loc_point = new Point(x, y);
_text_point = new Point(x + 10, y+5);
_rect = new Rectangle(_loc_point, new Size(_width, _height));
}
///
/// 绘制
///
public void Draw(Graphics g)
{
Color drawColor = _defaultColor;
//if (drawColor == _currentBlockColor)
// return;
using (Pen dashpen = new Pen(Color.Red, 1))
using (Brush gpBrush = new SolidBrush(drawColor))
using (var pen = new Pen(_borderColor, _borderWidth))
{
dashpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
dashpen.DashPattern = new float[] { 2, 2 };
//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
{
g.DrawLine(dashpen, _rect.LeftTop(), _rect.LeftBottom());
}
if (IsLineEndBlck)
{
g.DrawLine(pen, _rect.RightTop(), _rect.RightBottom());
}
g.FillRectangle(gpBrush, _rect);
DrawTimeTxt(g);
if (Mark > 0)
{
DrawMarkTxt(g);
}
if (!IsSelectedAble)
{
g.DrawLine(pen, _loc_point, new PointF(_rect.Right, _rect.Bottom));
}
}
}
///
/// 绘制文本
///
private void DrawMarkTxt(Graphics g)
{
if (Mark <= 0)
return;
using (var font = new Font("微软雅黑", 6))//字体
using (var brush = new SolidBrush(Color.Red))//笔刷
{
var format = new StringFormat();//文本格式
format.LineAlignment = StringAlignment.Near;//垂直居中
format.Alignment = StringAlignment.Center;//水平居中
var point = new Point(_loc_point.X + this.Width - 7 , _loc_point.Y+2);
//var scaleFont = ScaleFont(g, txt, _rect.Size, font);//缩放后字体
//g.DrawString(txt, font, brush, _rect, format);//绘制文本(文本,字体,笔刷,矩形,文本格式)
g.DrawString(Mark.ToString(), font, brush, point);
}
}
///
/// 绘制文本
///
private void DrawTimeTxt(Graphics g )
{
if (!_textVisible || string.IsNullOrEmpty(_textContent))
return;
using (var font = new Font("微软雅黑", 7, FontStyle.Bold))//字体
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(_textContent, font, brush, _text_point);
}
}
///
/// 缩放后字体
///
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);
}
///
/// 包含点
///
public bool ContainsPoint(Point point)
{
return _rect.Contains(point);
}
}
}