/// <summary> /// 鼠标按下事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnMouseDown(object sender, MouseEventArgs e) { if (_SelectionRow != null || _SelectionItem != null || _DragItem != null) { _SelectionRow = null; _SelectionItem = null; _DragItem = null; Invalidate(); } _IsMouseDown = e.Button == MouseButtons.Left && true; if (_IsMouseDown) { _SelectionRange = Rectangle.Empty; _SelectionTimeRange = null; _DragStartLocation = e.Location; if (IsHoverDateSplitLine(e.Location) && !IsHoverItem(e.Location)) { _IsAjustSize = true; } else if (IsHoverItem(e.Location)) { _IsDragItem = true; _DragItem = GetItem(e.Location); } } }
/// <summary> /// 画项 /// </summary> /// <param name="graphics"></param> /// <param name="titleFormat"></param> /// <param name="item"></param> /// <param name="range"></param> protected virtual void DrawItem(Graphics graphics, StringFormat titleFormat, GanttChartViewItem item, RectangleF range) { var clip = new RectangleF(range.X, (range.Y < ColumnHeight ? ColumnHeight : range.Y) + 1, range.Width, (range.Y < ColumnHeight ? (range.Height - (ColumnHeight - range.Y)) : range.Height) - 2); graphics.SetClip(clip); if (item.Ranges != null && item.Ranges.Any()) { graphics.FillRectangle(this.Brushes.ItemBackBrush, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); graphics.DrawRectangle(this.Brushes.ItemBackPen, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); var subs = item.Ranges.OrderBy(r => r.StartTime); foreach (var sub in subs) { DrawItemTimeRange(graphics, item, sub); } } else { graphics.FillRectangle(this.Brushes.ItemSubRangeBackBrush, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); graphics.DrawRectangle(this.Brushes.ItemSubRangeBackPen, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); } if (item == _SelectionItem) { var textBrush = item.IsWarnning ? this.Brushes.ItemWarnningTextBrush : this.Brushes.ItemSelectionTextBrush; graphics.DrawLine(this.Brushes.ItemStartFlagPen, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight / 2, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight); graphics.DrawRectangle(this.Brushes.ItemSelectionBorderPen, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); graphics.DrawString(item.Title, ItemTextFont, textBrush, new RectangleF(item.Left - _HScrollBar.Value + 2, item.Top - _VScrollBar.Value + 1, 0, ItemHeight - 2), titleFormat); } else if (_SelectionItem != null && SelectionItemSameComparator != null && SelectionItemSameComparator(item, _SelectionItem)) { var textBrush = item.IsWarnning ? this.Brushes.ItemWarnningTextBrush : this.Brushes.ItemSelectionTextBrush; graphics.DrawLine(this.Brushes.ItemStartFlagPen, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight / 2, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight); if (IsShowItemBorder) { graphics.DrawRectangle(this.Brushes.ItemBorderPen, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); } graphics.DrawString(item.Title, ItemTextFont, textBrush, new RectangleF(item.Left - _HScrollBar.Value + 2, item.Top - _VScrollBar.Value + 1, 0, ItemHeight - 2), titleFormat); } else { var textBrush = item.IsWarnning ? this.Brushes.ItemWarnningTextBrush : this.Brushes.ItemTextBrush; graphics.DrawLine(this.Brushes.ItemStartFlagPen, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight / 2, item.Left - _HScrollBar.Value + 1, item.Top - _VScrollBar.Value + ItemHeight); if (IsShowItemBorder) { graphics.DrawRectangle(this.Brushes.ItemBorderPen, item.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, Math.Max(item.Width - 2, 2), ItemHeight); } graphics.DrawString(item.Title, ItemTextFont, textBrush, new RectangleF(item.Left - _HScrollBar.Value + 2, item.Top - _VScrollBar.Value + 1, 0, ItemHeight - 2), titleFormat); } graphics.ResetClip(); }
/// <summary> /// 鼠标双击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnMouseDoubleClick(object sender, MouseEventArgs e) { if (IsHoverItem(e.Location)) { _SelectionItem = GetItem(e.Location); Invalidate(); if (ItemDoubleClick != null) { if (_IsShowTooltip) { _IsShowTooltip = false; _ShowTooltipItem = null; _ShowTooltipPoint = Point.Empty; } ItemDoubleClick(this, new GanttChartItemDoubleClickEventArgs(_SelectionItem)); } } }
/// <summary> /// 画项的时间范围 /// </summary> /// <param name="graphics"></param> /// <param name="item"></param> /// <param name="sub"></param> protected virtual void DrawItemTimeRange(Graphics graphics, GanttChartViewItem item, TimeRange sub) { var width = sub.Width; var right = sub.Left + sub.Width; if (right == item.Left + item.Width) { width = width - 2; if (width < 1) { return; } } if (item == _SelectionItem) { graphics.FillRectangle(this.Brushes.ItemSelectionSubRangeBackBrush, sub.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, width, ItemHeight); graphics.DrawRectangle(this.Brushes.ItemSelectionSubRangeBackPen, sub.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, width, ItemHeight); } else { graphics.FillRectangle(this.Brushes.ItemSubRangeBackBrush, sub.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, width, ItemHeight); graphics.DrawRectangle(this.Brushes.ItemSubRangeBackPen, sub.Left - _HScrollBar.Value, item.Top - _VScrollBar.Value, width, ItemHeight); } }
/// <summary> /// 鼠标滚动事件 /// </summary> /// <param name="e"></param> protected override void OnMouseWheel(MouseEventArgs e) { if (_IsShowTooltip) { _IsShowTooltip = false; _ShowTooltipItem = null; _ShowTooltipPoint = Point.Empty; } //int i = e.Delta > 0 ? -1 : 1; var scrollbar = _IsShiftPress ? _HScrollBar : (ScrollBar)_VScrollBar; var h = scrollbar.Value - (int)(e.Delta * MouseWheelSensitivity); if (h > scrollbar.Maximum) { h = scrollbar.Maximum; } if (h < scrollbar.Minimum) { h = scrollbar.Minimum; } scrollbar.Value = h; Invalidate(); base.OnMouseWheel(e); }
/// <summary> /// 初始化 /// </summary> /// <param name="item"></param> public GanttChartItemDoubleClickEventArgs(GanttChartViewItem item) { Item = item; }
/// <summary> /// 鼠标移动事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnMouseMove(object sender, MouseEventArgs e) { if (_IsMouseDown) { if (_IsShowTooltip) { _IsShowTooltip = false; _ShowTooltipItem = null; _ShowTooltipPoint = Point.Empty; Invalidate(); } if (_IsDragItem) { } else if (_DragStartLocation.X > RowHeaderWidth && _DragStartLocation.Y > ColumnHeight) { //TODO: 画选择框 _DragEndLocation = e.Location; Invalidate(); } return; } else if (IsHoverDateSplitLine(e.Location) && !IsHoverItem(e.Location)) { Cursor = Cursors.VSplit; } //else if (IsHoverRowHeader(e)) //{ // Cursor = DefaultCursor; //} else if (IsHoverItem(e.Location)) { Cursor = Cursors.Hand; //TODO:显示Tooltip if (!_IsShowTooltip || GetItem(e.Location) != _ShowTooltipItem) { _IsShowTooltip = true; _ShowTooltipItem = GetItem(e.Location); _ShowTooltipPoint = ComputeTooltipLocation(e.Location); Invalidate(); return; } else { return; } } else { Cursor = DefaultCursor; } if (_IsShowTooltip) { _IsShowTooltip = false; _ShowTooltipItem = null; _ShowTooltipPoint = Point.Empty; Invalidate(); } }