C++Builder: Getting page width and height

Posted on 2001-08-09
Medium Priority
Last Modified: 2008-03-03

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,

Question by:mgdPaul
  • 2

Expert Comment

ID: 6370247
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
              cout << left;
         to set the alignment to the left/right
         cout << setw(20) << "Name:";
         cout << setw(60) << "Occupation:\n";
         /* change fill character from ' ' to '.' */
         cout << setw(20) << "*LuckY*";
         cout << setw(60) << "Student -- Computer Science\n";
         return 0;


Author Comment

ID: 6371607
Emm? I don't see how that code relates to printing (as in, on paper) in VCL, sorry.
LVL 14

Accepted Solution

AlexVirochovsky earned 800 total points
ID: 6371760
1. ::SetMapMode(hPrnDC, MM_ANISOTROPIC); //for example

int nPrnWidth = ::GetDeviceCaps(hPrnDC, PHYSICALWIDTH); //Page width in
int nPrnHeight = ::GetDeviceCaps(hPrnDC, PHYSICALHEIGHT);//Page height in
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

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).


Author Comment

ID: 6371782

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) :(

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

607 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