Delphi 如何只捕获屏幕上鼠标点击的区域?

Delphi 如何只捕获屏幕上鼠标点击的区域?,delphi,winapi,screenshot,mouse-coordinates,Delphi,Winapi,Screenshot,Mouse Coordinates,下面的代码在每次单击鼠标左键时生成桌面的屏幕截图。 但我想做一个屏幕截图,只在该地区发生鼠标点击,例如,如果某个按钮被点击在某个网站上,屏幕截图必须只有这个按钮 GIF 这是可能的吗 如果是的话,如果有人展示代码示例,我会非常高兴!提前谢谢 program Project1; {$APPTYPE CONSOLE} {$R *.res} uses Windows, Messages, SysUtils, Graphics, Imaging.PngImage; ty

下面的代码在每次单击鼠标左键时生成桌面的屏幕截图。 但我想做一个屏幕截图,只在该地区发生鼠标点击,例如,如果某个按钮被点击在某个网站上,屏幕截图必须只有这个按钮

GIF

这是可能的吗

如果是的话,如果有人展示代码示例,我会非常高兴!提前谢谢

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  Windows,
  Messages,
  SysUtils,
  Graphics,
  Imaging.PngImage;
  
type
  MouseLLHookStruct = record
  end;
  
const
  WH_MOUSE_LL = 14;
  
var
  Msg: TMsg;
  mHook: Cardinal;

procedure GetCursor(ScreenShotBitmap: TBitmap);
var
  R: TRect;
  Icon: TIcon;
  II: TIconInfo;
  CI: TCursorInfo;
begin
  R := ScreenShotBitmap.Canvas.ClipRect;
  Icon := TIcon.Create;
  try
    CI.cbSize := SizeOf(CI);
    if GetCursorInfo(CI) then
      if CI.Flags = CURSOR_SHOWING then
      begin
        Icon.Handle := CopyIcon(CI.hCursor);
        if GetIconInfo(Icon.Handle, II) then
        begin
          ScreenShotBitmap.Canvas.Draw(CI.ptScreenPos.X - Integer(II.xHotspot) -
            R.Left, CI.ptScreenPos.Y - Integer(II.yHotspot) - R.Top, Icon);
        end;
      end;
  finally
    Icon.Free;
  end;
end;

procedure ScreenCapture;
var
  DC: HDC;
  Rect: TRect;
  png: TPngImage;
  Bitmap: TBitmap;
begin
  png := TPngImage.Create;
  Bitmap := TBitmap.Create;
  GetWindowRect(GetDesktopWindow, Rect);
  DC := GetWindowDC(GetDesktopWindow);
  try
    Bitmap.Width := Rect.Right - Rect.Left;
    Bitmap.Height := Rect.Bottom - Rect.Top;
    BitBlt(Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, DC, 0,
      0, SRCCOPY);
    GetCursor(Bitmap);
    png.Assign(Bitmap);
    png.SaveToFile('screenshot.png');
  finally
    ReleaseDC(GetDesktopWindow, DC);
    png.Free;
    Bitmap.Free;
  end;
end;

function LowLevelMouseHookProc(nCode: LongInt; WPARAM: WPARAM; lParam: lParam)
  : LRESULT; stdcall;
var
  info: ^MouseLLHookStruct absolute lParam;
begin
  Result := CallNextHookEx(mHook, nCode, WPARAM, lParam);
  if (WPARAM = WM_LBUTTONUP) then
    ScreenCapture;
end;

begin
  mHook := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseHookProc, HInstance, 0);
  
  while GetMessage(Msg, 0, 0, 0) do
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;
  UnhookWindowsHookEx(mHook);

end.

编辑:

我在VB.NET中找到了一个替代方案。但是用Delphi代码怎么可能是一个解决方案呢

Private Shared Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As Bitmap
        Dim bmp As Bitmap
        Dim hicon As IntPtr
        Dim ci As New CURSORINFO()
        Dim icInfo As ICONINFO
        ci.cbSize = Marshal.SizeOf(ci)
        If GetCursorInfo(ci) Then
            hicon = CopyIcon(ci.hCursor)
            If GetIconInfo(hicon, icInfo) Then
                x = ci.ptScreenPos.X - CInt(icInfo.xHotspot)
                y = ci.ptScreenPos.Y - CInt(icInfo.yHotspot)
                Dim ic As Icon = Icon.FromHandle(hicon)
                bmp = ic.ToBitmap()
                ic.Dispose()
                Return bmp
            End If
        End If
        Return Nothing
    End Function

'Insert on Timer tick event
    Private Sub Screenshot()
        Dim x As Integer
        Dim y As Integer

        Dim cursorBmp As Bitmap = CaptureCursor(x, y)

        Dim bmp As New Bitmap(Cursor.Size.Width, Cursor.Size.Height)
        Dim sourceLocation As Point = Control.MousePosition

        sourceLocation.Offset(-16, -16)

        Using g As Graphics = Graphics.FromImage(bmp)
            g.CopyFromScreen(sourceLocation, Point.Empty, bmp.Size)
            g.DrawImage(cursorBmp, x - sourceLocation.X, y - sourceLocation.Y)
            cursorBmp.Dispose()
        End Using

        Me.PictureBox1.Image = bmp
    End Sub

有一个简单的方法,你可以参考下面的代码

此代码可以获取包含鼠标位置的屏幕截图,只需使用
BitBlt
指定鼠标坐标和矩形大小(按钮大小),最终获得所需的BMP图像。使用
DrawImage
将BMP图像绘制到矩形框中,如GIF所示

通过调用
GetCursorInfo
可以获得鼠标坐标,并且可以根据需要指定矩形的大小

请注意,在获得鼠标坐标后,将它们传递到
BitBlt
中时,需要分别减去左侧和上方半个矩形的大小

比如说,

BitBlt(Newhdc, 
           0, 
           0, 
           rect_x,  //size of the rect 
           rect_y, 
           HDC, 
           x - half_rect_x,  //x,y => mouse coordinates
           y - half_rect_y,  //half_rect_x, half_rect_y  => the size of half the rectangle
           SRCCOPY);

。您需要GIF显示的效果吗?
屏幕截图必须仅显示此按钮。
如果您只需单击按钮的一角,例如按钮的右下角。如何显示屏幕截图?我不知道如何在网页上获取按钮实例,但我认为这与CSS有关。如果您需要单击按钮的某个部分来显示按钮的整个屏幕截图,我认为这不是一件简单的事情。至少您需要熟悉网页上控件的布局和属性。@STRIVESSUN MSFT,
您需要GIF显示的效果吗?
-是。
BitBlt(Newhdc, 
           0, 
           0, 
           rect_x,  //size of the rect 
           rect_y, 
           HDC, 
           x - half_rect_x,  //x,y => mouse coordinates
           y - half_rect_y,  //half_rect_x, half_rect_y  => the size of half the rectangle
           SRCCOPY);