Delphi - Twebbrowser和截图功能。错误?

时间:2016-08-09 13:30:14

标签: delphi screenshot twebbrowser

当我在Twebbrowser上导航后尝试截取(前景窗口的)屏幕截图时,功能screenshot()尝试捕获但创建一个0kb的screenshot.bmp。

这仅在我首次使用Webbrowser1.Navigate(url)时再现。 如果我没有导航,通常会截取任何窗口的屏幕截图。

(如果我不隐藏Form2,我可以在使用twebbrowser导航后获取该表单的屏幕截图,但这并不是我想要的截图())

PS:我编辑了windows reg入门键以使用IE11;

以下是代码:

procedure ScreenShot();

const
  FullWindow = false;
var
  Win: HWND;
  DC: HDC;
  Bmp: TBitmap;
  FileName: string;
  WinRect: TRect;
  Width: Integer;
  Height: Integer;

begin
  Form2.Hide;
  try
    Application.ProcessMessages;
    Win := GetForegroundWindow;

    if FullWindow then
    begin
      GetWindowRect(Win, WinRect);
      DC := GetWindowDC(Win);
    end 
    else
    begin
      Windows.GetClientRect(Win, WinRect);
      DC := GetDC(Win);
    end;
    try
      Width := WinRect.Right - WinRect.Left;
      Height := WinRect.Bottom - WinRect.Top;

      Bmp := TBitmap.Create;
      try
        Bmp.Height := Height;
        Bmp.Width := Width;
        BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);
        FileName := 'Screenshot_' +
                    FormatDateTime('mm-dd-yyyy-hhnnss', Now());
        Bmp.SaveToFile(Format('./screenshot.bmp', [FileName]));
      finally
        Bmp.Free;
      end;
    finally
      ReleaseDC(Win, DC);
    end;
  finally
    Form2.Show;
    sleep(500);
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Screenshot();
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  Form2.Webbrowser1.Navigate('http://www.google.pt/');
end;

0 个答案:

没有答案