Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing in WinAPI

Status
Not open for further replies.

XerxesTheMighty

Programmer
Jun 15, 2003
72
US
I want a simple little code to send a file to the printer... MSDN.com([evil]) gives a 'simple little code', if you will (quoted directly[smarty]):
[pc3]
The sequence for a print job is as follows:
[ol]
[li]To begin a print job, call StartDocPrinter. [/li]
[li]To begin each page, call StartPagePrinter. [/li]
[li]To write data to a page, call WritePrinter. [/li]
[li]To end each page, call EndPagePrinter. [/li]
[li]Repeat 2, 3, and 4 for as many pages as necessary. [/li]
[li]To end the print job, call EndDocPrinter. [/li]
[/ol]
(
But they won't give an 'implentation code'. If anyone who knows how to do this stuff, or possibly an easier way,
Answer ASAP[hourglass].
 
Those instructions seem pretty straightforward. What part are you having difficulty with?
 
I dealt with printing job fully in my last project....with SDK way..
where it is giving you problem...
 
I just don't understand it. What I want is some code that will teach me how to use each functions properly, because after several hours of studying on MSDN.com (and others), I still have no idea how use the functions.
 
szPrinterName: NULL-terminated string specifying printer name

lpData: Pointer to raw data bytes

dwCount: Length of lpData in bytes
What are these and how do I obtain them? These are what causing all the errors.
 
Also (dwJob = StartDocPrinter(hPrinter, 1, (unsigned char *)&DocInfo )) == NULL (therefore, not going to print). Why? The reason for the (unsigned char *) is because other wise it'll give an error:
[pc3]
error C2664: 'StartDocPrinterA' : cannot convert parameter 3 from 'struct _DOC_INFO_1A *' to
'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
 
I found the reason for the above error, it didn't get the name of the printer (had to input it manually). The lpData and dwCount I got figured out, too. So, I finally got it to print something. Now I just need to know two things. One is word wrapping, when I print it cuts off the text when it reaches the end of the paper. Two: Getting name of printer and other data from the default print dialog. I can get the default print dialog but I need to get all the data from it.
 
You can get information about each installed printer by calling EnumPrinters. A short program is below that shows how to use it. If you need info beyond the name, description and comment, try the PRINTER_INFO_2 structure instead.

As far as I know, when using the Win32 API directly, you'll need to wrap your text manually, meaning that you'll have to keep track of where you are horizontally on the page, based on the width of the characters and the width of the page.

Doing this will probably be easier if you use a device context when printing, instead of sending raw data to the printer, as I think you're doing. This way you can use GetTextMetrics to easily determine the available horizontal space, just as you would on any other window. See this article:


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void)
{
DWORD needed = 0, returned;
BOOL result;
LPBYTE printers;
DWORD i;

/* Find out the number of how much space is needed to store printer info */
(void) EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 1, NULL, 0, &needed, &returned);

printers = malloc(needed * sizeof *printers);
if (printers == NULL) {
fputs(&quot;malloc failed\n&quot;, stderr);
return EXIT_FAILURE;
}

result = EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 1, printers, needed,
&needed, &returned);

if (!result) {
printf(&quot;EnumPrinters failed (Error = %ld)\n&quot;, (long) GetLastError());
return EXIT_FAILURE;
}


for (i = 0; i < returned; ++i) {
PRINTER_INFO_1 *printer = (PRINTER_INFO_1 *) (printers + (i * sizeof *printer));
printf(&quot;Name = %s; Description = %s; Comment = %s\n&quot;,
printer->pName, printer->pDescription, printer->pComment);
}

return 0;
}
 
After getting your printer name route is straight forward

StartDoc
StartPage
...
TextOut(PrinterDC,x,y,Text,strlen(Text)...
.....
EndPage
EndDoc
....
Hope it helps
 
Ok, now that I've done a DC, what would I define in TEXTMETRIC (LONG tmHeight;
LONG tmAscent;
LONG tmDescent;
LONG tmInternalLeading;
LONG tmExternalLeading;
LONG tmAveCharWidth;
LONG tmMaxCharWidth;
LONG tmWeight;
LONG tmOverhang;
LONG tmDigitizedAspectX;
LONG tmDigitizedAspectY;
char tmFirstChar;
char tmLastChar;
char tmDefaultChar;
char tmBreakChar;
BYTE tmItalic;
BYTE tmUnderlined;
BYTE tmStruckOut;
BYTE tmPitchAndFamily;
BYTE tmCharSet;
) for the GetTextMetrics(hdc, &tm);
 
You don't define anything. You want to pass GetTextMetrics a pointer to an empty TEXTMETRIC struture and GetTextMetrics will fill in the structure based on the device context you provide in the first argument.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top