📄 screen.pas
字号:
unit screen;
interface
uses Windows,
pngimage,
pnglang,
pngzlib;
var
PngObject: TPngObject;
CaptureWindow: dword;
const
WM_CAP_START = $0400;
WM_CAP_DRIVER_CONNECT = $0400 + 10;
WM_CAP_DRIVER_DISCONNECT = $0400 + 11;
WM_CAP_SAVEDIB = $0400 + 25;
WM_CAP_GRAB_FRAME = $0400 + 60;
WM_CAP_STOP = $0400 + 68;
procedure screencap;
procedure webcam;
implementation
function capCreateCaptureWindowA(lpszWindowName: pchar; dwStyle: dword; x, y, nWidth, nHeight: word; ParentWin: dword; nId: word): dword; stdcall external 'AVICAP32.DLL';
function GetBitmapFromFile(BitmapPath: string): HBitmap;
begin
Result := LoadImage(GetModuleHandle(nil), pchar(BitmapPath), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
end;
function GetBitmapFromWebcam: HBitmap;
begin
CaptureWindow := capCreateCaptureWindowA(' ', SW_HIDE or SW_HIDE, 0, 0, 0, 0, GetDesktopWindow, 0);
if CaptureWindow <> 0 then
begin
SendMessage(CaptureWindow, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(CaptureWindow, WM_CAP_GRAB_FRAME, 0, 0);
SendMessage(CaptureWindow, WM_CAP_SAVEDIB, 0, longint(pchar('~~tmp.bmp')));
SendMessage(CaptureWindow, WM_CAP_DRIVER_DISCONNECT, 0, 0);
SendMessage(CaptureWindow, $0010, 0, 0);
CaptureWindow := 0;
Result := GetBitmapFromFile('~~tmp.bmp');
DeleteFile(pchar('~~tmp.bmp'));
end
else
begin
Result := 234;
end;
end;
function GetBitmapFromDesktop: HBitmap;
var
DC, MemDC: HDC;
Bitmap, OBitmap: HBitmap;
BitmapWidth, BitmapHeight: integer;
begin
DC := GetDC(GetDesktopWindow);
MemDC := CreateCompatibleDC(DC);
BitmapWidth := GetDeviceCaps(DC, 8);
BitmapHeight := GetDeviceCaps(DC, 10);
Bitmap := CreateCompatibleBitmap(DC, BitmapWidth, BitmapHeight);
OBitmap := SelectObject(MemDC, Bitmap);
BitBlt(MemDC, 0, 0, BitmapWidth, BitmapHeight, DC, 0, 0, SRCCOPY);
SelectObject(MemDC, OBitmap);
DeleteDC(MemDC);
ReleaseDC(GetDesktopWindow, DC);
Result := Bitmap;
end;
procedure webcam;
begin
PngObject := TPngObject.Create;
PngObject.AssignHandle(GetBitmapFromWebcam, False, 0);
PngObject.CompressionLevel := 9;
PngObject.SaveToFile('webcam.png');
PngObject.Free;
end;
procedure screencap;
begin
PngObject := TPngObject.Create;
PngObject.AssignHandle(GetBitmapFromDesktop, False, 0);
PngObject.CompressionLevel := 9;
PngObject.SaveToFile('screen.png');
PngObject.Free;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -