-
Notifications
You must be signed in to change notification settings - Fork 5
/
DragDropAgent.cs
153 lines (121 loc) · 3.58 KB
/
DragDropAgent.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
using System;
using System.Windows.Forms;
namespace Subindex
{
/// <summary>
///
/// </summary>
public class DragDropAgent//4 groupBox control in runtime dragDrop support, Written By Linnet 2006-4-6
{
private DragDropAgent()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 得到一个包含首层父控件的控件的对象名,如form1下groupbox1中的textbox1,将返回form1.textbox1
/// </summary>
/// <param name="sender">要获得对象名的控件实例</param>
/// <returns></returns>
public static string getFullName(Control sender)
{
string name=sender.Name;
while (sender.Parent!=null)
sender=sender.Parent;
return sender.Name+"."+name;
}
/// <summary>
/// 设置指定GroupBox控件的拖放操作,根据CanDrag和CanDrop决定拖出/放入的支持
/// </summary>
/// <param name="sender">要设置拖放特性的GroupBox对象</param>
/// <param name="CanDrag">是否可以拖出</param>
/// <param name="CanDrop">是否可以放入</param>
public static void DragDropHook(GroupBox sender,bool CanDrag,bool CanDrop)
{
if (CanDrag) DragHook(sender);
if (CanDrop) DropHook(sender);
gutPosition(sender);
}
/// <summary>
/// 设置指定GroupBox控件的拖放操作,同时支持拖出和放入
/// </summary>
/// <param name="sender">要设置拖放特性的GroupBox对象</param>
public static void DragDropHook(GroupBox sender)
{
DragDropHook(sender,true,true);
}
private static void DragHook(GroupBox sender)
{
sender.MouseDown+=new MouseEventHandler(Do_Drag);
}
private static void DropHook(GroupBox sender)
{
sender.AllowDrop=true;
sender.DragDrop+=new DragEventHandler(Drag_Drop);
sender.DragEnter+=new DragEventHandler(Drag_Enter);
}
private static void Drag_Enter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(new GroupBox().GetType()))
e.Effect = DragDropEffects.Move ;
}
private static void Do_Drag(object sender, MouseEventArgs e)
{
(sender as Control) .DoDragDrop(sender, DragDropEffects.Move);
}
private static void Drag_Drop(object sender, System.Windows.Forms.DragEventArgs e)
{
try
{
GroupBox self=sender as GroupBox;
GroupBox ct= e.Data.GetData(self.GetType()) as GroupBox;
if (self==ct) return;
Control parent=self.Parent;
int ord=parent.Controls.GetChildIndex(self);
int ordself=self.Top>ct.Top?ord+1:ord-1;
DockStyle dss=self.Dock;
self.Dock=ct.Dock;;
ct.Dock=dss;
// System.Drawing.Point dps=self.Location;
// self.Location=ct.Location;
// ct.Location=dps;
if (ordself>ord)
{
setPosition(self,ordself);
setPosition(ct,ord);
}
else
{
setPosition(ct,ord);
setPosition(self,ordself);
}
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.Message,"Error");
}
}
private static void gutPosition(GroupBox sender)
{
string order=AppSettings.ReadSetting(getFullName(sender)+":Order");
string dock=AppSettings.ReadSetting(getFullName(sender)+":Dock");
try
{
int Position=int.Parse(order);
sender.Parent.Controls.SetChildIndex(sender,Position);
Position=int.Parse(dock);
sender.Dock=(DockStyle)Position;
}
catch
{
}
}
private static void setPosition(GroupBox sender,int Position)
{
sender.Parent.Controls.SetChildIndex(sender,Position);
AppSettings.WriteSetting(getFullName(sender)+":Order",Position.ToString());
AppSettings.WriteSetting(getFullName(sender)+":Dock",((int)sender.Dock).ToString());
}
}
}