using DevExpress.Utils.Extensions; using DevExpress.XtraCharts.Native; using System; using System.ComponentModel.DataAnnotations; using System.Drawing; namespace IStation.WinFrmUI { public class TimeSmallBlock { /// /// 绘制类型 /// public enum eBlockType { [Display(Name = "常规")] Default } #region 变量 /// /// 是否是边界的盒子 /// public bool IsLineStartBlck { get; set; } = false; public bool IsLineEndBlck { get; set; } = false; /// /// 下标 /// public int Index { get { return _index; } } private int _index; /// /// 是否可选 /// public bool IsSelectedAble { get; set; } = true; /// /// /// private int _hour; public int Hour { get { return _hour; } } /// /// /// private int _minute; public int Minute { get { return _minute; } } /// /// 矩形左上角坐标 /// 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; } } /// /// 矩形 /// public RectangleF Rectangle { get { return _rect; } } private RectangleF _rect; /// /// 边框宽度 /// public int BorderWidth { get => _borderWidth; } private int _borderWidth = 1; /// /// 默认颜色 /// 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); } /// /// /// public int PumpCount { get => _pumpCount; set { _pumpCount = value; _defaultColor = GetColor(value); } } private int _pumpCount; /// /// 边框色 /// public Color BorderColor { get => _borderColor; } private Color _borderColor = Color.Black; /// /// 字体色 /// public Color FontColor { get => _fontColor; } private Color _fontColor = Color.Black; /// /// 选中状态 /// public bool IsSelected { get => _isSelected; set { _isSelected = value; } } private bool _isSelected = false; /// /// 文本信息 /// public string Text { get => _text; set => _text = value; } private string _text; /// /// 文本可见性 /// public bool TextVisible { get => _textVisible; set => _textVisible = value; } private bool _textVisible = false ; /// /// 绘制类型 /// public eBlockType BlockType { get { return _blckType; } set { _blckType = value; } } private eBlockType _blckType; #endregion /// /// /// /// /// 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; } } /// /// 调整大小 /// public void Resize(int x, int y ) { _loc_point = new Point(x,y); _rect = new Rectangle(_loc_point, new Size(_width, _height)); } /// /// 绘制 /// 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)); } } } /// /// 绘制文本 /// 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); } } /// /// 缩放后字体 /// 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); } } }