My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
// website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Text;

namespace EbookCopier
{
/// <summary>
/// 屏幕剪辑区域的边框样式。
/// </summary>
public enum FocusStyle
{
/// <summary>
/// 正常模式
/// </summary>
Normal,
/// <summary>
/// 持续拷贝过程中。
/// </summary>
ContCapture,
/// <summary>
/// 拷贝前,拷屏区域必须为空白。
/// </summary>
BeforeCopy,
/// <summary>
/// 已经拷贝完毕,以特殊的样式提醒用户已经拷贝完成。
/// </summary>
AfterCopy,
}

/// <summary>
/// 标识屏幕剪辑区域的窗体。可以类比照相机的取景框。
/// </summary>
internal class FocusRectForm : AlphaForm
{
StringFormat centerFormat = null;
/// <summary>
/// 初始化类 <see cref="FocusRectForm"/> 的新实例。
/// </summary>
/// <param name="screen">指定的屏幕。</param>
public FocusRectForm(Screen screen)
: base()
{
this.ClickThroughEnable = true;

centerFormat = new StringFormat(StringFormatFlags.NoWrap);
centerFormat.Alignment = StringAlignment.Center;
centerFormat.LineAlignment = StringAlignment.Center;

this.SetScreen(screen);
}


public int pageIndex = 0;
/// <summary>
/// 获取或设置 当前捕捉的页码。
/// </summary>
/// <value>The index of the page.</value>
public int PageIndex
{
get { return pageIndex; }
set
{
if (pageIndex == value)
return;
pageIndex = value;

UpdateLens();
}
}



Point nextPageMouseLocation = Point.Empty;
/// <summary>
/// 获取或设置 “下一页”按钮的鼠标点击位置。
/// </summary>
/// <value>鼠标点击位置</value>
public Point NextPageMouseLocation
{
get { return nextPageMouseLocation; }
set
{
if (nextPageMouseLocation == value)
return;
nextPageMouseLocation = value;

UpdateLens();
}
}
Point prevPageMouseLocation = Point.Empty;
/// <summary>
/// 获取或设置 “上一页”按钮的鼠标点击位置。
/// </summary>
/// <value>鼠标点击位置</value>
public Point PrevPageMouseLocation
{
get { return prevPageMouseLocation; }
set
{
if (prevPageMouseLocation == value)
return;
prevPageMouseLocation = value;

UpdateLens();
}
}


Rectangle selectedRect = Rectangle.Empty;
/// <summary>
/// 获取或设置 屏幕剪辑区域。
/// </summary>
public Rectangle SelectedRect
{
get { return selectedRect; }
set
{
if (selectedRect == value)
return;
selectedRect = value;

UpdateLens();
}
}

FocusStyle focusStyle = FocusStyle.Normal;
/// <summary>
/// 获取或设置 屏幕剪辑区域边框样式。
/// </summary>
/// <value>样式。</value>
public FocusStyle FocusStyle
{
get { return focusStyle; }
set
{
if (focusStyle == value)
return;
focusStyle = value;

UpdateLens();
}
}
Bitmap bmp = new Bitmap(1024, 768);
/// <summary>
/// 设置要显示剪辑区域边框的屏幕。
/// </summary>
/// <param name="screen">The screen.</param>
public void SetScreen(Screen screen)
{
Rectangle bounds = screen.Bounds;
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
bmp = new Bitmap(bounds.Width, bounds.Height);
this.Bounds = screen.Bounds;

UpdateLens();
}



/// <summary>
/// 更新界面。
/// </summary>
void UpdateLens()
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);

//绘制鼠标点击位置
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.SystemDefault;
if (prevPageMouseLocation != Point.Empty)
{
g.DrawPath(Pens.Red, Common.GetFocusGraphicsPath(prevPageMouseLocation, 12, 8, 3));
g.DrawString("上一页", new Font("宋体", 9), Brushes.Red,
new Point(prevPageMouseLocation.X + 12, prevPageMouseLocation.Y - 5));
}
if (nextPageMouseLocation != Point.Empty)
{
g.DrawPath(Pens.Red, Common.GetFocusGraphicsPath(nextPageMouseLocation, 12, 8, 3));
g.DrawString("下一页", new Font("宋体", 9), Brushes.Red,
new Point(nextPageMouseLocation.X + 12, nextPageMouseLocation.Y - 5));
}

g.SmoothingMode = SmoothingMode.Default;

if (selectedRect.Width > 0 && selectedRect.Height > 0)
{
OnPaintFocusRect(g, selectedRect, focusStyle);
}
}

this.SetBitmap(bmp);
this.TopMost = true;
}

Color color = Color.Green;
/// <summary>
/// 剪辑区域边框的颜色。
/// </summary>
public Color FocusBorderColor
{
get { return color; }
set
{
if(color == value)
return;
color = value;

UpdateLens();
}
}
/// <summary>
/// 绘制捕捉框。
/// </summary>
/// <param name="g">Graphics 实例。</param>
/// <param name="rect">要标识的区域</param>
/// <param name="style">边框样式。</param>
void OnPaintFocusRect(Graphics g, Rectangle rect, FocusStyle style)
{
//四个角的位置
Point[] points = new Point[]{
new Point(rect.Left,rect.Top),
new Point(rect.Left,rect.Bottom),
new Point(rect.Right,rect.Top),
new Point(rect.Right,rect.Bottom),
};

//半透明的宽线
Pen penBorder = new Pen(Color.FromArgb(100, color), 4);
//窄虚线
Pen penFocus = new Pen(color, 1);
penFocus.DashStyle = DashStyle.Dash;
//g.DrawRectangle(pen, new Rectangle(rect.X - 10, rect.Y - 10, rect.Width + 20, rect.Height + 20));

switch (style)
{
case FocusStyle.Normal:
{
g.SmoothingMode = SmoothingMode.Default;
if (rect.Width > 50 && rect.Height > 50)
{
g.DrawLine(penFocus, rect.Left + 20, rect.Top, rect.Right - 20, rect.Top);//上边线
g.DrawLine(penFocus, rect.Left + 20, rect.Bottom, rect.Right - 20, rect.Bottom);//下边线
g.DrawLine(penFocus, rect.Left, rect.Top + 20, rect.Left, rect.Bottom - 20);
g.DrawLine(penFocus, rect.Right, rect.Top + 20, rect.Right, rect.Bottom - 20);

for (int i = 0; i < points.Length; i++)
g.DrawPath(new Pen(color, 1), Common.GetFocusGraphicsPath(points[i], 12, 8, 3));
}
else
{//框太小了,只画个矩形
g.DrawRectangle(penFocus, rect);
}

g.SmoothingMode = SmoothingMode.AntiAlias;
if (rect.Width >= 100 && rect.Height >= 100)
{
//绘制定位点
GraphicsPath path = Common.GetFocusGraphicsPath(
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2),
30, 30, 10);
g.DrawPath(penBorder, path);
}

if (rect.Width > 250 && rect.Height > 100)
{//宽度 > 200,则显示字体
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString("剪辑 区域", new Font("黑体", 22.0F, FontStyle.Bold), new SolidBrush(Color.FromArgb(120, color)), rect, centerFormat);
}

}
break;
case FocusStyle.ContCapture:
{
g.SmoothingMode = SmoothingMode.Default;
g.DrawRectangle(new Pen(Color.FromArgb(120, color), 1), rect);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(string.Format("第 {0} 页", pageIndex), new Font("黑体", 22.0F, FontStyle.Bold), new SolidBrush(Color.FromArgb(120, color)), rect, centerFormat);
}
break;
case FocusStyle.BeforeCopy:
{
g.SmoothingMode = SmoothingMode.Default;
g.DrawRectangle(penFocus, rect);
}
break;
case FocusStyle.AfterCopy:
{
g.SmoothingMode = SmoothingMode.Default;
g.DrawRectangle(new Pen(Color.FromArgb(120, color), 1), rect);
g.FillRectangle(new SolidBrush(Color.FromArgb(60, color)), rect);

//g.SmoothingMode = SmoothingMode.AntiAlias;
//g.TextRenderingHint = TextRenderingHint.AntiAlias;
//g.DrawString(string.Format("第 {0} 页",pageIndex), new Font("黑体", 22.0F, FontStyle.Bold), new SolidBrush(Color.FromArgb(120, color)), rect, centerFormat);
}
break;
}
}
/// <summary>
/// 获取屏幕上指定区域的截图。
/// </summary>
/// <returns>截图</returns>
public Bitmap GetSelectedScreen()
{
Bitmap scr = null;
if (selectedRect.Width > 0 && selectedRect.Height > 0)
{
scr = new Bitmap(selectedRect.Width, selectedRect.Height);
using (Graphics g = Graphics.FromImage(scr))
{
g.CopyFromScreen(selectedRect.Location, new Point(0, 0), selectedRect.Size);
}
}

return scr;
}
}
}

Change log

r2 by chuangen on Mar 11, 2009   Diff
ebookcopier v1.0.3357.8997

为准备将项目开源,我做了些修改,主要包括:
1. 代码书写规范;
2. 补充必要的注释;
3. 审查版权问题,对引用的别人代码做了版权说明。
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 12793 bytes, 351 lines
Powered by Google Project Hosting