Author: Lou Adler
I currently use DC := GetDC(GetDesktopWindow) to get a screen capture. What do I
need to do do to capture the desktop when multiple monitors are present instead of
just the primary monitor?
Answer:
Solve 1:
On multi-monitor systems the desktop is one big bitmap. On my dual-monitor system
the following captures both desktops into one big bitmap. If you just wanted the
second monitor you could adjust the left setting of the TRect to be captured. Keep
in mind, however, that some people might arrange monitors other than side by side
with monitor 1 on the left and monitor 2 on the right.
1
2 procedure GrabScreen(const SourceRect: TRect; Bitmap: TBitmap);
3 var
4 ScreenCanvas: TCanvas;
5 begin
6 ScreenCanvas := TCanvas.Create;
7 try
8 ScreenCanvas.Handle := GetDC(0);
9 try
10 Bitmap.Width := SourceRect.Right - SourceRect.Left;
11 Bitmap.Height := SourceRect.Bottom - SourceRect.Top;
12 Bitmap.Canvas.CopyRect(Rect(0, 0, Bitmap.Width, Bitmap.Height), ScreenCanvas,
13 SourceRect);
14 finally
15 ReleaseDC(0, ScreenCanvas.Handle);
16 ScreenCanvas.Handle := 0;
17 end;
18 finally
19 ScreenCanvas.Free;
20 end;
21 end;
22
23 procedure TForm1.Button1Click(Sender: TObject);
24 begin
25 GrabScreen(Rect(0, 0, Screen.Monitors[0].Width + Screen.Monitors[1].Width,
26 Screen.Height),
27 Image1.Picture.Bitmap);
28 Caption := Format('Width %d Height %d', [Image1.Width, Image1.Height]);
29 end;
Solve 2:
Referring to Answer 1: Finally, I went through and used the debugger to check out
all of the Screen variable fields at runtime and found what I needed:
30 { ... }
31 DC := GetDC(GetDesktopWindow);
32 BMPImage := TBitmap.Create;
33 try
34 BMPImage.Width := Screen.DesktopWidth;
35 BMPImage.Height := Screen.DesktopHeight;
36 BitBlt(BMPImage.Canvas.Handle, 0, 0, BMPImage.Width, BMPImage.Height,
37 DC, Screen.DesktopLeft, Screen.DesktopTop, SRCCOPY);
38 finally
39 ReleaseDC(GetDesktopWindow, DC);
40 end;
41 { ... }
|