-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathControlExtensions.cs
155 lines (139 loc) · 4.29 KB
/
ControlExtensions.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DigitalPlatform.Forms
{
/// <summary>
/// Control 的扩展方法
/// </summary>
public static class ControlExtention
{
#region 不会被自动 Dispose 的 子 Control,放在这里托管,避免内存泄漏
public static void AddFreeControl(List<Control> controls,
Control control)
{
if (controls.IndexOf(control) == -1)
controls.Add(control);
}
public static void RemoveFreeControl(List<Control> controls,
Control control)
{
controls.Remove(control);
}
public static void DisposeFreeControls(List<Control> controls)
{
foreach (Control control in controls)
{
control.Dispose();
}
controls.Clear();
}
#endregion
// 清除控件集合。使用这个函数便于 Dispose() 所清除的控件对象
public static void ClearControls(this Control parent,
bool bDispose = false)
{
if (parent.Controls.Count == 0)
return;
List<Control> controls = new List<Control>();
foreach (Control control in parent.Controls)
{
controls.Add(control);
}
parent.Controls.Clear();
if (bDispose)
{
foreach (Control control in controls)
{
control.Dispose();
}
}
}
public static string GetText(this Control control)
{
if (control.InvokeRequired == false)
return control.Text;
string strText = "";
control.Invoke((Action)(() =>
{
strText = control.Text;
}));
return strText;
}
public static void SetText(this Control control, string strText)
{
if (control.InvokeRequired == false)
control.Text = strText;
else
{
control.Invoke((Action)(() =>
{
control.Text = strText;
}));
}
}
}
/// <summary>
/// ScrollableControl 的扩展方法
/// </summary>
public static class ScrollableControlExtention
{
public static void SetAutoScrollPosition(this ScrollableControl control, Point p)
{
if (p.Y + control.ClientSize.Height > control.DisplayRectangle.Height)
p.Y = control.DisplayRectangle.Height - control.ClientSize.Height;
if (p.X + control.ClientSize.Width > control.DisplayRectangle.Width)
p.X = control.DisplayRectangle.Width - control.ClientSize.Width;
control.AutoScrollPosition = new Point(p.X, p.Y);
}
}
public static class WebBrowserExtension
{
public static void ScrollToEnd(this WebBrowser webBrowser1)
{
if (webBrowser1.Document != null
&& webBrowser1.Document.Window != null
&& webBrowser1.Document.Body != null)
webBrowser1.Document.Window.ScrollTo(
0,
webBrowser1.Document.Body.ScrollRectangle.Height);
}
}
public static class SplitContainerExtension
{
public static float GetSplitterState(this SplitContainer container)
{
float fValue = (float)container.SplitterDistance /
(
container.Orientation == Orientation.Horizontal ?
(float)container.Height
:
(float)container.Width
)
;
return fValue;
}
public static void SetSplitterState(this SplitContainer container,
float fValue)
{
try
{
container.SplitterDistance = (int)Math.Ceiling(
(
container.Orientation == Orientation.Horizontal ?
(float)container.Height
:
(float)container.Width
)
* fValue);
}
catch
{
}
}
}
}