## Print Preview and CBuilder

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

### Print Preview and CBuilder

Has anybody experience with PrintPreview and CBuilder 6? I was trying to print some picture but my experiments resulted only in exception .
Code: Select all
TPrintDialog *PrintDialog1=new TPrintDialog(this);PrintDialog1->MinPage=1;PrintDialog1->MaxPage=1;PrintDialog1->PrintRange= prAllPages;PrintPreview1->BeginDoc();PrintPreview1->Canvas->MoveTo(1,1);PrintPreview1->Canvas->LineTo(100,100);PrintPreview1->Canvas;PrintPreview1->EndDoc();delete PrintDialog1;

What´s wrong?

Thanks

Tom[/code]
tomas
Active Member

Posts: 8
Joined: November 10th, 2006, 11:33 pm
Location: Czech republic

Dunno, but your code looks a bit weird, you actually creating new printdialog, then repainting preview... why don't you use existing dialog then try to repaint everything? check the demo for correct method of printing.
Johnny_Bit
VIP Member

Posts: 455
Joined: June 15th, 2003, 9:56 am

### C++Builder and PrintPreview

I wrote it not correct, so:
Code: Select all
TPrintPreview *PrintPreview1=new TPrintPreview(this);...PrintPreview1->BeginDoc();PrintPreview1->Canvas->MoveTo(1,1);PrintPreview1->Canvas->LineTo(100,100);PrintPreview1->Canvas;PrintPreview1->EndDoc();  //results in exception...delete PrintPreview1;

I did not find any cpp example included . Could you write here a short example in C or send some link, please?? I don´t use Delphi...

Thanks
tomas
Active Member

Posts: 8
Joined: November 10th, 2006, 11:33 pm
Location: Czech republic

Hmm... that would be difficult since i work on linux for a long time, but i'll try.

First, don't bother dynamically create printpreview, drop it on for it would be much easier. So lets assume that and go on.

Code: Select all
...TRect R;...PrintPreview1->BeginDoc();R=PrintPreview1->PageBounds;PrintPreview1->Canvas->MoveTo(R.Top, R.Left);PrintPreview1->Canvas->LineTo(R.Top+100, R.Left+100);PrintPreview1->EndDoc();...

Besides, translating from c to delphi and vice-versa is quite simple... unless with records and objects, I don't even want to know that... records by dot, objects by arrow... damn.
Johnny_Bit
VIP Member

Posts: 455
Joined: June 15th, 2003, 9:56 am

### Re: C++Builder and PrintPreview

bayer wrote:I wrote it not correct, so:
Code: Select all
TPrintPreview *PrintPreview1=new TPrintPreview(this);...PrintPreview1->BeginDoc();PrintPreview1->Canvas->MoveTo(1,1);PrintPreview1->Canvas->LineTo(100,100);PrintPreview1->Canvas;PrintPreview1->EndDoc();  //results in exception...delete PrintPreview1;

I did not find any cpp example included . Could you write here a short example in C or send some link, please?? I don´t use Delphi...

Thanks

The reason you get an exception is that your newly created PrintPreview has no Parent control.

You should follow Johnny_Bit's code as the right code.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### CBuilder

Thanks, a new command was added:

PrintPreview1->Parent=Form1;

How could I display print properties (page orientation, scale, page numbers) on the form? Only canvas with drawing was displayed. It is also necessary to set working units, drawing looks very small ?
tomas
Active Member

Posts: 8
Joined: November 10th, 2006, 11:33 pm
Location: Czech republic

well... since you put it that way, you could use ConvertXY method from preview unit. it allows you to convert between any units. there is a code in demo, that can be easily translated to something like this:
Code: Select all
...TPoint conv;...conv=PrintPreviev1->ConvertXY(100, 100, mmLoMetric, PrintPreview1->Units); //100 in LoMetric=1cm...PrintPreview1->Canvas->LineTo(conv.X, conv.Y);

That would help.

To display Additional things, additional components were made, like TPaperPreview, TThumbnailPreview. Others are easily accessible from printing properties, and you should display them manually.
Johnny_Bit
VIP Member

Posts: 455
Joined: June 15th, 2003, 9:56 am

Thanks...

But threre is another problem. Preview was OK (component was resized to the whole window area), but no picture was printed.

...
PrintPreview1=new TPrintPreview(Form1);
PrintPreview1->Parent=Form1;
PrintPreview1->BeginDoc();
PrintPreview1->Canvas->MoveTo(0,0);
TPoint conv;
conv=PrintPreview1->ConvertXY(1000, 1000, mmLoMetric, PrintPreview1->Units); //100 in LoMetric=1cm
PrintPreview1->Canvas->LineTo(conv.x, conv.y);
PrintPreview1->Canvas;
PrintPreview1->PrintPages(1,2);
PrintPreview1->EndDoc();
delete PrintPreview1;
tomas
Active Member

Posts: 8
Joined: November 10th, 2006, 11:33 pm
Location: Czech republic

I wonder why you create the control manually?

Anyway you have to call PrintPages method after EndDoc.

Also, you can tell PrintPreview to do not create preview and instead print the pages directly.

Code: Select all
PrintPreview1->DirectPrint := True;PrintPreview1->BeginDoc();PrintPreview1->Canvas->MoveTo(0,0);TPoint conv;conv=PrintPreview1->ConvertXY(1000, 1000, mmLoMetric, PrintPreview1->Units); //100 in LoMetric=1cmPrintPreview1->Canvas->LineTo(conv.x, conv.y);PrintPreview1->EndDoc();

By the way, what the reason of the following line in your code?

Code: Select all
PrintPreview1->Canvas;
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

Thanks.

>>> I wonder why you create the control manually?
You think new/delete construction? I tried to install and add dowloaded data as a new component to CBuilder, but I was not successful .

Very nice component... I used QReport, but this component is much better. Is there any possibility to draw a StrinGrid with grid lines? I could print cells and then draw lines, but maybe there is a better way.
tomas
Active Member

Posts: 8
Joined: November 10th, 2006, 11:33 pm
Location: Czech republic

TPrintPreview offers low level printing functions, so you have to do everything by yourself.
Kambiz

Kambiz