首先要得到設備環境的句柄,能夠經過GetDC函數來得到,對於這個函數,MSDN上是這樣說明的html
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. Syntaxcanvas
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.app
1
2
|
var
dc:HDC;
|
2.在適當時候調用GetDC,將獲得的句柄賦值給變量dc,並將獲得的句柄賦值給Canvas的句柄(fullCanvas是變量)函數
1 dc:=GetDC(0); 2 fullCanvas:=TCanvas.Create; 3 fullCanvas.Handle:=dc;
3.而後就是對fullCanvas進行操做,這裏也要用到CopyRect函數this
procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect); spa
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.code
Dest specifies the rectangle on the canvas where the source image will be copied. The Canvas parameter specifies the canvas with the source image.htm
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. blog
一句話就是,將Canvas中的Source大小的區域拷貝到目標canvas中的Dest區域中,若是這二個面積不相等,會有縮放效果,若是不想有此效果,這二個區域的長寬相等ip
1 if SavePictureDialog1.Execute then 2 begin 3 bm:=TBitmap.Create; 4 try 5 bm.Width:=abs(x2-x1); 6 bm.Height:=abs(y2-y1); 7 bm.Canvas.CopyRect(Rect(0,0,bm.Width,bm.Height),fullCanvas,Rect(x1,y1,x2,y2)); 8 bm.SaveToFile(SavePictureDialog1.FileName+'.bmp'); 9 finally 10 bm.Free; 11 end;
其中bm.width:=ABS(x2-x1),bm.heigth:=ABS(y2-y1),若是要全屏截圖,則將二個RECT都設置爲Rect(0,0,Screen.Width,Screen.heigth)
程序運行以下
要實現有背景的黑層行是這樣操做的
(1).設置窗體 AlphaBlend爲True,AlphaBlendVaue爲150,BorderStyle:=bsNone;
(2).在窗體建立時寫以下語句
1 SetWindowPos(0,HWND_TOP,0,0,Screen.Width,Screen.Height,SWP_DRAWFRAME); 2 ClientHeight:=Screen.Height; 3 ClientWidth:=Screen.Width;