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

How to make a screen shot of my actual window

我定义了一个 Tactionlist,其中包含对 show/hide 我的表单的所有操作。这可以是模态 (showmodal) 或非模态 (visible:=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;

我的"scan"套路如下:

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;

当我 运行 这段代码时,我得到了主窗体的大约前 150 个操作,然后是一些其他窗体,如日志或浏览器或......几乎从来没有我想要的窗体。

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

有什么想法可以捕捉到我实际打开的 window?

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

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;

window 的标题是在展会期间设置的:

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

您遇到的问题是由于 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

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