I have written a Print Preview option for a larger application. Mostly it works to my satisfaction, except sometimes the image is truncated.
The code below is a much cut-down version that shows the problem. I have a TPanel on which I am putting a TImage, which for now is just a white rectangle with a red line going from the top left corner to the bottom right.
In the larger application, I want to choose between A4 portrait size and A4 landscape. My little app here has two buttons to choose which page size is required.
Here's the problem. When I start the app and select Portrait, it works correctly:
But, when I then click Landscape, the image is truncated. You can see which way the red line is going:
On the other hand, when I stop the app and restart it and select Landscape, it works ok:
When I then click Portrait, it is this image that is now truncated:
How can I get this to work?
My code is below:
const float A4width = 8.27/4, A4height = 11.69/4;
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
image = new TImage(Owner);
image->Parent = Panelpreview;
image->Left = 16;
image->Top = 16;
}
// Portrait button click
void __fastcall TForm1::BportraitClick(TObject *Sender)
{
fheight = A4height;
fwidth = A4width;
showPreview();
}
//Landscape button click
void __fastcall TForm1::BlandscapeClick(TObject *Sender)
{
fheight = A4width;
fwidth = A4height;
showPreview();
}
// Draw the picture
void TForm1::showPreview()
{
int pagewidth, pageheight;
TRect imagerect;
pagewidth = fwidth * Canvas->Font->PixelsPerInch;
pageheight = fheight * Canvas->Font->PixelsPerInch;
imagerect = TRect(0, 0, pagewidth, pageheight);
image->Width = pagewidth;
image->Height = pageheight;
image->Canvas->Brush->Color = clWhite;
image->Canvas->FillRect(imagerect);
image->Canvas->Pen->Color = clRed;
image->Canvas->MoveTo(0, 0);
image->Canvas->LineTo(pagewidth, pageheight);
}




main()function. You could obviously reduce this down to something that we could compile and that would just bemain()calling a few functions (if you couldn't manage to inline them) - there shouldn't be anything else. But, at the very least, we should be able to compile what you posted.