/
SelectedCellsBorderHelper.cs
143 lines (125 loc) · 4.94 KB
/
SelectedCellsBorderHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils.Paint;
namespace WindowsApplication1
{
public class SelectedCellsBorderHelper
{
public SelectedCellsBorderHelper(GridView gridView) {
_GridView = gridView;
gridView.GridControl.PaintEx += GridControl_PaintEx;
}
private void GridControl_PaintEx(object sender, PaintExEventArgs e) {
if(IsCopyMode)
DrawCopyBorder(e);
else
DrawRegularBorder(e);
}
private bool _IsCopyMode;
private GridView _GridView;
public GridView GridView {
get { return _GridView; }
set { _GridView = value; }
}
private GridControl _GridControl;
public GridControl GridControl {
get { return _GridView.GridControl; }
}
public bool IsCopyMode {
get { return _IsCopyMode; }
set { _IsCopyMode = value; OnCopyModeChanged(); }
}
XPaint _paint = new XPaint();
private XPaint Paint {
get
{
return _paint;
}
}
private int _DragRectSize = 6;
int lineWidth = 3;
private Rectangle GetSelectionBounds() {
int width = 0;
int height = 0;
Rectangle rTop = Rectangle.Empty;
bool shouldReturn = false;
GridView view = GridControl.FocusedView as GridView;
GridViewInfo info = view.GetViewInfo() as GridViewInfo;
GridCell[] gridCells = view.GetSelectedCells();
if(gridCells.Length == 0) {
shouldReturn = true;
return Rectangle.Empty;
}
Brush hb = Brushes.Black;
List<GridCellInfo> visibleColl = new List<GridCellInfo>();
foreach(GridRowInfo row in info.RowsInfo) {
if(row is GridGroupRowInfo) {
continue;
}
GridCellInfoCollection coll = (row as GridDataRowInfo).Cells;
foreach(GridCellInfo cell in coll)
visibleColl.Add(cell);
}
List<GridCellInfo> collection = new List<GridCellInfo>();
foreach(GridCell cell in gridCells)
foreach(GridCellInfo cellInfo in visibleColl)
if(cellInfo.RowInfo != null && cellInfo.ColumnInfo != null)
if(cell.RowHandle == cellInfo.RowHandle && cell.Column == cellInfo.Column)
collection.Add(cellInfo);
if(collection.Count == 0) {
shouldReturn = true;
return Rectangle.Empty;
}
rTop = GetCellRect(view, collection[0].RowHandle, collection[0].Column);
Rectangle rBottom = GetCellRect(view, collection[collection.Count - 1].RowHandle, collection[collection.Count - 1].Column);
if(rTop.Y > rBottom.Y)
height = rTop.Y - rBottom.Bottom;
else
height = rBottom.Bottom - rTop.Y;
if(rTop.X <= rBottom.X)
width = rBottom.Right - rTop.X;
else
width = rTop.X - rBottom.Right;
return new Rectangle(rTop.X, rTop.Y, width, height);
}
private void DrawCopyBorder(PaintExEventArgs e) {
Rectangle rect = GetSelectionBounds();
Paint.DrawFocusRectangle(e.Cache.Graphics, rect, Color.Black, Color.White);
}
private void DrawRegularBorder(PaintExEventArgs e) {
Rectangle rect = GetSelectionBounds();
e.Cache.DrawRectangle(e.Cache.GetPen(Color.Black, lineWidth), rect);
if(GridView.GetSelectedCells().Length == 1)
DrawDragImage(e, rect);
}
public Rectangle GetDragRect() {
return GetDragRect(GetSelectionBounds());
}
private Rectangle GetDragRect(Rectangle rect) {
return new Rectangle(rect.Right - _DragRectSize, rect.Bottom - _DragRectSize, _DragRectSize, _DragRectSize);
}
private void DrawDragImage(PaintExEventArgs e, Rectangle rect) {
e.Cache.FillRectangle(Brushes.Black, GetDragRect(rect));
}
Rectangle GetCellRect(GridView view, int rowHandle, GridColumn column) {
GridViewInfo info = (GridViewInfo)view.GetViewInfo();
GridCellInfo cell = info.GetGridCellInfo(rowHandle, column);
if(cell != null)
return cell.Bounds;
return Rectangle.Empty;
}
private void OnCopyModeChanged() {
GridView.InvalidateRow(GridView.FocusedRowHandle);
}
}
}