IE Printing HTML file through ExecWB - print dialog comming up behindmy app - help!

A

Andrew Meador

I have implemented a printing scenario where an html file is
printed using the the following code:

public void PrintHtmlFile(string url)
{
RegistryKey IERegKey;
string header = null;
string footer = null;
object o = null;
InternetExplorerClass ie = null;
IWebBrowser wb = null;


// Get registry key for IE PageSetup and current values
for header and footer. If IE hasn't been
// through a print cycle, the PageSetup key will not
exist. Best thing to do is to run IE through
// at least one print job so the key will be set. With the
code below, the print will work, but the
// first printout will have headers and footers based on
default settings created on first print cycle.
// Once the registry key is in place - the following will
effectively hide the header and footer on
// printouts.
IERegKey = Registry.CurrentUser.OpenSubKey("software\
\Microsoft\\Internet Explorer\\PageSetup", true);
if (IERegKey != null)
{
footer = IERegKey.GetValue("footer", 0).ToString();
header = IERegKey.GetValue("header", 0).ToString();


// Set header and foot to blank
IERegKey.SetValue("footer", "");
IERegKey.SetValue("header", "");
}


// Create Internet Explorer instance - this (or the wb
object below) will fail if IE has not been through
// it's initial first run sequence.
ie = new InternetExplorerClass();


// Create web browser page - this (or the ie object above)
will fail if IE has not been through
// it's initial first run sequence.
wb = (IWebBrowserApp)ie;


//ie.Visible = true; // Shows the IE instance.


wb.Navigate(url, ref o, ref o, ref o, ref o);


// Wait for IE object to be ready - otherwise intermittant
errors occur when print is initiated.
while (ie.ReadyState !=
SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
Application.DoEvents();
}
// Print the page - prompt user
ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, ref o, ref o);


if (IERegKey != null)
{
// Replace IE Page settings
IERegKey.SetValue("footer", footer);
IERegKey.SetValue("header", header);
}


This works pretty well. Anyway, when the user clicks the 'Print'
button on the application form, this sub is called with the full path
to the file passed via the URL parameter. The user then gets a Print
Dialog box letting them select the printer to print to. All
they have to do it click ok/print and it prints out. The problem is
that the print dialog box is coming up behind the application. How
can
I make the dialog box come up on top of the application?


Thanks in advance!
 
R

raylopez99

   This works pretty well. Anyway, when the user clicks the 'Print'
button on the application form, this sub is called with the full path
to the file passed via the URL parameter. The user then gets a Print
Dialog box letting them select the printer to print to. All
they have to do it click ok/print and it prints out. The problem is
that the print dialog box is coming up behind the application. How
can
I make the dialog box come up on top of the application?

   Thanks in advance!

PrintDialog is a standard dialog that is well supported--I don't see
why you are playing with the registry keys for a simple PrintDialog.
This looks dangerous--what if the user loses power during your
PrintDialog method? Then his registry tree is hosed.

To answer your question however, simply change the "Z-Order" of your
box (controls) programically.

The "Z-Order" determines which controls are on top. The order is
according to how the controls are added to the container using
this.Controls.Add(this.zorder0button) in the "InitializeComponent()"
method during compilation, or, you can use the SetChildIndex property
if you have a collection of controls. Also, during runtime (which
your question asks), you can change the "z-order" by the methods Bring
To Front (sets z-order to zero) or "Send to Back". Use the
Control.BringToFront and Control.SendToBack methods.

Do us a favor and post any solution you find so maybe I can use it in
my examples library.

RL
 
A

Andrew Meador

PrintDialog is a standard dialog that is well supported--I don't see
why you are playing with the registry keys for a simple PrintDialog.
This looks dangerous--what if the user loses power during your
PrintDialog method?  Then his registry tree is hosed.

To answer your question however, simply change the "Z-Order" of your
box (controls) programically.

The "Z-Order" determines which controls are on top.  The order is
according to how the controls are added to the container using
this.Controls.Add(this.zorder0button) in the "InitializeComponent()"
method during compilation, or, you can use the SetChildIndex property
if you have a collection of controls.  Also, during runtime (which
your question asks), you can change the "z-order" by the methods Bring
To Front (sets z-order to zero) or "Send to Back".  Use the
Control.BringToFront and Control.SendToBack methods.

Do us a favor and post any solution you find so maybe I can use it in
my examples library.

RL

Sorry, I didn't see your response to this until just now. Ooops!

I wouldn't do this registry stuff if I could avoid it, but I havn't
been able to find another way. If I don't do this, they user gets the
Internet Explorer default (or user modified) headers and footers
printing out on my report. I want this report to look clean, so the IE
headers and footers need to go away one way or another.

I don't understand how to set the z-order or how to call the
BringToFront method of this dialog box. I call the ie.ExecWB method -
which in turn brings up the print dialog box. The ie object is created
from the SHDocVw.dll file referenced in this app. I don't know how to
set the property or call the mothod against this since it is not a
control on my form. How would I do this given the code givien
initially?

Thanks!
 
R

raylopez99

I don't understand how to set the z-order or how to call the
BringToFront method of this dialog box. I call the ie.ExecWB method -
which in turn brings up the print dialog box. The ie object is created
from the SHDocVw.dll file referenced in this app. I don't know how to
set the property or call the mothod against this since it is not a
control on my form. How would I do this given the code givien
initially?

Thanks!

Well beats me. I've only been coding for about a month. But, here's
an idea (besides getting somebody more knowledgeable to post in this
thread): try setting all the other controls on your form--the ones
you *do* have control over, to "Send to Back" (one by one). By the
process of elimination, eventually the only control left (and in
front) will be the Print Dialog box.

Let us know if that works.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top