如何检测打印命令在TWebBrowser中完成? [英] How to detect Print command has finished in TWebBrowser?

查看:112
本文介绍了如何检测打印命令在TWebBrowser中完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

同样的预览:
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.

更新(1)

根据这个问题我测试了 OnCommandStateChange
在打印对话框中打印或取消后被触发。但是在对话框打开之前,可以点击1或2次

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.

更新(2)
找到一个可以做的伎俩的解决方法(这是一个基本的想法):

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;

用法:

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

适用于 OLECMDID_PRINT OLECMDID_PRINTPREVIEW
请告诉我你的想法...

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

推荐答案

我一直在寻找一个解决方案,我发现 PRINT_WAITFORCOMPLETION 标志前几天,但不能让它上班。诀窍很简单(参见 note nr。4)。传递的第三个参数错误 ExecWB 方法vs85%29.aspxrel =nofollow> OLECMDID_PRINT 命令作为变体类型 VT_I4 ,但它是超载的, a href =http://msdn.microsoft.com/en-us/library/aa769937%28v=vs.85%29.aspx =nofollow> PRINT_WAITFORCOMPLETION 必须转换为确切类型 VT_I2 ,Delphi表示为 smallint

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.

这是如何使打印对话框模态(也回答< a href =http://stackoverflow.com/questions/2491560/twebbrowser-modal-print-dialog>此意外:)

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;

如果用户将文档发送到打印机队列或取消对话框,则无法获取任何反馈。 IDM_PRINT 没有输出值,这将返回此值。另一件事是,即使用户接受打印对话框也不意味着文档将被物理打印。为此,您将不得不像Remy所说,监视打印机队列。

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.

这篇关于如何检测打印命令在TWebBrowser中完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆