Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an C++ app that allows the user to print multiple documents. It uses a class based on CHTMLView and on a print request sends a OLECMDID_PRINT once for each document. This works but is crude as each document produces a print dialog that the user has to interact with. What I'd like to do is either throw up a print dialog and capture the user's desires and pass that along to the browser, maybe via the print template, which I'm already modifying, or find a way to get the user response to the browser's print dialog and use that for all subsequent docs with OLECMDEXECOPT_DONTPROMTUSER.

So far I've found many trying to do similar things but no answers. Setting the default printer isn't an acceptable option as that will affect all applications. I don't see where the templateprinter in the print template allows selecting the printer and can find no way to trap any event from the browser's print dialog.

I'm stuck, anyone have an idea?
Posted

1 solution

Hi,

I just had to do exactly this in C# today ;). In my code i had to print a bundle of documents in all kind of formats, I've added below the relevant bits.

I could not find anyway to do this without changing the default printer. I just made sure I changes the default for as little time as necessary.

Here is my code:

I have derived the WebBrowser control and added my own Print method.

C#
public partial class MyWebBrowser : WebBrowser


C#
public void Print(PrinterSettings ps)
{
    PrinterSettings p = new PrinterSettings();
    string oldPrinter = p.PrinterName;
    SetAsDefaultPrinter(ps.PrinterName);
    this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_PRINT,
        OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
        null, IntPtr.Zero);
    SetAsDefaultPrinter(oldPrinter);
}
static int SetAsDefaultPrinter(string printerDevice)
{
    int ret = 0;
    string path = "win32_printer.DeviceId='" + printerDevice + "'";
    using (ManagementObject printer = new ManagementObject(path))
    {
        ManagementBaseObject outParams =
        printer.InvokeMethod("SetDefaultPrinter",null, null);
        ret = (int)(uint)outParams.Properties["ReturnValue"].Value;
    }
    return ret;
}


and I call it this way:

C#
PrinterSettings ps = new PrinterSettings();
    PrintDialog p = new PrintDialog();
    if (p.ShowDialog() == DialogResult.OK)
    {
        ps = p.PrinterSettings;
// ...
// a few bits that are not relevant for this example.
// ...

            MyWebBrowser browser = new MyWebBrowser();
            browser.Url = new Uri(document);
            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
            browser.Print(ps);

// ...
    }



hope it helps.

Valery.
 
Share this answer
 
v2
Comments
GBrook 27-Dec-10 11:20am    
Thanks Valery, but as I mentioned in my post setting the default printer isn't an acceptable option in my case. The user could navigate away from my app while the default printer is changed.
Valery Possoz 30-Dec-10 14:51pm    
I've got the same problem an another thing I have been looking at for my application is replacing the webbrowser control by an open source .net wrapper of webkit.
http://sourceforge.net/projects/webkitdotnet/
Google returns a few code sample, this one for example: http://shelastyle.net/blog/webkit-vs-ie-browser-control-in-c/

Valery.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900