Delphi to achieve screenshot

We must first get a handle to a device context, can be obtained by GetDC function, for this function, it is described on MSDN

The GetDC function retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC. The device context is an opaque data structure, whose values are used internally by GDI.
The GetDCEx function is an extension to GetDC, which gives an application more control over how and whether clipping occurs in the client area.
Syntax

HDC GetDC(
__in HWND hWnd
);
Parameters
hWnd [in]
A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen.
Return value
If the function succeeds, the return value is a handle to the DC for the specified window's client area.
If the function fails, the return value is NULL.

1. Affirms handle a full variable storage GetDC obtained by the method, HDC type
?
1
2
var
DC: HDC;
2. assignment in due course handle calls GetDC, will get to a variable dc, and assigned to the Canvas to get a handle handle (fullCanvas is variable)

DC. 1: = the GetDC (0);
2 FullCanvas: = TCanvas.Create;
. 3 fullCanvas.Handle: = DC;
3. FullCanvas then is to operate, but also used here function CopyRect

procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect);

Copies part of an image from another canvas into the canvas.Use CopyRect to transfer part of the image on another canvas to the image of the TCanvas object.

Dest specifies the rectangle on the canvas where the source image will be copied. The Canvas parameter specifies the canvas with the source image.

Source specifies a rectangle bounding the portion of the source canvas that will be copied.The portion of the source canvas is copied using the mode specified by CopyMode.

Word is, the size of the Source of the Canvas area Dest copied to the target area of ​​the canvas, if these two areas are not equal, there will be a zoom effect, if you do not have this effect, equal to the length and width of these two regions

Copy the code
. 1 IF SavePictureDialog1.Execute the then
2 the begin
. 3 BM: = TBitmap.Create;
. 4 the try
. 5 bm.Width: = ABS (X1-X2);
. 6 bm.Height: = ABS (Y1-Y2);
. 7 bm.Canvas .CopyRect (Rect (0,0, bm.Width, bm.Height), FullCanvas, Rect (X1, Y1, X2, Y2));
. 8 bm.SaveToFile (SavePictureDialog1.FileName + 'BMP.');
. 9 the finally
10 BM .free;
. 11 End;
duplicated code
wherein bm.width: = ABS (x2-x1 ), bm.heigth: = ABS (y2-y1), if required for full screen theme, then the two are set RECT Rect (0, 0, screen.Width, Screen.heigth)

The program runs as follows


To achieve a black background layer is operated row

(1) Form AlphaBlend set to True, AlphaBlendVaue to 150, BorderStyle: = bsNone;

(2) Write the following statement when the form is created

1 SetWindowPos(0,HWND_TOP,0,0,Screen.Width,Screen.Height,SWP_DRAWFRAME);
2 ClientHeight:=Screen.Height;
3 ClientWidth:=Screen.Width;

Guess you like

Origin www.cnblogs.com/blogpro/p/11446107.html