Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

C++Builder: Getting page width and height

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
mgdPaul
Asked:
mgdPaul
  • 2
1 Solution
 
andysalihCommented:
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
 
mgdPaulAuthor Commented:
Emm? I don't see how that code relates to printing (as in, on paper) in VCL, sorry.
0
 
AlexVirochovskyCommented:
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
 
mgdPaulAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now