How to detect Print command has finished in TWebBrowser?

跟風遠走 提交于 2019-12-01 05:37:18

问题


procedure TForm1.Button1Click(Sender: TObject);
var 
  vaIn, vaOut: OleVariant;
begin
  WebBrowser1.Navigate('http://www.google.com');
  while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
    Application.ProcessMessages;
  WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);

  // HOWTO: WAIT until print <strike>job</strike> dialog is done or canceled

  // UPDATE (1):
  WebBrowser1.Enabled := False;
  WebBrowser1.OnCommandStateChange := WebBrowser1CommandStateChange;
end;

procedure TForm1.WebBrowser1CommandStateChange(Sender: TObject; Command: Integer; Enable: WordBool);
begin
  Memo1.Lines.Add(Format('%d : %d : %d', [WebBrowser1.QueryStatusWB(OLECMDID_PRINT), Command, Ord(Enable)]));
  // TODO: after LAST event when the print dialog closes:
  // WebBrowser1.OnCommandStateChange := nil;
end;

Same goes for Preview: WebBrowser1.ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT, vaIn, vaOut);

I need to wait (or trigger an event) until the Print / Print Preview dialogs are done, and user has selected either print or cancel.

UPDATE (1)

Based on this question I tested the OnCommandStateChange. It is fired after print or cancel in the Print dialog. but it can be fired 1 or 2 times before the dialog opens.

UPDATE (2) Found a workaround that might do the trick (it's a basic idea):

procedure TForm1.WaitPrintDialog;
var
  t1, t2: DWORD;
  w, wpd: HWND;
begin
  t1 := GetTickCount();
  t2 := t1;
  wpd := 0;
  while ((wpd = 0) and (t2 - t1 <= 5000)) do // 5 sec timeout
  begin
    w := FindWindowEx(0, 0, 'Internet Explorer_TridentDlgFrame', nil);
    if (w <> 0) and (GetWindow(w, GW_OWNER) = Self.Handle) then
    begin
      wpd := w;
    end;
    Application.ProcessMessages;
    t2 := GetTickCount();
  end;
  if wpd <> 0 then // found and no timeout
    while IsWindow(wpd) and (not Application.Terminated) do
    begin
      Application.HandleMessage; // Application.ProcessMessages;
    end;
end;

usage:

WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);
WaitPrintDialog;
ShowMessage('Print Done!');

works both for OLECMDID_PRINT and OLECMDID_PRINTPREVIEW please tell me what you think...


回答1:


When I've been looking for a solution I've found the PRINT_WAITFORCOMPLETION flag few days ago but can't get it to work. And the trick was quite easy (see note nr. 4). I've been wrong with passing the third parameter of ExecWB method for the OLECMDID_PRINT command as variant type VT_I4 but it is overloaded and for PRINT_WAITFORCOMPLETION must be converted to the exact type VT_I2, what is in Delphi represented as a smallint.

Here is how to make the print dialog modal (also answer to this by accident :)

procedure TForm1.Button1Click(Sender: TObject);
var
  vaIn: OleVariant;
  vaOut: OleVariant;
const
  PRINT_WAITFORCOMPLETION = $02;
begin
  WebBrowser1.Navigate('http://www.google.com');
  while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
    Application.ProcessMessages;

  vaIn := OleVariant(VarAsType(PRINT_WAITFORCOMPLETION, varSmallint));
  WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);

  ShowMessage('Print dialog has been closed ...');
end;

Unfortunately you can't get any feedback if user sent the document to the printer queue or cancelled the dialog. The IDM_PRINT has no output value, which would return this. Another thing is that even if user accepts the printing dialog it doesn't mean that the document will be physically printed. For this you would have to, as Remy said, monitor the printer queue.




回答2:


The print job is spooled and outputted to the printer driver by the OS in the background. The WebBrowser does not tell you when it is finished. ExecWB() exits once the print job has been queued. You would have to monitor the printer queue directly to know what it is doing.




回答3:


The following code makes the Print dialogbox a model dialog box, so that only when the use done with the Print dialog he will come back to the application.

WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, 2, 0


来源:https://stackoverflow.com/questions/8376618/how-to-detect-print-command-has-finished-in-twebbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!