Solved

Output to printer ?

Posted on 1998-10-09
6
319 Views
Last Modified: 2012-05-05
C++ ( CodeWarrior, PMac ), how do I direct output to the printer instead of the screen ?
0
Comment
Question by:ianscott
  • 4
6 Comments
 
LVL 3

Expert Comment

by:elfie
ID: 1174713
Don't know code warrior , but try a freopen of stdout.
0
 
LVL 3

Accepted Solution

by:
NullTerminator earned 50 total points
ID: 1174714
Start by creating a CPrintDialog object.  do a do modal on it.  Then get relevant printer information from it.

CPrintDialog pdlg;
pdlg.DoModal();
CString strDriver, strDevice, strPort;
strDriver = pdlg.GetDriverName();
strDevice = pdlg.GetDeviceName();
strPort = pdlg.GetPortName();

// Then create a DC, in my case a member variable, (persistent scope)
LPDEVMODE lpDevMode = NULL;
lpDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);

if (!m_CDC.CreateDC(strDriver, strDevice, strPort, lpDevMode) )      {
        iOK = MessageBox(NULL, "Cannot print to device", "Print Parameters",
              MB_OK | MB_ICONINFORMATION);          
        GlobalFree(lpDevMode);
        return FALSE;
}
GlobalFree(lpDevMode);

//  Now that you have DC,
m_CDC.StartDoc();
m_CDC.StartPage()
m_CDC.TextOut(various params);
// eject
m_CDC.EndPage();
m_CDC.StarPage();
m_CDC.TextOut(more stuff);
m_CDC.EndPage();
m_CDC.EndDoc().

///////////////////////////////////////////////////////////////////////////////////
// Moving & Drawing - Default to calling parent class functions
// BOOL CPTPrinter::LineTo(CPoint ptPoint);
// NOTE INVERTED Y COORDINATES
// Some MapModes have inverted coordinate systems

BOOL CPTPrinter::LineTo(int x, int y)
{
      return m_CDC.LineTo(x, -y);
}        
CPoint CPTPrinter::MoveTo(int x, int y)
{  
       return m_CDC.MoveTo(x, -y);
}  

//  if your first few tries to print feed out blank pages, try a loop which increments the y value from minus 5000 to plus 5000, step by 100's,  print some text and the Y value for reference.  Study the various SetMapMode() choices.  I prefer high english in .001 of inch.  Origin is upper left,  postive x to the right,  negative Y moves downward,

// (Quirk in VC,  RESET MAPMODE after every StartPage() )

'\0'

0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174715
Followup,  Sorry if I assumed windows and VC++,

If you are just doing console stuff,  real easy way is to use
fprintf(stdprn, "My String of stuff with %s", "Args As Desired");

'\0'
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:ianscott
ID: 1174716
I *am* just doing console stuff - is :
 
fprintf(stdprn, "My String of stuff with %s", "Args As Desired");

- standard C++ o/p to printer ?

Could you clarify what the commands do ?

fprintf=?
stdprn=?

Also, what are the args ?

Thanks,
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174717
fprintf is a standard library function that formats text and outputs it to a FILE stream buffer.  In this case I use the predefined system FILE stream represented by stdprn.  Other standards are stderr, stdout, stdin.

for more information on args, study the specification for printf and format specifiers

fprintf writes to the first argument,  instead of to stdout like printf

You can also use sprintf which writes to a character stream

char s[80];
int i = 10;
sprintf(s, "some Text with number %d", i);  // %d is replace with value of i
//  contents of s is now
//  "some Text with number 10"

'\0'

'\0'
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174718
PS
#include <stdio.h>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question