I want to paint the whole form including its caption bar and frame on a TBitmap
object.
GetFormImage
is cool, but it has two problems:
Here is a little function that I've used that you might find useful.
Edit: Oops I didn't fully read the question. This probably doesn't work with a hidden window, but I'm not sure anything would unless the form could draw the frame itself, but it doesn't, the OS does.
function GetScreenShot(const aWndHandle:THandle; AeroAware:boolean=true):TBitmap;
var
wWindow: HDC;
wRect : TRect;
wDesktop : THandle;
begin
Result := TBitmap.Create;
try
if AeroAware then
wDesktop := GetDesktopWindow
else
wDesktop := aWndHandle;
wWindow := GetDC(wDesktop);
try
Result.PixelFormat := pf32bit;
GetWindowRect(aWndHandle, wRect);
Result.Width := wRect.Right-wRect.Left;
Result.Height := wRect.Bottom-wRect.Top;
if AeroAware then
BitBlt(Result.Canvas.Handle, 0, 0, Result.Width, Result.Height, wWindow, wRect.Left, wRect.Top, SRCCOPY)
else
BitBlt(Result.Canvas.Handle, 0, 0, Result.Width, Result.Height, wWindow, 0, 0, SRCCOPY);
Result.Modified := True;
finally
ReleaseDC(wDesktop, wWindow);
end;
except
Result.Free;
Result := nil;
end;
end;
The key to accessing non-client area is GetWindowDC function, everything else is blitting as usual:
procedure TForm5.FormClick(Sender: TObject);
var
Bitmap: TBitmap;
DC: HDC;
FileName: string;
begin
Bitmap := TBitmap.Create;
try
Assert(HandleAllocated);
DC := GetWindowDC(Handle);
Win32Check(DC <> 0);
Bitmap.SetSize(Width, Height);
Win32Check(BitBlt(Bitmap.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY));
FileName := Name + '.' + GraphicExtension(TBitmap);
Bitmap.SaveToFile(FileName);
ShellExecute(HWND_DESKTOP, nil, PChar(FileName), nil, nil, SW_NORMAL);
finally
ReleaseDC(Handle, DC);
Bitmap.Free;
end;
end;
For the second case of hidden (not painted!) window - see a comment from RRUZ.
About capturing form image when hidden, use
AlphaBlend := true;
AlphaBlendValue := 0;
while form is shown. The user will not see the form but GetFormImage() will capture its canvas. I think this can work with "OnTheFly" suggestion too.