- Print Online from Console Application
- Posted by Michael R. Copeland on March 2nd, 2007
I have been trying to find basic information about how to print
(text) data to an online printer from a 32 Bit Console application. So
far, all I've seen is Windows/GUI related, and that's not what I need or
can use. I'm only looking for a _simple_ callable interface that mimics
NotePad (or even simpler) that I can use to deliver text line(s) to my
online printer.
Perhaps this isn't possible...but there must be _some_ code that does
that. Or, perhaps there's some utility/background function that I could
use to print my output... TIA
- Posted by Bertel Brander on March 4th, 2007
Michael R. Copeland skrev:
There is no real difference in printing from a console application and
from at windows application, you will need to do the same things.
I have a very small demo program here:
http://home20.inet.tele.dk/midgaard/tipwin20050130.html
If you set the PD_RETURNDEFAULT flag in PRINTDLG Flags there will
be no dialog-box to select printer; you will get the default one.
--
Just another homepage:
http://damb.dk
But it's mine - Bertel
- Posted by Michael R. Copeland on March 12th, 2007
Thanks much for your help so far - I was able to get a small
prototype to produce printed output. Now that I see your code working,
I find it necessary to (1) initialize the printer, (2) print several
lines, and (3) close the job. Your example doesn't support this, and I
have tried to modify it to do so - unsuccessfully... 8<{{
To summarize: I need to start a console application, print a number
of lines of output (I'd very much like to change the font and font size
throughout), and close the whole process when I terminate the job.
Here's the way I've modified your code (which doesn't work: it only
prints from the first call...). TIA
/* Code */
#include <windows.h>
#include <string>
using namespace std;
#define OWNER 0
PRINTDLG PrintDlgData;
HDC PrintDc;
DOCINFO DocInfo;
HFONT Font;
HGDIOBJ OldFont;
int Height;
HDC TDc = GetDC(OWNER);
bool startPrint()
{
memset(&PrintDlgData, 0, sizeof(PrintDlgData));
PrintDlgData.lStructSize = sizeof(PrintDlgData);
PrintDlgData.Flags = PD_RETURNDC;
PrintDlgData.hwndOwner = OWNER;
if(PrintDlg(&PrintDlgData))
{
Height = 16;
Height = MulDiv(Height,
GetDeviceCaps(PrintDc, LOGPIXELSY),
GetDeviceCaps(TDc, LOGPIXELSY));
ReleaseDC(OWNER, TDc);
PrintDc = PrintDlgData.hDC;
memset(&DocInfo, 0, sizeof(DocInfo));
DocInfo.cbSize = sizeof(DocInfo);
DocInfo.lpszDocName = "Whatever.txt";
Font = CreateFont(Height, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, "Arial");
OldFont = SelectObject(PrintDc, Font);
if(StartDoc(PrintDc, &DocInfo) < 0)
{
MessageBox(OWNER, "Failed to start printing", "Whatever",
MB_OK);
return false;
}
else // not sure what to do here...
{
return true;
}
}
return false;
} // startPrint
void stopPrint()
{
EndDoc(PrintDc);
SelectObject(PrintDc, OldFont);
return;
} // stopPrint
void printOnline(const string &Text, int pixels = 16)
{
Height = pixels;
Height = MulDiv(Height,
GetDeviceCaps(PrintDc, LOGPIXELSY),
GetDeviceCaps(TDc, LOGPIXELSY));
// ReleaseDC(OWNER, TDc);
TextOut(PrintDc, 200, 200, Text.c_str(), Text.size());
return;
} // printOnline
int main()
{
if(startPrint())
{
printOnline("The Phantom Spitter was here. (16 pixel)");
printOnline("The Phantom Spitter was here. (24 pixel)", 24);
printOnline("The Phantom Spitter was here. (12 pixel)", 12);
printOnline("The Phantom Spitter was here. (16 pixel)");
stopPrint();
}
return 0;
}
- Posted by Bertel Brander on March 12th, 2007
Michael R. Copeland skrev:
Please find a slightly improved version below.
It will adjust the vertical position and make sure that the text is
nicely aligned below each other.
It will print the text on seperate lines.
You still need to handle page change. You can get the height by:
int VertSize = GetDeviceCaps(PrintDc, VERTRES);
Then in printOnline you can change page by calling:
EndPage(PrintDc);
If VertOffset is greater than VertSize.
#include <windows.h>
#include <string>
using namespace std;
#define OWNER 0
PRINTDLG PrintDlgData;
HDC PrintDc;
DOCINFO DocInfo;
HFONT Font;
HGDIOBJ OldFont;
int Height;
int VertOffset = 10;
bool startPrint()
{
memset(&PrintDlgData, 0, sizeof(PrintDlgData));
PrintDlgData.lStructSize = sizeof(PrintDlgData);
PrintDlgData.Flags = PD_RETURNDC;
PrintDlgData.hwndOwner = OWNER;
if(PrintDlg(&PrintDlgData))
{
HDC TDc = GetDC(OWNER);
Height = 16;
Height = MulDiv(Height,
GetDeviceCaps(PrintDc, LOGPIXELSY),
GetDeviceCaps(TDc, LOGPIXELSY));
ReleaseDC(OWNER, TDc);
PrintDc = PrintDlgData.hDC;
memset(&DocInfo, 0, sizeof(DocInfo));
DocInfo.cbSize = sizeof(DocInfo);
DocInfo.lpszDocName = "Whatever.txt";
Font = CreateFont(Height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"Arial");
OldFont = SelectObject(PrintDc, Font);
if(StartDoc(PrintDc, &DocInfo) < 0)
{
MessageBox(OWNER, "Failed to start printing", "Whatever", MB_OK);
return false;
}
else // not sure what to do here...
{
return true;
}
}
return false;
} // startPrint
void stopPrint()
{
EndDoc(PrintDc);
SelectObject(PrintDc, OldFont);
DeleteObject(Font);
return;
} // stopPrint
void printOnline(const string &Text, int pixels = 16)
{
if(pixels != Height)
{
Height = pixels;
HDC TDc = GetDC(OWNER);
Height = MulDiv(Height,
GetDeviceCaps(PrintDc, LOGPIXELSY),
GetDeviceCaps(TDc, LOGPIXELSY));
ReleaseDC(OWNER, TDc);
SelectObject(PrintDc, OldFont);
DeleteObject(Font);
Font = CreateFont(Height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"Arial");
OldFont = SelectObject(PrintDc, Font);
}
TextOut(PrintDc, 200, VertOffset, Text.c_str(), Text.size());
VertOffset += Height;
return;
} // printOnline
int main()
{
if(startPrint())
{
printOnline("The Phantom Spitter was here. (16 pixel)");
printOnline("The Phantom Spitter was here. (24 pixel)", 24);
printOnline("The Phantom Spitter was here. (12 pixel)", 12);
printOnline("The Phantom Spitter was here. (16 pixel)");
stopPrint();
}
return 0;
}
--
Just another homepage:
http://damb.dk
But it's mine - Bertel
- Posted by Michael R. Copeland on March 13th, 2007
That was a big help, and it works. I have, however, encountered a
problem: the formatted text I'm printing doesn't line up. I suppose
it's a "font" thing, where the "Arial" font doesn't handle spaces and
their alignment, but I don't know what to do (with your code) to correct
it. The result of printing is something like this:
1234 First Name 12 M Mesa AZ
987 2nd Name 34 F Scottsdale AZ
and of course I wish it to appear as:
1234 First Name 12 M Mesa AZ
987 2nd Name 34 F Scottsdale AZ
I'd appreciate any thoughts you (or others) have on this.
- Posted by Bertel Brander on March 13th, 2007
Michael R. Copeland skrev:
In the Arial font not all letters and spaces have the same width,
to get that effect you will need to use a fix width font such as
"Courier" or "Lucida Console"
Moreover you will need to append spaces to make all fields have
the same number of characters. How to do that depend on how you
format the string to be printed.
--
Just another homepage:
http://damb.dk
But it's mine - Bertel
- Posted by Davide Guolo on March 13th, 2007
Dear Mr. Copeland,
have you already seen Printfil ?
You can find more info and a free trial version to download at
http://www.printfil.com
Regards,
Davide Guolo
aSwIt s.r.l.
--------------------------------------------------------------
Printfil - Windows Printing System for Applications
http://www.printfil.com
Odbc4All - Connection to ODBC Data Sources for any Application
http://www.aswit.com/odbc4all
@Kill - Batch Close Windows Applications - Freeware
http://www.aswit.com/akill
--------------------------------------------------------------
- Posted by Michael R. Copeland on March 15th, 2007
Lucida Courier did the trick. Thanks again...