Perhaps it's the most convenient way, but it's not a reliable one; at least
if you need to print to something other than the default printer.
I did not use your code, but as I said before, I have no problem changing
the default printer. I just used the win api:
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDefaultPrinter(string name);
However, this is a bad solution because (in my opinion), it is not good to
change a user's default, even if you change it back after printing. Also,
this does not reliably change the printer that IE uses. It works just fine
on my development machine (Win 2003), but on a couple of test machines (Win
XP/IE 6), IE still prints to the previous default printer. I tried the
following to let IE know that the printer changed, but this does not work (at
least on some machines).
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags,
uint uTimeout, int lpdwResult);
SendMessageTimeout((IntPtr)HWND_BROADCAST,
WM_SETTINGCHANGE,
(UIntPtr)0, (IntPtr)0,
SendMessageTimeoutFlags.SMTO_NORMAL,
1000,
result);
Even if this worked, it seems like a bad hack (changing the default printer).
My conclusion after many hours at this problem is that IE (WebBrowser
control) is not up to the task of printing reliably from a Windows Forms. I
would be a 'happy camper' if someone could prove me wrong.
Maybe you could suggest to the team in charge of WebBrowser that they should
add a bit more functionality to the managed control so that you can specify a
printer. Ability to define headers, footers, and margins from managed code
(and without resorting to a print template) would also be a great addition.
Thanks for the response.
Thomas
[quoted text, click to view] "Luke Zhang [MSFT]" wrote:
> I think WebBrower control is the most convenient way to print a HTML
> document. So, the problem is, sending documents to the wrong printer.
> WebBrower control will print to the default printer, if we set the default
> printer correctly, and then we can fix the problem, isn't it? Have you
> tried the code in my previous message to set the default printer? Can it
> help on the problem "ending documents to the wrong printer"?
>
> Regards,
>
> Luke Zhang
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>