Solved

C++Builder: Getting page width and height

Posted on 2001-08-09
4
453 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
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
         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
ID: 6371607
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
ID: 6371760
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
ID: 6371782
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

26 Experts available now in Live!

Get 1:1 Help Now