This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortableCheckedListBox.cs
199 lines (193 loc) · 8.72 KB
/
SortableCheckedListBox.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Drawing;
namespace Sample
{
class CustomCheckedlistBox : CheckedListBoxControl
{
int dragSourceIndex, dragTargetIndex;
bool isDraging;
bool[] allowMove;
Rectangle dragBeginRect;
public CustomCheckedlistBox()
: base()
{
SelectionMode = SelectionMode.None;
TabStop = false;
CheckOnClick = true;
isDraging = false;
}
protected virtual internal void SetAllowMoving(int index, bool value)
{
allowMove[index] = value;
}
protected virtual internal void CreateAllowMovingArray() { allowMove = new bool[Items.Count]; }
protected virtual bool IsDraging { get { return isDraging; } set { if (isDraging != value) isDraging = value; } }
protected override BaseStyleControlViewInfo CreateViewInfo() { return new CustomCheckedListBoxViewInfo(this); }
protected override BaseControlPainter CreatePainter() { return new CustomPainterCheckedListBox(); }
protected new virtual CustomCheckedListBoxViewInfo ViewInfo { get { return base.ViewInfo as CustomCheckedListBoxViewInfo; } }
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
int index = this.IndexFromPoint(e.Location);
if (index >= 0 && index < Items.Count && !allowMove[index]) return;
IsDraging = false;
dragSourceIndex = index;
dragTargetIndex = dragSourceIndex;
if (dragSourceIndex != -1)
{
Size dragSize = SystemInformation.DragSize;
dragBeginRect = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
dragBeginRect = Rectangle.Empty;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (!IsDraging || dragBeginRect == Rectangle.Empty) base.OnMouseUp(e);
if (dragBeginRect == Rectangle.Empty) return;
if (dragSourceIndex != -1 && dragTargetIndex != dragSourceIndex)
ChangeItemsPositionCore(dragSourceIndex, dragTargetIndex);
dragBeginRect = Rectangle.Empty;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
if (dragBeginRect != Rectangle.Empty && !dragBeginRect.Contains(e.X, e.Y))
{
IsDraging = true;
dragTargetIndex = this.IndexFromPoint(e.Location);
if (dragTargetIndex == -1)
if (e.Y < this.ViewInfo.GetItemRectangle(0).Bottom) dragTargetIndex = 0;
CheckedListBoxViewInfo.CheckedItemInfo info = ViewInfo.GetItemByIndex(dragSourceIndex);
if (info != null) info.PaintAppearance.ForeColor = Color.Red;
ViewInfo.MarkItem(dragTargetIndex, dragSourceIndex);
}
}
protected virtual void ChangeItemsPositionCore(int source, int target)
{
CorrectAllowMove(source, target);
if (target == -1)
{
Items.Add(Items[source]);
}
else
{
Items.Insert(target, Items[source]);
if (source > target) source++;
}
Items.RemoveAt(source);
}
protected virtual void CorrectAllowMove(int source, int target)
{
bool b = allowMove[source];
if (target == -1) target = Items.Count-1;
if (target > source)
for (int i = source; i < target; i++) allowMove[i] = allowMove[i+1];
else
for (int i = source; i > target; i--) allowMove[i] = allowMove[i-1];
allowMove[target] = b;
}
}
public class CustomCheckedListBoxViewInfo : CheckedListBoxViewInfo
{
Color dragDropLineColor;
public CustomCheckedListBoxViewInfo(CheckedListBoxControl listBox) : base(listBox) { }
protected override ItemInfo CreateItemInfo(Rectangle bounds, object item, string text, int index)
{
CheckedItemInfo info = base.CreateItemInfo(bounds, item, text, index) as CheckedItemInfo;
CustomCheckedItemInfo patchedInfo = new CustomCheckedItemInfo(info);
return patchedInfo;
}
public virtual Color DragDropLineColor { get { return dragDropLineColor; } set { if (dragDropLineColor != value) dragDropLineColor = value; } }
protected internal virtual void UnderlineItem(int index)
{
CustomCheckedItemInfo info = base.GetItemByIndex(index) as CustomCheckedItemInfo;
if (info != null)
info.IsUnderLine = true;
}
protected internal virtual void OverlineItem(int index)
{
CustomCheckedItemInfo info = base.GetItemByIndex(index) as CustomCheckedItemInfo;
if (info != null)
info.IsOverLine = true;
}
protected virtual internal int ItemCountAccessMethod() { return ItemCount; }
protected internal virtual void DropLine()
{
foreach (CustomCheckedItemInfo info in VisibleItems)
info.DropLine();
}
protected internal virtual void MarkItem(int targetIndex, int sourceIndex)
{
DropLine();
dragDropLineColor = Color.Red;
if ((targetIndex == sourceIndex) || (targetIndex == sourceIndex + 1) ||
(sourceIndex == ItemCount - 1 && targetIndex == -1)) dragDropLineColor = Color.LightGray;
if (targetIndex == -1)
UnderlineItem( ItemCount - 1);
else
OverlineItem(targetIndex);
if (targetIndex > 0)
UnderlineItem(targetIndex - 1);
OwnerControl.Invalidate();
}
public class CustomCheckedItemInfo : CheckedItemInfo
{
bool isUnderLine, isOverLine;
public CustomCheckedItemInfo(BaseListBoxControl ownerControl, Rectangle rect, object item, string text, int index, CheckState checkState, bool enabled)
: base(ownerControl, rect, item, text, index, checkState, enabled)
{
DropLine();
}
public CustomCheckedItemInfo(CheckedItemInfo info)
: this(info.ListBoxControl, info.Bounds, info.Item, info.Text, info.Index, info.CheckArgs.CheckState, info.Enabled)
{
this.CheckArgs.Assign(info.CheckArgs);
this.TextRect = info.TextRect;
}
protected virtual internal bool IsUnderLine { get { return isUnderLine; } set { isUnderLine = value; } }
protected virtual internal bool IsOverLine { get { return isOverLine; } set { isOverLine = value; } }
protected virtual internal void DropLine()
{
isUnderLine = false;
isOverLine = false;
}
}
}
public class CustomPainterCheckedListBox : PainterCheckedListBox
{
const int lineWidth = 1;
protected override void DrawItemCore(ControlGraphicsInfoArgs info, BaseListBoxViewInfo.ItemInfo itemInfo, ListBoxDrawItemEventArgs e)
{
base.DrawItemCore(info, itemInfo, e);
Sample.CustomCheckedListBoxViewInfo.CustomCheckedItemInfo customInfo =
itemInfo as Sample.CustomCheckedListBoxViewInfo.CustomCheckedItemInfo;
if (customInfo == null) return;
Rectangle rec = new Rectangle(itemInfo.Bounds.Location, new Size(itemInfo.Bounds.Width, lineWidth));
Color lineColor = ((CustomCheckedListBoxViewInfo)info.ViewInfo).DragDropLineColor;
if (customInfo.IsOverLine)
{
if (customInfo.Index == 0) rec.Height++;
e.Cache.FillRectangle(lineColor, rec);
}
if (customInfo.IsUnderLine)
{
rec.Offset(0, itemInfo.Bounds.Height - lineWidth);
if (customInfo.Index == ((CustomCheckedListBoxViewInfo)info.ViewInfo).ItemCountAccessMethod() - 1) rec.Height++;
e.Cache.FillRectangle(lineColor, rec);
}
}
}
}