3
我有一個程序,它需要一個監視器的屏幕截圖,並且可以在快照中包含鼠標光標。原始功能僅適用於一臺顯示器。當繪製鼠標光標時,它目前僅在主監視器上正確顯示。但是,我無法弄清楚如何將它放置在任何其他顯示器上。請參閱此過程結束時的註釋。多監視器屏幕截圖 - 定位鼠標光標
procedure ScreenShot(var Bitmap: TBitmap; const MonitorNum: Integer;
const DrawCursor: Boolean; const Quality: TPixelFormat);
var
DC: HDC;
C: TCanvas;
R: TRect;
CursorInfo: TCursorInfo;
Icon: TIcon;
IconInfo: TIconInfo;
M: TMonitor;
CP: TPoint;
begin
M:= Screen.Monitors[MonitorNum];
DC:= GetDC(GetDesktopWindow);
try
C:= TCanvas.Create;
try
C.Handle:= DC;
R:= M.BoundsRect;
Bitmap.Width:= R.Width;
Bitmap.Height:= R.Height;
Bitmap.PixelFormat:= Quality;
Bitmap.Canvas.CopyRect(Rect(0,0,R.Width,R.Height), C, R);
finally
C.Free;
end;
finally
ReleaseDC(GetDesktopWindow, DC);
end;
if DrawCursor then begin
R:= Bitmap.Canvas.ClipRect;
Icon:= TIcon.Create;
try
CursorInfo.cbSize:= SizeOf(CursorInfo);
if GetCursorInfo(CursorInfo) then
if CursorInfo.Flags = CURSOR_SHOWING then
begin
Icon.Handle:= CopyIcon(CursorInfo.hCursor);
if GetIconInfo(Icon.Handle, IconInfo) then
begin
CP:= CursorInfo.ptScreenPos;
//Transition mouse position...?
CP.X:= CP.X + M.Left;
CP.Y:= CP.Y + M.Top; //No difference?
Bitmap.Canvas.Draw(
CP.X - Integer(IconInfo.xHotspot) - R.Left,
CP.Y - Integer(IconInfo.yHotspot) - R.Top,
Icon);
end;
end;
finally
Icon.Free;
end;
end;
end;
如何根據我使用的顯示器正確地轉換鼠標位置?
呃拍得好,這個'+'本來是一個''''它做到了。如果你刪除了這兩行,它就不起作用 - 它只會讓光標顯示在光標位於主顯示器上,但不顯示截屏的實際顯示器。 –
哦,我明白了,你用了兩次R! R的值是位圖rect,它總是固定在0,0。 –
這是值得回收的,因爲'如果DrawCursor然後開始'標誌着一個不同的代碼塊的開始。因此,我留下了一個簡短的名字,比如'R'。 –