C++/Setting Printer
Expert: Ralph McArdell - 10/19/2004
QuestionHi Ralph,
Thanks for the helpful reply of yours.It was quite educating.The irony is that i have all those methods.My problem is that i cant use Startpage/endpage since i m using the webbrwoser's execWb method to print a page using the particular printer which uses the default printer of the system.In this case i m unable to use the metohs mentioned by you.Can i anyway use SetPrinter() to set the printer programitcaly for the application?
Thanks again.
-------------------------
Followup To
Question -
Hi!
I have more than one printer installe don my PC.through visucall c++ ,i want to print a job to a prticular printer, in windows 2000.How can i do that using Setprinter().
thanks
Answer -
I presume you mean that you wish to do this using the Win32 GDI printer API functions, such as SetPrinter. I am not sure from your question if you know what SetPrinter does or not, or how much you know about printing under the MS Windows GDI. I shall assume that you have very little knowledge and apologise now if you already know some or all of what follows.
First let's deal with SetPrinter. SetPrinter sets the data or state of a particular printer (such as pause, resume, clear). It does _not_ set which printer to use for a print job. If you read the documentation on SetPrinter you will note that you have to pass it a HANDLE to a previously opened or added printer - and you use the functions OpenPrinter and AddPrinter to achieve these tasks. The only way SetPrinter might be used in the way you wish is to set a particular printer as the default printer. However, this seems to be a waste of time to me as you would then know the name of the printer and could use it directly anyway! Also it would modify your system unless of course you reset the original default printer afterwards...
Next I shall point you in the direction of the main MS Windows GDI printing and print spooler documentation and I suggest you read through it if you have not done so already. You will notice that before you get to the reference material on SetPrinter etc... there are sections leading you through the general philosophy behind GDI printing and how to go about printing. If you have a local copy of the MSDN Library then you can find the GDI printing reference material in the Windows GDI section under Printing and Print Spooler - for the July 2004 edition this is on the documentation path Graphics and Multimedia / Windows GDI / SDK Documentation / Windows GDI / Printing and Print Spooler. If you do not have a local copy of the MSDN library to hand then you can find it online at
http://www.msdn.microsoft.com/library/default.asp - the printing documentation is on the documentation path Win32 and COM Development / Graphics and Multimedia / GDI / Windows GDI / Printing and Print Spooler.
Now GDI 101: GDI was the original MS Windows graphics sub-system. It stands for Graphics Device Interface. To perform any drawing on any device you require a device context or DC. Windows keeps a cache of DCs to the display device, but if you want to draw on something else - such as a printer - then you need to create your own, and delete it when finished. The GDI API functions to do this are CreateDC and DeleteDC and are documented in the MSDN library in the section Device Contexts - at the same level in the documentation tree as (the root) Printing and Print Spooler. It is to CreateDC that you provide the printer name information. Note that generally you would use the common printer dialog boxes provided by Windows. These are available using the PrintDlgEx or PrintDlg API functions (documented under User Interface Design and Development / Windows Management / User Input / Common Dialog Box Library / Common Dialog Box Reference / Functions - or similar documentation path). They allow you to set up the printer, its settings and the print job settings and can return a DC handle to the selected printer.
Having obtained a handle to a printer DC you next have to set up a print job abort call-back function and modeless print job cancel dialog box.
Finally you get to start printing the document. You need to call StartDoc and EndDoc functions at the beginning and end of the print job and StartPage and EndPage for each page. Finally you use GDI drawing functions to print the document. All this is covered in the Printing and Print Spooler documentation - with some simple sample C code.
Now you may need to programmatically find the names of available printers installed on your system. In MS Windows functions that iterate through sets of things are prefixed with the letters Enum (for Enumerate). Generally you call the Enum function and either provide a call back function which is called for each item in the set, or pass output parameters which the function fills in with the data for all enumerated items. In this case the function you require is EnumPrinters, and it is of the type that you pass a buffer which is filled in by EnumPrinter with PRINTER_INFO_n structures where n is 1, 2, 4 or 5, depending on the requested printer information level (err, this being 1, 2, 4 or 5). From a quick peek level 2 and PRINTER_INFO_2 structures look good for Windows 2000.
You might like to investigate using the MFC framework and its support for printing - see the Visual Studio .NET documentation for Visual C++ / Adding Functionality / Visual C++ Libraries / MFC / User Interface / Printing and Print Preview or locate the relevant material for your version of Visual C++.
Hope this helped a little.
AnswerI have not done so, but the documentation (go read it!) states that this is one of those things you can do with SetPrinter for Windows 200/XP – aren't you lucky...However you should perhaps look at SetDefaultPrinter instead as this is a lot less complex – it just takes the name of the printer as a LPCTSTR. I suggest you obtain the name of the previous default printer _before_ you switch over so you can swap back after you are done. Like to guess the name of the function to get the default printer name? If you thought GetDefaultPrinter then award yourself a large something nice...
Hope this is what you need. You might also like to check for further information on printing using IWebBrowser or related interfaces. For example you might like to look at the article “Beyond Print Preview: Print Customization for Internet Explorer 5.5” . Part 2 says it will allow customisation of the Print dialog... (See also the related Print Template documentation in the Internet Explorer documentation). This may or may not be of help - I only did a quick search of the MSDN library and I could find no easy way to select the printer other than displaying the print dialog – which may not be what you wish.