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...