using DevExpress.Office.Utils;
using IStation.WinFrmUI.CalcErQu;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace IStation.WinFrmUI
{
public partial class HourBlockRangePanel : Panel
{
public HourBlockRangePanel()
{
InitializeMenu();
}
///
/// 绘制对象列表
///
List _block_item_list = new List();
public List GetUseHourValues()
{
List list = new List();
foreach (var v in _block_item_list)
{
if (v.IsSelected)
{
list.Add( v.Hour );
}
}
return list;
}
public Dictionary GetLimitHourValues()
{
Dictionary list = new Dictionary();
foreach (var v in _block_item_list)
{
if (v.Mark>0)
{
list[v.Hour] = v.Mark;
}
}
return list;
}
private void MenuSetLevelHeight_Click(object sender, EventArgs e)
{
if (_clickRightBlock == null) return;
InputWaterLevelDlg frm = new InputWaterLevelDlg(); ;
if (frm.ShowDialog() != DialogResult.OK)
return;
_clickRightBlock.Mark = frm.WaterLevel;
this.Invalidate(new System.Drawing.Rectangle(
(int)_clickRightBlock.Rectangle.X, (int)_clickRightBlock.Rectangle.Y,
_clickRightBlock.Width, _clickRightBlock.Height));//刷新局部区域
}
private void MenuClearLevelHeight_Click(object sender, EventArgs e)
{
if (_clickRightBlock == null) return;
_clickRightBlock.Mark = -1;
this.Invalidate(new System.Drawing.Rectangle(
(int)_clickRightBlock.Rectangle.X, (int)_clickRightBlock.Rectangle.Y,
_clickRightBlock.Width, _clickRightBlock.Height));//刷新局部区域
}
///
/// 初始化数据
///
public void InitialTimeBlock()
{
_block_item_list = new List();
for (var time = 0; time < 24; time = time + 1)
{
var block = new HourSmallBlock(time);
_block_item_list.Add(block);
}
if (!_block_item_list.Any())
return;
InitialItemSize();
}
///
///
///
///
///
///
public void SetRangePumpCount(int start, int end, bool isSelect, bool isRefreshUI)
{
foreach (var item in _block_item_list)
{
if (item.Hour >= start && item.Hour < end)
{
if (isSelect)
{
item.IsSelected = true;
item.DefaultColor = SelectColor;
}
else
{
item.IsSelected = false;
item.DefaultColor = Color.White;
}
}
}
if (isRefreshUI)
this.Refresh();
}
///
/// 初始化项大小
///
public void InitialItemSize()
{
if (_block_item_list == null || !_block_item_list.Any())
return;
int last_right_x = 10;
int last_loc_y = 10;
for (int i = 0; i < _block_item_list.Count; i++)
{
var item = _block_item_list[i];
var current_loc_x = last_right_x;
if (item.TextVisible)
{
if (current_loc_x + item.Width > this.Width - 50)
{
last_loc_y = last_loc_y + item.Height + 10;
current_loc_x = 10;
item.IsLineStartBlck = true;
_block_item_list[i - 1].IsLineEndBlck = true;
}
}
else
{
if (current_loc_x + item.Width > this.Width - 20)
{
last_loc_y = last_loc_y + item.Height + 10;
current_loc_x = 10;
item.IsLineStartBlck = true;
_block_item_list[i - 1].IsLineEndBlck = true;
}
}
if (i == 0)
{
item.IsLineStartBlck = true;
}
if (i == _block_item_list.Count - 1)
{
item.IsLineEndBlck = true;
}
last_right_x = current_loc_x + item.Width;
item.Resize(current_loc_x, last_loc_y);
}
this.Refresh();
}
///
///
///
///
private HourSmallBlock GetCaptureBlock(Point point)
{
for (int index = 0; index < _block_item_list.Count; index++)
{
var item = _block_item_list[index];
if (!item.ContainsPoint(point))
continue;
return item;
}
return null;
}
#region override
HourSmallBlock _clickRightBlock = null;
HourSmallBlock _startSelBlock = null;
HourSmallBlock _endSelBlock = null;
private ContextMenuStrip contextMenuStrip1;
///
/// 是否是选择状态
///
bool _isSelectBlockIng = false;
///
/// 鼠标点击事件
///
///
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Clicks == 2)
{
_clickRightBlock = null;
var tipSelBlock = GetCaptureBlock(e.Location);
if (tipSelBlock == null)
return;
if(tipSelBlock.IsSelected)
{
tipSelBlock.DefaultColor = Color.White;
tipSelBlock.IsSelected = false;
}
else
{
tipSelBlock.DefaultColor = SelectColor;
tipSelBlock.IsSelected = true ;
}
this.Invalidate(new System.Drawing.Rectangle(
(int)tipSelBlock.Rectangle.X, (int)tipSelBlock.Rectangle.Y,
tipSelBlock.Width, tipSelBlock.Height));//刷新局部区域
if (OnFreshTotalHour != null)
{
int hhh = 0;
foreach (var v in _block_item_list)
{
if (v.IsSelected)
{
hhh++;
}
}
OnFreshTotalHour.Invoke(hhh);
}
}
else if (e.Button == MouseButtons.Left)
{
_clickRightBlock = null;
_isSelectBlockIng = true;
_startSelBlock = GetCaptureBlock(e.Location);
}
else if (e.Button == MouseButtons.Right)
{
_isSelectBlockIng = false ;
_clickRightBlock = GetCaptureBlock(e.Location);
if (_clickRightBlock == null) return;
contextMenuStrip1.Show(this,e.Location);
}
base.OnMouseDown(e);
}
//
public static Color SelectColor = Color.FromArgb(50, Color.Blue);
///
/// 鼠标移动事件
///
///
protected override void OnMouseMove(MouseEventArgs e)
{
if (!_isSelectBlockIng)
return;
if (_startSelBlock == null)
{
return;
}
_endSelBlock = GetCaptureBlock(e.Location);
if (_endSelBlock == null)
return;
if (_endSelBlock.Hour > _startSelBlock.Hour)
{
foreach (var item in _block_item_list)
{
if (item.Hour >= _startSelBlock.Hour && item.Hour <= _endSelBlock.Hour)
{
if (!item.IsSelected)
{
item.IsSelected = true;
item.DefaultColor = SelectColor;
this.Invalidate(new System.Drawing.Rectangle(
(int)item.Rectangle.X, (int)item.Rectangle.Y,
item.Width, item.Height));//刷新局部区域
}
}
}
}
else
{
foreach (var item in _block_item_list)
{
if (item.Hour >= _endSelBlock.Hour && item.Hour <= _startSelBlock.Hour)
{
if (item.IsSelected)
{
item.IsSelected = false;
item.DefaultColor = Color.White;
this.Invalidate(new System.Drawing.Rectangle(
(int)item.Rectangle.X, (int)item.Rectangle.Y,
item.Width, item.Height));//刷新局部区域
}
}
}
}
if (OnFreshTotalHour != null)
{
int hhh = 0;
foreach (var v in _block_item_list)
{
if (v.IsSelected)
{
hhh++;
}
}
OnFreshTotalHour.Invoke(hhh);
}
base.OnMouseMove(e);
//base.Invalidate();
}
public Action OnFreshTotalHour = null;
///
/// 鼠标弹出事件
///
///
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
//foreach (var item in _block_item_list)
//{
// if (item.DrawType == HourSmallBlock.eDrawType.Lock)
// continue;
//}
_endSelBlock = null;
_startSelBlock = null;
_isSelectBlockIng = false;
}
}
///
///
///
///
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
}
///
/// 重绘事件
///
///
protected override void OnPaint(PaintEventArgs e)
{
if (DesignMode)
return;
base.OnPaint(e);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//抗锯齿
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 // 设置GDI高质量模式抗锯齿
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
if (_block_item_list != null && _block_item_list.Any())
{
foreach (var rect in _block_item_list)
{
rect.Draw(e.Graphics);
}
}
}
///
///
///
///
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
//InitialItemSize();
this.Refresh();
}
#endregion
private void InitializeMenu()
{
var components = new System.ComponentModel.Container();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(components);
var toolStripTextBox1 = new System.Windows.Forms.ToolStripButton();
var toolStripTextBox2 = new System.Windows.Forms.ToolStripButton();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
toolStripTextBox1 ,toolStripTextBox2});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(120, 54);
//
// toolStripTextBox1
//
toolStripTextBox1.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
toolStripTextBox1.Name = "toolStripTextBox1";
toolStripTextBox1.Size = new System.Drawing.Size(100, 23);
toolStripTextBox1.Text = "设置水位要求";
toolStripTextBox1.Click += MenuSetLevelHeight_Click;
toolStripTextBox2.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
toolStripTextBox2.Name = "toolStripTextBox2";
toolStripTextBox2.Size = new System.Drawing.Size(100, 23);
toolStripTextBox2.Text = "清理水位要求";
toolStripTextBox2.Click += MenuClearLevelHeight_Click;
this.contextMenuStrip1.ResumeLayout(false);
this.contextMenuStrip1.PerformLayout();
this.ResumeLayout(false);
}
}
}