Solved

C++Builder: Getting page width and height

Posted on 2001-08-09
4
446 Views
Last Modified: 2008-03-03
Hi,

My boss forced me to do everything the VCL way now, and I had to rewrite a complete printing routine that I wrote using Win32 code. My Win32 code used MM_LOMETRIC as mapping mode, so I could easily specify where I wanted things, and so it would print the same, regardless of the DPI of the printer. With my VCL version I pulled the same trick, but not using VCL functions, since TCanvas doesn't seem to provide a way of setting mapping modes. So I did it this way:

SetMapMode(pCanvas->Handle, MM_LOMETRIC);

TPrinter gives the page width and height nicely, and the help says that VCL automatically starts a new page if you draw text beyond the bounds of the page... but all this doesn't work right anymore (I fear it's cause of my manual setting of the mapping mode). If I draw a line that should be the width of the page, minus a margin on the left and right sides, then I get a line that starts nicely at the margin, but goes on and on as if the page is 2 - 3 times wider than it actually is. And also, if I draw text lower than the page height (umm.. that is... draw below where the page ends), then it just gets chopped off, no new page is started. This leaves me without some important information, the width and height of the page. If I can somehow calculate those, then I'm happy, and I don't need VCL to start a new page for me, I can do it myself. So my question(s) would be (I guess):

- Is there a way to set the mapping mode with VCL functions? (I passed TCanvas to my print function instead of TPrinter, cause I need to use the same function for previewing)
- If there isn't, is there a way I can still get the proper page width and height? (Maybe perform some calculations on the width/height values TPrinter gives me?)

Thanks in advance,



Paul
0
Comment
Question by:mgdPaul
  • 2
4 Comments
 
LVL 3

Expert Comment

by:andysalih
Comment Utility
hmmm will this do it


#include <iostream>
#include <iomanip>
using namespace std;
//


    int main() {
         /*notes: newer implementations use ios_base
         and not ios. Also they allow using the following
         syntax:
              cout << left;
         to set the alignment to the left/right
         */
         //
         cout.setf(ios::left,ios::adjustfield);
         cout << setw(20) << "Name:";
         cout.setf(ios::right,ios::adjustfield);
         cout << setw(60) << "Occupation:\n";
         //
         /* change fill character from ' ' to '.' */
         //
         cout.fill('.');
         //
         cout.setf(ios::left,ios::adjustfield);
         cout << setw(20) << "*LuckY*";
         cout.setf(ios::right,ios::adjustfield);
         cout << setw(60) << "Student -- Computer Science\n";
         //
         return 0;
}


cheers
Andy
0
 

Author Comment

by:mgdPaul
Comment Utility
Emm? I don't see how that code relates to printing (as in, on paper) in VCL, sorry.
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 200 total points
Comment Utility
1. ::SetMapMode(hPrnDC, MM_ANISOTROPIC); //for example

2.
int nPrnWidth = ::GetDeviceCaps(hPrnDC, PHYSICALWIDTH); //Page width in
pixels
int nPrnHeight = ::GetDeviceCaps(hPrnDC, PHYSICALHEIGHT);//Page height in
pixels
int nPrnXDpI = ::GetDeviceCaps(hPrnDC, LOGPIXELSX);//Printer Pixels per Inch
for X axes
int nPrnYDpI = ::GetDeviceCaps(hPrnDC, LOGPIXELSY);//Printer Pixels per Inch
for Y axes

3.Remember, we have got two DCs, hScreenDC and hPrnDC.
And you can use all API for thous DC

4.
Plus you can use TPrinter PageHeight and PageWidth properties that return the width and
height of the page in inches multiplied by the printer's resolution,
respectively. For example, a 300dpi resolution printer using 8"x10" paper would
report a PageWidth of (8 x 300 = 2400) and a PageHeight of (10 x 300 = 3000).


0
 

Author Comment

by:mgdPaul
Comment Utility
Thanks!

I hope I can get my print function to work properly now, cause I've had problems with it for months now, and different problems every time (and the customer keeps wanting modifications) :(
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now