Delphi截图并加上鼠标
Delphi截图并加上鼠标
操作方法
- 01
procedure TForm1.Button1Click(Sender: TObject);var Bitmap: TBitmap; Canvas: TCanvas; DC: HDC; Cursor: TIcon; CurPoint: TPoint;begin Bitmap := TBitmap.Create; Bitmap.Width := Screen.Width; Bitmap.Height := Screen.Height; Canvas := TCanvas.Create; DC := GetDC(0); Canvas.Handle := DC; Cursor := TIcon.Create; Bitmap.Canvas.CopyRect(Bitmap.Canvas.ClipRect, Canvas, Bitmap.Canvas.ClipRect); GetCursorPos(CurPoint); Cursor.Handle := GetCursor; Bitmap.Canvas.Draw(CurPoint.X, CurPoint.Y, Cursor); Image1.Picture.Bitmap.Assign(Bitmap); Cursor.Free; Bitmap.Free; Canvas.Free; ReleaseDC(0, DC);end;
- 02
===================代码二========================procedure My_GetScreenToBmp(DrawCur:Boolean;StreamName:TMemoryStream);varMybmp:Tbitmap;Cursorx, Cursory: integer;dc: hdc;Mycan: Tcanvas;R: TRect;DrawPos: TPoint;MyCursor: TIcon;hld: hwnd;Threadld: dword;mp: tpoint;pIconInfo: TIconInfo; begin Mybmp := Tbitmap.Create; {建立BMPMAP } Mycan := TCanvas.Create; {屏幕截取} dc := GetWindowDC(0);try Mycan.Handle := dc; R := Rect(0, 0, Screen.Width,Screen.Height{GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN)}); Mybmp.Width := R.Right; Mybmp.Height := R.Bottom; Mybmp.Canvas.CopyRect(R, Mycan, R);finally releaseDC(0, DC);end; Mycan.Handle := 0;Mycan.Free; if DrawCur then {画上鼠标图象}begin GetCursorPos(DrawPos); MyCursor := TIcon.Create; getcursorpos(mp); hld := WindowFromPoint(mp); Threadld := GetWindowThreadProcessId(hld, nil); AttachThreadInput(GetCurrentThreadId, Threadld, True); MyCursor.Handle := Getcursor(); AttachThreadInput(GetCurrentThreadId, threadld, False); GetIconInfo(Mycursor.Handle, pIconInfo); cursorx := DrawPos.x - round(pIconInfo.xHotspot); cursory := DrawPos.y - round(pIconInfo.yHotspot); Mybmp.Canvas.Draw(cursorx, cursory, MyCursor); {画上鼠标} DeleteObject(pIconInfo.hbmColor);{GetIconInfo 使用时创建了两个bitmap对象. 需要手工释放这两个对象} DeleteObject(pIconInfo.hbmMask);{否则,调用他后,他会创建一个bitmap,多次调用会产生多个,直至资源耗尽} Mycursor.ReleaseHandle; {释放数组内存} MyCursor.Free; {释放鼠标指针}end; Mybmp.PixelFormat:=pf8bit; //256色//Mybmp.SaveToFile(Filename); Mybmp.SaveToStream(StreamName);Mybmp.Free; end;