How can I paint the whole form on a bitmap?

前端 未结 3 2000
小蘑菇
小蘑菇 2021-01-28 07:47

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:

3条回答
  •  青春惊慌失措
    2021-01-28 08:08

    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.

提交回复
热议问题