Есть следующая функция, получающая скриншот
procedure ScreenShot(const Width, Height: LongInt; const Filename: PChar);
var
Bih: TBitmapInfoHeader;
Bfh: TBitmapFileHeader;
CDC, DC: HDC;
CBmp: HBitMap;
Bmp: Pointer;
BmpInfo: TBitMapInfo;
F, Written, Size: LongWord;
begin
DC := GetDC(0);
CDC := CreateCompatibleDC(DC);
Bih.biSize := SizeOf(TBitmapInfoHeader);
Bih.biWidth := Width;
Bih.biHeight := Height;
Bih.biPlanes := 1;
Bih.biBitCount := GetDeviceCaps(DC, BITSPIXEL) * GetDeviceCaps(DC, PLANES);
if Bih.biBitCount = 32 then Bih.biBitCount := 24;
Bfh.bfType := $4D42;
Bfh.bfOffBits := SizeOf(TBitmapFileHeader) + SizeOf(TBitmapInfoHeader);
Size := Width * Height * 3;
BmpInfo.bmiHeader := Bih;
CBmp := CreateDIBSection(DC, BmpInfo, 0, Bmp, 0, 0);
SelectObject(CDC, CBmp);
StretchBlt(CDC, 0, 0, Width, Height, DC, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SRCCOPY);
F := CreateFile(Filename, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, 0, 0);
WriteFile(F, Bfh, SizeOf(TBitmapFileHeader), Written, nil);
WriteFile(F, Bih, SizeOf(TBitmapInfoHeader), Written, nil);
WriteFile(F, Bmp^, Size, Written, nil);
CloseHandle(F);
end;
Она отлично работает, если у программы нет окна, но если создать простейшее окно, хотя бы функцией CreateWindowEx, то скрин не создается

Как быть?