如何制作我实际窗口的屏幕截图

时间:2015-05-16 19:43:19

标签: delphi screenshot delphi-2009

我定义了一个Tactionlist,其中包含显示/隐藏表单的所有操作。这可以是模态(showmodal)或非模态(可见:= true)。我找到了一些代码来捕捉屏幕截图:

procedure GetScreenShot(shotType: TScreenShotType; var img: TJpegImage);
var
  w,h: integer;
  DC: HDC;
  hWin: Cardinal;
  r: TRect;
  tmpBmp: TBitmap;
begin
  hWin := 0;
  case shotType of
    sstActiveWindow:
      begin  //This is what I use
        //only the active window
        hWin := GetForegroundWindow;
        dc := GetWindowDC(hWin);
        GetWindowRect(hWin,r);
        w := r.Right - r.Left;
        h := r.Bottom - r.Top;
      end;  //sstActiveWindow
    sstActiveClientArea:
      begin
      end;  //sstActiveClientArea
    sstPrimaryMonitor:
      begin
      end;  //sstPrimaryMonitor
    sstDesktop:
      begin
      end;  //sstDesktop
    else begin
      Exit;
    end;  //case else
  end;  //case

  //convert to jpg
  tmpBmp := TBitmap.Create;
  try
    tmpBmp.Width := w;
    tmpBmp.Height := h;
    BitBlt(tmpBmp.Canvas.Handle,0,0,tmpBmp.Width,
      tmpBmp.Height,DC,0,0,SRCCOPY);
    img.Assign(tmpBmp);
  finally
    ReleaseDC(hWin,DC);
    FreeAndNil(tmpBmp);
  end;  //try-finally
end;

我的“扫描”程序如下:

for ACnt := 0 to GenActions.ActionCount - 1 do
    begin
    try
    LogBook.ML(Format('%d. Aktion %s gestartet',[ACnt,quotedstr(GenActions.Actions[ACnt].Name)]));
    if GenActions.Actions[ACnt].Tag > 0 then
         begin  // Action is ready for test
         TAction(GenActions.Actions[ACnt]).checked:=true;
         if GenActions.Actions[ACnt].Execute then
              begin
              LogBook.ML(Format('%d. Aktion %s erfolgreich ausgeführt',[ACnt,quotedstr(GenActions.Actions[ACnt].Name)]));
              if SaveScreens then   // var boolean
                   begin
                   img:=TJPEGImage.Create;
                   try
                   GetScreenShot(sstActiveWindow,img);         
                   img.SaveToFile(IncludeTrailingBackslash(Optionen.PictPfad.Text)+inttostr(ACnt)+'.jpg');
                   finally
                        img.Free;
                        end;
                   end;
              repeat
              sleep(100);
              Application.ProcessMessages;
              until not DM_Gen.TestTimer.Enabled ;  //for modal windows a timer sends modalresult:=mrcancel
              end;
         end
    else
         begin
         LogBook.ML(Format('%d Aktion %s nicht getestet',[ACnt,quotedstr(GenActions.Actions[ACnt].Name)]));
         end;
    except
         on E: Exception do
         LogBook.ML(Format('%d. Aktion hat Fehler %s gemeldet',[ACnt,E.Message]));
         end;
    end;
finally
    LogBook.ML('Testlauf beendet');
    end;

当我运行这段代码时,我得到了关于mainform的前150个动作,然后是其他一些形式,比如日志或浏览器......或者几乎不是我想要的形式。

我发现了一些推荐使用“findwindow”的帖子。这是我的问题,我不知道窗口的确切标题,因为在所有窗口中,在onshow事件中修改标题以显示实际信息。

任何想法如何抓住我实际打开的窗口?

所以问题是了解我的行为是如何运作的。这里有两个典型的例子:

procedure TDM_Gen.VALstVisActExecute(Sender: TObject);
begin
if Sender is TAction then
    begin   // set some properties
    end;
ListeVeranst_2.Visible:=VALstVisAct.Checked;
end;

procedure TDM_Gen.NewVAActExecute(Sender: TObject);
var
NewVA : TNewVeranstaltung;
begin
if Sender <> nil then
    begin
    if Sender is TButton then
         begin   //do something depending on who fired
         end;
    end;
try
NewVA:=TNewVeranstaltung.Create(nil);
case NewVA.ShowModal of
mrOk:
    begin    // e.g. refresh some lists
    end;
mrCancel:    
    begin    // clean up
    end;
end;

finally
    NewVA.Free;
    end;
end;

窗口的标题在onshow事件期间通过以下方式设置:

caption:=Format('This is window %s %s',[Param1, Param2]);

1 个答案:

答案 0 :(得分:3)

您遇到的问题是由ShowModal阻止呼叫的方法造成的。这意味着该调用之后的所有后续代码将在窗体关闭后开始执行。

以下简化示例中的代码流程:

  MyAction.Execute;
  CaptureScreen;

procedure TSomeForm.MyActionExecute(Sender: TObject);
var frm: TForm;
begin
  frm := TForm.Create(nil);
  try
    frm.ShowModal; // this call blocks execution of subsequent code in this method until form is closed
  finally
    frm.Free;
  end;
end;

将是MyAction.Execute -> frm.ShowModal -> frm.Close -> frm.Free -> CaptureScreen

您必须从模态表单中启动屏幕捕获才能捕获其屏幕。

相关问题