How to detect Print command has finished in TWebBrowser?

家住魔仙堡 提交于 2019-12-01 08:41:15
TLama

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.

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.

Sreeandh

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