程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 用C# 實現截圖功能(1)(類似QQ截圖)

用C# 實現截圖功能(1)(類似QQ截圖)

編輯:關於C#

概述:

在公司,不能自己安裝軟件,也不能下載,但有時候需要截圖。用PrintScreen鍵只能截取全屏,感覺很麻煩。於是決定自己編寫一個截圖工具。

眾所周知,QQ截圖首先將截取全屏為一個圖片,然後用在這個圖片基礎上截取需要的部分。本程序實現方法類似。

程序運行截圖如下:

圖中心矩形為即將截取區域

程序很粗糙,希望大家提出寶貴意見。

1,自定義矩形類MyRectangle

在QQ截圖程序中,用戶用鼠標繪制出的截圖區域是可調整大小和位置的,在4個邊的中點和4個頂點各有一個小矩形標記。(如圖所示)

.NET Framework中本身沒有這樣的矩形,因此要自定義實現。

考慮到類的專用性,不必實現.Net Framework2.0中Rectangle的全部功能。

該MyRectangle類圖如下:

MyRectangle需包含如下屬性

int X記錄矩形左上角x坐標

int Y記錄矩形左上角y坐標

int DownPointX繪制矩形時鼠標落點x坐標

int DownPointY繪制矩形時鼠標落點y坐標

int Width矩形寬

int Height矩形高

int MinWidth 矩形最小寬度

int MinHeight 矩形最小高度

bool ChangeSizeMode 標識矩形當前繪制模式是否為“改變大小”

bool MoveMode 標識矩形當前繪制模式是否為“移動”

bool MouseOnLeftTop 標識鼠標當前位置是否在矩形左上角

bool MouseOnLeftMiddle 標識鼠標當前位置是否在矩形左邊中點

bool MouseOnLeftBottom 標識鼠標當前位置是否在矩形左下角

bool MouseOnRightTop 標識鼠標當前位置是否在矩形右上角

bool MouseOnRightMiddle 標識鼠標當前位置是否在矩形右邊中點

bool MouseOnRightBottom 標識鼠標當前位置是否在矩形右下角

bool MouseOnTopMiddle 標識鼠標當前位置是否在矩形頂邊中點

bool MouseOnBottomMiddle 標識鼠標當前位置是否在矩形底邊中點

bool MouseOnMiddle 標識鼠標當前位置是否在矩形中心

int LittleRectangleWidth 矩形周邊8個小矩形的寬度

int LittleRectangleHeight 矩形周邊8個小矩形的高度

Rectangle LeftTopRectangle 矩形左上角小矩形

Rectangle LeftMiddleRectangle 矩形左邊中點小矩形

Rectangle LeftBottomRectangle 矩形左下角小矩形

Rectangle RightTopRectangle 矩形右上角小矩形

Rectangle RightMiddleRectangle 矩形右邊中點小矩形

Rectangle RightBottomRectangle 矩形右下角小矩形

Rectangle TopMiddleRectangle 矩形頂邊中點小矩形

Rectangle BottomMiddleRectangle 矩形底邊中點小矩形

Rectangle Rect 主體矩形

Size Size 矩形大小

Image BackImage 背景圖片

Cursor MyCursor 光標樣式

矩形本身包含監測當前繪制模式和繪制方法,主要方法成員如下:

SetLittleRectangle() 設置8個小矩形

Draw(Color backColor) 繪制方法,+1重載

ChangeSize(MouseEventArgs e) 改變矩形大小

Move(int newX, int newY) 改變矩形位置

CheckMouseLocation(MouseEventArgs e) 判斷鼠標當前落點

setAllModeFalse() 將所有模式設定為false

public bool onChangingMode() 判斷當前繪制模式是否為“改變大小”或“移動”

Initialize(int x, int y, int width, int height) 根據給定參數初始化矩形

MyRectagle類代碼實現如下:

class MyRectangle
2  {
3    /**//// <summary>
4    /// The x-coordinate of the upper-left corner of the main rectangle
5    /// </summary>
6    private int x;
7
8    /**//// <summary>
9    /// The star x-coordinate when you draw the main rectangle
10    /// </summary>
11    private int downPointX;
12
13    /**//// <summary>
14    /// The y-coordinate of the upper-left corner of the main rectangle
15    /// </summary>
16    private int y;
17
18    /**//// <summary>
19    /// The star y-coordinate when you draw the main rectangle
20    /// </summary>
21    private int downPointY;
22
23    /**//// <summary>
24    /// The width of the main rectangle
25    /// </summary>
26    private int width;
27
28    /**//// <summary>
29    /// The height of the main rectangle
30    /// </summary>
31    private int height;
32
33    /**//// <summary>
34    /// The least width of the main rectangle
35    /// </summary>
36    private int minWidth;
37
38    /**//// <summary>
39    /// The least height of the main rectangle
40    /// </summary>
41    private int minHeight;
42
43    /**//// <summary>
44    /// Sign the main rectangle is on change size mode or not
45    /// </summary>
46    private bool changeSizeMode;
47
48    /**//// <summary>
49    /// Sign the main rectangle is on move mode or not
50    /// </summary>
51    private bool moveMode;
52
53    /**//// <summary>
54    /// Sign the current mouse position is on the upper-left corner of the main rectangle or not
55    /// </summary>
56    private bool mouseOnLeftTop;
57    /**//// <summary>
58    /// Sign the current mouse position is on the middle point of the left line of the main rectangle or not
59    /// </summary>
60    private bool mouseOnLeftMiddle;
61    /**//// <summary>
62    /// Sign the current mouse position is on the bottom-left corner of the main rectangle or not
63    /// </summary>
64    private bool mouseOnLeftBottom;
65
66    /**//// <summary>
67    /// Sign the current mouse position is on the upper-right corner of the mian rectangle or not
68    /// </summary>
69    private bool mouseOnRightTop;
70    /**//// <summary>
71    /// 鼠標落點在右中點標志
72    /// </summary>
73    private bool mouseOnRightMiddle;
74    /**//// <summary>
75    /// Sign the current mouse position is on the middle point of the right line of the main rectangle or not
76    /// </summary>
77    private bool mouseOnRightBottom;
78
79    /**//// <summary>
80    /// Sign the current mouse position is on the middle point of the top line of the main rectangle or not
81    /// </summary>
82    private bool mouseOnTopMiddle;
83    /**//// <summary>
84    /// Sign the current mouse position is on the middle point of the bottom line of the main rectangle or not
85    /// </summary>
86    private bool mouseOnBottomMiddle;
87
88    /**//// <summary>
89    /// Sign the current mouse position is in the main rectangle or not
90    /// </summary>
91    private bool mouseOnMiddle;
92    /**//// <summary>
93    /// The width of the 8 little rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle
94    /// </summary>
95    private int littleRectangleWidth;
96    /**//// <summary>
97    /// The height of the 8 little rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle
98    /// </summary>
99    private int littleRectangleHeight;
100
101    /**//// <summary>
102    /// The little rectangle on the upper-left corner of the main rectangle
103    /// </summary>
104    private Rectangle leftTopRectangle;
105    /**//// <summary>
106    /// The little rectangle on the middle point of the left line of the main rectangle
107    /// </summary>
108    private Rectangle leftMiddleRectangle;
109    /**//// <summary>
110    /// The little rectangle on the bottom-left corner of the main rectangle
111    /// </summary>
112    private Rectangle leftBottomRectangle;
113
114    /**//// <summary>
115    /// The little rectangle on the upper-right corner of the main rectangle
116    /// </summary>
117    private Rectangle rightTopRectangle;
118    /**//// <summary>
119    /// The little rectangle on the middle point of the right line of the main rectangle
120    /// </summary>
121    private Rectangle rightMiddleRectangle;
122    /**//// <summary>
123    /// The little rectangle on the bottom-right corner of the main rectangle
124    /// </summary>
125    private Rectangle rightBottomRectangle;
126
127    /**//// <summary>
128    /// The little rectangle on the middle point of the top line of the main rectangle
129    /// </summary>
130    private Rectangle topMiddleRectangle;
131    /**//// <summary>
132    /// The little rectangle on the middle point of the bottom line of the main rectangle
133    /// </summary>
134    private Rectangle bottomMiddleRectangle;
135
136    /**//// <summary>
137    /// The main rectangle
138    /// </summary>
139    private Rectangle rect;
140
141    /**//// <summary>
142    /// The size of the main rectangle
143    /// </summary>
144    private Size size;
145
146    /**//// <summary>
147    /// The background image of the screen
148    /// </summary>
149    private Image backImage;
150
151    /**//// <summary>
152    /// The cursor manner
153    /// </summary>
154    private Cursor myCursor;
155
156    /**//// <summary>
157    /// Gets of sets the x-coordinate of the upper-left corner of the main rectangle
158    /// </summary>
159    public int X
160    {
161      get { return x; }
162      set
163      {
164        x = value;
165        rect.X = value;
166      }
167    }
168    /**//// <summary>
169    /// Gets of sets the y-coordinate of the upper-left corner of the main rectangle
170    /// </summary>
171    public int Y
172    {
173      get { return y; }
174      set
175      {
176        y = value;
177        rect.Y = value;
178      }
179    }
180
181    /**//// <summary>
182    /// Gets of sets the star x-coordinate when you draw the main rectangle
183    /// </summary>
184    public int DownPointX
185    {
186      get { return downPointX; }
187      set { downPointX = value; }
188    }
189    /**//// <summary>
190    /// Gets of sets the star y-coordinate when you draw the main rectangle
191    /// </summary>
192    public int DownPointY
193    {
194      get { return downPointY; }
195      set { downPointY = value; }
196    }
197    /**//// <summary>
198    /// Gets of sets the width of the main rectangle
199    /// </summary>
200    public int Width
201    {
202      get { return width; }
203      set
204      {
205        width = value;
206        rect.Width = value;
207      }
208    }
209    /**//// <summary>
210    /// Gets or sets the height of the main rectangle
211    /// </summary>
212    public int Height
213    {
214      get { return height; }
215      set
216      {
217        height = value;
218        rect.Height = value;
219      }
220    }
221
222    /**//// <summary>
223    /// Gets or sets the least width of the main rectangle
224    /// </summary>
225    public int MinWidth
226    {
227      get { return minWidth; }
228      set { minWidth = value; }
229    }
230
231    /**//// <summary>
232    /// Gets or sets the least height of the main rectangle
233    /// </summary>
234    public int MinHeight
235    {
236      get { return minHeight; }
237      set { minHeight = value; }
238    }
239
240    /**//// <summary>
241    /// Gets or sets the sign of the change size mode of the main rectangle
242    /// </summary>
243    public bool ChangeSizeMode
244    {
245      get { return changeSizeMode; }
246      set
247      {
248        changeSizeMode = value;
249        moveMode = !value;
250      }
251    }
252
253    /**//// <summary>
254    /// Gets or sets the sign of the move mode of the main rectangle
255    /// </summary>
256    public bool MoveMode
257    {
258      get { return moveMode; }
259      set
260      {
261        moveMode = value;
262        changeSizeMode = !value;
263      }
264    }
265
266    /**//// <summary>
267    /// Gets or sets the sign of current mouse position
268    /// (is on the upper-left corner of the main rectangle or not)
269    /// </summary>
270    public bool MouseOnLeftTop
271    {
272      get { return mouseOnLeftTop; }
273      set
274      {
275        mouseOnLeftTop = value;
276        if (value)
277        {
278          mouseOnLeftMiddle = false;
279          mouseOnLeftBottom = false;
280
281          mouseOnRightTop = false;
282          mouseOnRightMiddle = false;
283          mouseOnRightBottom = false;
284
285          mouseOnTopMiddle = false;
286          mouseOnBottomMiddle = false;
287
288          mouseOnMiddle = false;
289        }
290      }
291    }
292    /**//// <summary>
293    /// Gets or sets the sign of current mouse position
294    /// (is on the middle point of the left line of the main rectangle or not)
295    /// </summary>
296    public bool MouseOnLeftMiddle
297    {
298      get { return mouseOnLeftMiddle; }
299      set
300      {
301        mouseOnLeftMiddle = value;
302        if (value)
303        {
304          mouseOnLeftTop = false;
305          mouseOnLeftBottom = false;
306
307          mouseOnRightTop = false;
308          mouseOnRightMiddle = false;
309          mouseOnRightBottom = false;
310
311          mouseOnTopMiddle = false;
312          mouseOnBottomMiddle = false;
313
314          mouseOnMiddle = false;
315        }
316      }
317    }
318    /**//// <summary>
319    /// Gets or sets the sign of current mouse position
320    /// (is on the bottom-left corner of the main rectangle or not)
321    /// </summary>
322    public bool MouseOnLeftBottom
323    {
324      get { return mouseOnLeftBottom; }
325      set
326      {
327        mouseOnLeftBottom = value;
328        if (value)
329        {
330          mouseOnLeftTop = false;
331          mouseOnLeftMiddle = false;
332
333          mouseOnRightTop = false;
334          mouseOnRightMiddle = false;
335          mouseOnRightBottom = false;
336
337          mouseOnTopMiddle = false;
338          mouseOnBottomMiddle = false;
339
340          mouseOnMiddle = false;
341        }
342      }
343    }
344
345    /**//// <summary>
346    /// Gets or sets the sign of current mouse position
347    /// (is on the upper-right corner of the main rectangle or not)
348    /// </summary>
349    public bool MouseOnRightTop
350    {
351      get { return mouseOnRightTop; }
352      set
353      {
354        mouseOnRightTop = value;
355        if (value)
356        {
357          mouseOnLeftTop = false;
358          MouseOnLeftMiddle = false;
359          mouseOnLeftBottom = false;
360
361          mouseOnRightMiddle = false;
362          mouseOnRightBottom = false;
363
364          mouseOnTopMiddle = false;
365          mouseOnBottomMiddle = false;
366
367          mouseOnMiddle = false;
368        }
369      }
370    }
371    /**//// <summary>
372    /// Gets or sets the sign of current mouse position
373    /// (is on the middle point of the right line of the main rectangle or not)
374    /// </summary>
375    public bool MouseOnRightMiddle
376    {
377      get { return mouseOnRightMiddle; }
378      set
379      {
380        mouseOnRightMiddle = value;
381        if (value)
382        {
383          mouseOnLeftTop = false;
384          mouseOnLeftBottom = false;
385          mouseOnLeftMiddle = false;
386
387          mouseOnRightTop = false;
388          mouseOnRightBottom = false;
389
390          mouseOnTopMiddle = false;
391          mouseOnBottomMiddle = false;
392
393          mouseOnMiddle = false;
394        }
395      }
396    }
397    /**//// <summary>
398    /// Gets or sets the sign of current mouse position
399    /// (is on the bottom-right corner of the main rectangle or not)
400    /// </summary>
401    public bool MouseOnRightBottom
402    {
403      get { return mouseOnRightBottom; }
404      set
405      {
406        mouseOnRightBottom = value;
407        if (value)
408        {
409          mouseOnLeftTop = false;
410          mouseOnLeftBottom = false;
411          mouseOnLeftMiddle = false;
412
413          mouseOnRightTop = false;
414          mouseOnRightMiddle = false;
415
416          mouseOnTopMiddle = false;
417          mouseOnBottomMiddle = false;
418
419          mouseOnMiddle = false;
420        }
421      }
422    }
423
424    /**//// <summary>
425    /// Gets or sets the sign of current mouse position
426    /// (is on the middle point of the top line of the main rectangle or not)
427    /// </summary>
428    public bool MouseOnTopMiddle
429    {
430      get { return mouseOnTopMiddle; }
431      set
432      {
433        mouseOnTopMiddle = value;
434        if (value)
435        {
436          mouseOnLeftTop = false;
437          mouseOnLeftBottom = false;
438          mouseOnLeftMiddle = false;
439
440          mouseOnRightTop = false;
441          mouseOnRightBottom = false;
442          mouseOnRightMiddle = false;
443
444          mouseOnBottomMiddle = false;
445
446          mouseOnMiddle = false;
447        }
448      }
449    }
450    /**//// <summary>
451    /// Gets or sets the sign of current mouse position
452    /// (is on the middle point of the middle line of the main rectangle or not)
453    /// </summary>
454    public bool MouseOnBottomMiddle
455    {
456      get { return mouseOnBottomMiddle; }
457      set
458      {
459        mouseOnBottomMiddle = value;
460        if (value)
461        {
462          mouseOnLeftTop = false;
463          mouseOnLeftBottom = false;
464          mouseOnLeftMiddle = false;
465
466          mouseOnRightTop = false;
467          mouseOnRightBottom = false;
468          mouseOnRightMiddle = false;
469
470          mouseOnTopMiddle = false;
471
472          mouseOnMiddle = false;
473        }
474      }
475    }
476
477    /**//// <summary>
478    /// Gets or sets the sign of current mouse position
479    /// (is in the main rectangle or not)
480    /// </summary>
481    public bool MouseOnMiddle
482    {
483      get { return mouseOnMiddle; }
484      set
485      {
486        mouseOnMiddle = value;
487        if (value)
488        {
489          mouseOnLeftTop = false;
490          mouseOnLeftBottom = false;
491          mouseOnLeftMiddle = false;
492
493          mouseOnRightTop = false;
494          mouseOnRightBottom = false;
495          mouseOnRightMiddle = false;
496
497          mouseOnTopMiddle = false;
498          MouseOnBottomMiddle = false;
499        }
500      }
501    }
502    /**//// <summary>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved