forked from yaunqiying/EasyCharts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathXlWookbookHelper.cs
300 lines (261 loc) · 8.19 KB
/
XlWookbookHelper.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
//
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn_Graphics
{
public partial class XlWorkbookHelper
{
/////////////////////////////////////////////////////////
/// 私变区
/////////////////////////////////////////////////////////
//应用程序
private Excel.Application app;
private Excel.Window win;
public Excel.Window Win
{
get { return win; }
set { win = value; }
}
/////////////////////////////////////////////////////////
/// 创建区
/////////////////////////////////////////////////////////
public XlWorkbookHelper()
{
app = Globals.ThisAddIn.Application;
win = app.ActiveWindow;
//pan = win.ActivePane;
//cell = win.ActiveCell;
}
/////////////////////////////////////////////////////////
/// 字段区
/////////////////////////////////////////////////////////
//【窗口句柄】
public IntPtr HWND_Application
{
get
{
return new IntPtr(app.Hwnd);
}
}
public IntPtr HWND_Desktop
{
get
{
string path = "XLDESK";
return XlWorkbookHelper.GetHandle(HWND_Application, path);
}
}
public IntPtr HWND_WinWorkbook
{
get
{
string path = "XLDESK/EXCEL7";
return XlWorkbookHelper.GetHandle(HWND_Application, path);
}
}
public IntPtr HWND_HScrBar
{
get
{
return XlWorkbookHelper.GetHandle(HWND_WinWorkbook, "NUIScrollbar", "水平");
}
}
public IntPtr HWND_VScrBar
{
get
{
return XlWorkbookHelper.GetHandle(HWND_WinWorkbook, "NUIScrollbar", "垂直");
}
}
public IntPtr HWND_NavBar
{
get
{
return XlWorkbookHelper.GetHandle(HWND_WinWorkbook, "XLCTL", null);
}
}
//【屏幕参数】
//沿屏幕宽度每逻辑英寸的像素数
private float PixelsPerInchX
{
get
{
return GetDeviceCaps(GetWindowDC(HWND_DESKTOP), LOGPIXELSX);
}
}
//沿屏幕高度每逻辑英寸的像素数
private float PixelsPerInchY
{
get
{
return GetDeviceCaps(GetWindowDC(HWND_DESKTOP), LOGPIXELSY);
}
}
//X方向第磅像素值
public double PixelsPerPointX
{
get
{
return PixelsPerInchX / PoundsPerInch * (win.Zoom / 100);
}
}
//Y方向第磅像素值
public double PixelsPerPointY
{
get
{
return PixelsPerInchY / PoundsPerInch * (win.Zoom / 100);
}
}
//屏幕
/////////////////////////////////////////////////////////
/// 方法区
/////////////////////////////////////////////////////////
public static IntPtr GetHandle(IntPtr handle, string path)
{
IntPtr retHandel = handle;
string[] nodes = path.Split('/');
foreach (var item in nodes)
{
retHandel = FindWindowEx(retHandel, IntPtr.Zero, item, null);
if (retHandel == IntPtr.Zero) return retHandel;
}
return retHandel;
}
public static IntPtr GetHandle(IntPtr handle, string className, string title)
{
return FindWindowEx(handle, IntPtr.Zero, className, title);
}
public Rectangle GetWindowRect(IntPtr handle)
{
RECT winRect;
//GetClientRect(handle, out winRect);
GetWindowRect(handle, out winRect);
Rectangle rect = new Rectangle(
winRect.Left,
winRect.Top,
winRect.Right - winRect.Left,
winRect.Bottom - winRect.Top);
return rect;
}
public Rectangle GetClientRect(IntPtr handle)
{
RECT winRect; GetClientRect(handle, out winRect);
POINT winPoint; ClientToScreen(handle, out winPoint);
Rectangle rect = new Rectangle(winPoint.Left, winPoint.Top, winRect.Right, winRect.Bottom);
return rect;
}
//绘图区
private Pen pen;
public Pen Pen
{
get { return pen; }
set { pen = value; }
}
public int GetColor(IntPtr handle, int x, int y)
{
IntPtr dc = GetDC(handle);
int color = (int)GetPixel(dc, x, y);
return color;
}
public void DrawRectangles()
{
//////////////////////////////////////
/// 采用 .net 绘制
//////////////////////////////////////
//初始
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(HWND_WinWorkbook);
Rectangle rectExcel = GetClientRect(HWND_WinWorkbook); //主区域
//参考线
int left = 40;
int top = 40;
Pen pen = new Pen(Color.Red, 4);
g.DrawRectangles(pen,
new Rectangle[]
{
//大框
new Rectangle
(
0,
0,
rectExcel.Width - 1,
rectExcel.Height - 1
),
//参考线
new Rectangle
(
0 + left,
0 + top,
rectExcel.Width - left*2 - 1,
rectExcel.Height - top*2 - 1
),
//参考线
new Rectangle
(
0 + left * 2,
0 + top * 2,
rectExcel.Width - left*4 - 1,
rectExcel.Height - top*4 - 1
),
});
//End of DrawRectangles
}
//获得所有句柄
public static List<IntPtr> GetAllChildWindows(IntPtr handle)
{
List<IntPtr> hList = new List<IntPtr>();
hList.Add(handle);
//enum all child windows
EnumChildWindows
(
handle,
delegate(IntPtr hWnd, int lParam)
{
hList.Add(hWnd);
return true;
},
0
);
return hList;
}
//测试用
public void DrawTest()
{
//////////////////////////////////////
/// 采用 .net 绘制
//////////////////////////////////////
//初始
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(HWND_WinWorkbook);
Rectangle rectExcel = GetClientRect(HWND_WinWorkbook); //主区域
Pen pen = new Pen(Color.Red, 20);
g.DrawRectangles(pen,
new Rectangle[]
{
//大框
new Rectangle
(
60,
60,
rectExcel.Width - 60 * 2 - 1,
rectExcel.Height - 60 * 2 - 1
),
});
}
public int ColorTest()
{
return GetColor(HWND_WinWorkbook, 60+0, 60+0);
}
//public Graphics GS_Excel
//{
// get
// {
// IntPtr handle = GetDC(HWND_WinWorkbook);
// return Graphics.FromHdc(handle);
// }
//}
}
}