How to use TPrinter

I'm having problems with TPrinter

//code ....
#include <Printers.hpp>
.. . .
TBitmap* PrintScn  = new TBitmap ;
//add graphics to PrintScn
TPrinter *Printer1 = new TPrinter() ;
//PrintDialog stuff
TRect CRect(1, 1, PrintScn->Width, PrintScn->Height) ;
Printer1->Canvas->CopyRect(CRect, PrintScn->Canvas, CRect) ; //generates an 
AV
if(!Printer1->Canvas->HandleAllocated()) ShowMessage("Canvas has no Handle") 
; //is true,

//And I get the same with Printer()   ie

TRect CRect(1, 1, PrintScn->Width, PrintScn->Height) ;
Printer()->Canvas->CopyRect(CRect, PrintScn->Canvas, CRect) ; //generates an 
AV
if(!Printer()->Canvas->HandleAllocated()) ShowMessage("Canvas has no 
Handle") ; //is/true,
//end code

Is "TBitmap* PrintScn  = new TBitmap;"  ok to create a TPrinter object ?

The documentation example just calls Printer() repeatedly without a formal 
instantiation on the heap.
Does this generate just one TPrinter object ?

Why do I get an AV when CopyRect'ing PrintScn to Printer1 and to Printer() .
Is it cos the Canvas has no handle ?
Or
Is it something to do with my printer driver not supporting device dependent 
bit maps ?

John
0
John
8/11/2015 7:01:11 AM
embarcadero.cppbuilder.using 1848 articles. 1 followers. Follow

7 Replies
779 Views

Similar Articles

[PageSpeed] 28
Get it on Google Play
Get it on Apple App Store

John wrote:

> TPrinter *Printer1 = new TPrinter() ;

Do not create a new TPrinter object.  Use the Printer() function instead 
to obtain a TPrinter object that is owned by the RTL:

{code}
TPrinter *Printer1 = Printer();
{code}

> TRect CRect(1, 1, PrintScn->Width, PrintScn->Height) ;

Why 1 instead of 0?

> //generates an AV

Are you sure it is actally an AV and not some other exception?  What is the 
actual exception class type, and what does its Message actually say?

> Is "TBitmap* PrintScn  = new TBitmap;"  ok to create a TPrinter object ?

The two have nothing to do with each other.  But yes, you can create a TBitmap 
and draw it onto a TPrinter canvas.

> The documentation example just calls Printer() repeatedly without a
> formal instantiation on the heap.

There is a TPrinter object allocated on the heap.  It is allocated inside 
of Printer() the first time it is called, and then repeated calls return 
the same object.  The object is freed when the app is shutdown.

> Does this generate just one TPrinter object ?

Yes.

> Why do I get an AV when CopyRect'ing PrintScn to Printer1 and to
> Printer() .

Cant answer that without seeing the actual error you are seeing.

> Is it cos the Canvas has no handle ?

Hard to say.  What is the value of the TPrinter's PrinterIndex property when 
you get the error?

> Or Is it something to do with my printer driver not supporting device
> dependent bit maps ?

Should not be

-- 
Remy Lebeau (TeamB)
0
Remy
8/11/2015 5:37:59 PM
Remy wrote -
> Do not create a new TPrinter object.  Use the Printer() function instead
> to obtain a TPrinter object that is owned by the RTL:
>
> {code}
> TPrinter *Printer1 = Printer();
> {code}

Noted .

>> TRect CRect(1, 1, PrintScn->Width, PrintScn->Height) ;
> Remy asked  Why 1 instead of 0?

No particular reason !

> //generates an AV
>
> Are you sure it is actally an AV and not some other exception?  What is 
> the
> actual exception class type, and what does its Message actually say?

 TPrinter *Printer1 = Printer() ;
(code)
TRect CRect(1, 1, PrintScn->Width, PrintScn->Height) ;
(code)
try
{
Printer1->Canvas->CopyRect(CRect, Form1->PrintScn->Canvas, CRect) ;
 //  Printer1->Canvas->Draw(1,1, Form1->PrintScn) ;  //generates same AV
 }
catch(Exception &EPrinter)
 {
 unsigned int Error = GetLastError() ;
 msg = "Exception when copying PrintScn to the Printer canvas. Msg is \"" ; 
msg += EPrinter.Message ; msg += "\"" ;
 msg += ".  Printer->PrinterIndex = " ;  msg += Printer1->PrinterIndex ;
 if(Printer1->Canvas->HandleAllocated()) msg += ". No canvas handle" ;
 ShowMessage(msg.c_str()) ;
 }
catch(...)
 {
 ShowMessage("Caught unspecified exception") ;
 }

The above generates :-
Exception when copying PrintScn to the Printer canvas. Msg is "Printer is 
not currently printing".  Printer->PrinterIndex = 0

I have 3 printer devices.  They generate PrinterIndex'es of 0, 1 and 2.

John commented
>> The documentation example just calls Printer() repeatedly without a 
>> formal instantiation on the heap.
Remy replied
> There is a TPrinter object allocated on the heap.  It is allocated inside
> of Printer() the first time it is called, and then repeated calls return
> the same object.  The object is freed when the app is shutdown.

Pity this is not stated as clearly and simply in the Help documentation !

John
0
John
8/12/2015 1:11:30 PM
John wrote:

> catch(Exception &EPrinter)
> {
> unsigned int Error = GetLastError() ;

GetLastError() is not guaranteed to work inside of an exception handler like 
that.  It is only meaningful when called *immediately* after a failed API 
call.  Once an exception is thrown, the act of creating the exception and 
throwing it can reset the error code that GetLastError() returns.

> The above generates :-
> Exception when copying PrintScn to the Printer canvas. Msg is "Printer
> is not currently printing".

That is not an AV (AccessViolation when trying to accessing invalid memory). 
 That is a standard TPrinter error.  It means you did not call TPrinter::BeginDoc() 
to start a new print job before sending data to the printer Canvas, eg:

{code}
TPrinter *Printer1 = Printer() ;
Printer1->BeginDoc(); // <-- add this!
// use Printer1->Canvas as needed...
Printer1->EndDoc(); // <-- add this!
{code}

This is clearly stated in the TPrinter documentation.

I would even go as far as doing this:

{code}
TPrinter *Printer1 = Printer() ;
Printer1->BeginDoc(); // <-- add this!
try
{
    // use Printer1->Canvas as needed...
    Printer1->EndDoc(); // <-- add this!
}
catch (const Exception &)
{
    Printer1->Abort(); // <-- add this!
    throw;
}
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
8/12/2015 4:42:50 PM
Remy

Thanx for the help.
You are quite right.  I put BeginDoc AFTER attempting to write to the 
Prrinter's Canvas.
Putting it BEFORE is the fix and now printing is working (at least to MS's 
XPS printer, can't
test with laser at this moment cos of driver problems which I'll fix in 
morning, its past midnite
here !)

I'll incorporate your suggestions & read up on GetLastError.

If you're stating the documentation clearly states BeginDoc should precede 
writing to the
Printer's Canvas I'll disagree.  The crucial sentences read  "A *print* job 
is started by a
call to BeginDoc. The application sends commands by rendering through a Text 
variable
or the printer's canvas".  -   the stars are mine

Best  John
0
John
8/13/2015 12:03:56 AM
John wrote:

> If you're stating the documentation clearly states BeginDoc should
> precede writing to the Printer's Canvas I'll disagree.

http://docwiki.embarcadero.com/RADStudio/XE8/en/Printing

{quote}
To get a list of installed and available printers, use the Printers property. 
An instance of the class TPrinter uses a TCanvas (which is identical to the 
form's TCanvas). This means that anything that can be drawn on a form can 
be printed as well. To print an image, **call the BeginDoc method followed 
by whatever canvas graphics you want to print (including text through the 
TextOut method) and send the job to the printer by calling the EndDoc method**.
{quote}

http://docwiki.embarcadero.com/Libraries/XE8/en/Vcl.Printers.TPrinter

{quote}
A print job is started by a call to Vcl.Printers.TPrinter.BeginDoc. The application 
sends commands by rendering through a Text variable or the printer's canvas. 
You can move to a new page by calling the NewPage method. The job stays open 
until the application calls EndDoc. If a problem occurs and you need to terminate 
a print job that was not sent to the printer successfully, call the Abort 
method. 
{quote}

-- 
Remy Lebeau (TeamB)
0
Remy
8/13/2015 12:32:53 AM
> 
> Best  John

You might look at the web page below. It has some very good components for C++Builder printing. They now have sample projects through XE7. This worked for me a few years ago. 

Printing Utilities:
code4sale.com/tryitbuyit/index.htm

TExcellentImagePrinter

TExcellentFormPrinter
0
Larry
8/13/2015 12:57:09 AM
Remy

Ok the DocWiki is clear. . .
"To print an image, call the BeginDoc method *followed* by whatever canvas 
graphics you want to print"
Hitherto I've just referred to the docus shipped with CB2007/XE2 . 
Moral - use DocWiki. .

Larry

I've noted your references. .

Best  John
0
John
8/13/2015 11:37:50 PM
Reply:

Similar Artilces:

Using Using
I have just completed reading a chapter in my book on performance. The books says to speed up performance on your web site you should use the using statement when opening connections. My question is how do you catch errors if you are using this as apposed to a try catch block.thanksBryan  Why would you use it as an alternative to a try/catch statement. Couldn't you just do something like  public void UsingSomeMethod() { try { SomeMethod(); } catch(SomeException e) { /// do something useful }}public void SomeMethod() { using (Something) { ...

When to use ( and when to use ((?
I'm just not getting it. When do you use ( in a statement, when do you need to use ((, and is the space after either or both mandatory? For example, I coded this statement: if (( $_ eq $bad_guys_ip )) { more_stuff_here }; Why the two (( in an if? And why does it seem to not work when I miss the space after the ((? Thanx! Mark me as... /Corn-fused|Dense/ > I'm just not getting it. >=20 > When do you use ( in a statement, when do you need to use ((, and is > the space after either or both mandatory? >=20 > For example, I coded this sta...

How to use the arguments to use() in the package being used
Howdy,=20 The subject says it all believe it or not :) What I'm trying to figure out is how to pass an argument=20 (pragma I believe is the proper term) to use() and do=20 sonethign in the package based on it. I've looked at CGI.pm source but can't seem to track it down.=20 (Similar idea as to CGIs -oldstyle_urls -newstyel_urls) http://search.cpan.org/~lds/CGI.pm-3.04/CGI.pm#PRAGMAS What I'd like to do is something like this: # for old time's sake we'll just use our favorite module use Foo::Monkey qw(:Foo :Bar -doamazingthings); #then in Foo::Monkey...

why to use 1)use CGI,2)use Strict ,3)use Carp in script
hi why we r using 1)use CGI, 2)use Strict, 3)use Carp in the begining of script sometimes. What is the use of it.Is the files CGI.pm,Strict.pm,Carp.pm default with PERL. Thnx __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com On Thu, 2004-11-04 at 09:31, supriya devburman wrote: > hi Hi > why we r using > 1)use CGI, We only use CGI when we want to use methods from that module (usually on web pages); see `perldoc CGI` for more information on what you...

When to use/not use ViewState
I'm creating a client app and I'm wondering is it neccessary to enable viewstate for "All" the fields. When should you use viewstate and when should you "NOT" use viewstate. Thanks.Daniel It depends on your requirements. if you dont need to preserve the contents of inputboxes past the initial post then dont turn on viewstate for them. you could take the approach of turning off viewstate for everything, and then turn it back on item by item as you find you need it. It shouldnt just be enabled for everything as it does make the page heavier - especially with ...

DSRepair
Any good information on when to use Dsrepair which settings to use ? Is this something that should be run on a monthly /weekly basis and which of the million settings should be turned on:o) Use DSREPAIR to fix a broken database - that is what it is intended to be used for. It is not intended to be used as a "regular maintenance" tool. Using it in this way can in fact hide more serious issues by removing symptoms that indicate a more serious problem. Use it as a last resort for trying to identify problems with the database. Run health checks weekly or monthly (...

this keyword- to use or not to use???
While reading some articles I came across this sample code which uses the "this" keyword. Is this best practice?         this.sqldsCustomerDetails.SelectParameters.Clear();        this.sqldsCustomerDetails.SelectParameters.Add("customerid", Convert.ToString(this.gvCustomers.DataKeys[row.RowIndex].Value));        this.dvCustomerDetail.DataSource = this.sqldsCustomerDetails;        this.dvCustomerDetail.DataBind(); In my code I usual...

To use or not to use Delphi
Sadly, it seems to me that there is a sort of race between the two threads, for and against using Delphi in new projects, with more or less the same users posting in both threads. Arguments are fiercely debated in both camps. Borland had their own vision. As a community, now that Delphi has changed ownership I believe we should try to be more consistent, more clear, and more articulate in what we expect from Embercadero in terms of Delphi. We can contribute to keeping Delphi alive and moving in the right direction. "Laurent Cocea" schrieb: > Sadly, it seems to me that there ...

How to use net use?
Please can any one tell me how to use the dos command net use properly? I have read the help but can't seem to get it to work. Keep getting sytax error message. Thanks in advance. "Beowulf" <who-knows@no-one.com> wrote in message news:9krutk$puu$1@news.grc.com... > Please can any one tell me how to use the dos command net use properly? > > I have read the help but can't seem to get it to work. Keep getting sytax > error message. what are you trying to do? There are about 50 options to "net use" that do a ton of different thin...

I used to use Firefox until...
Name: Dan Product: Firefox Summary: I used to use Firefox until... Comments: Firefox takes forever to load now, even the absolute newest version. It has become clunky and everyone I know has moved on to another browser. I prefer Chrome. I suggest adding some sort of configuration optimization tool as well as toolbar/add-on install/hijack prevention. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a re...

CodeSmith
Of course it is a matter of preference, but personally I prefer to code our own template and dislike tools like this, like to do a quick poll here to know if many uses it, like to get your feedback on: (1) Do you use CodeSmith? (2) Why yes and why not? Thanks       No, prefer learning on my own.Rod H  >(1) Do you use CodeSmith?No >(2) Why yes and why not?Never had the opportunity!I incidentally use ReSharper from Jetbrains, that helps tremendously - it is like intellisence on steroids. Don't forget to click "...

To use DNN or not to use?
Hello,I am developing a community web based appication for non-profits and social service agencies in our county, that will be used to provide and track client services in a single datastore.Some built in features that dnn has is what I need, but I need custom tree navigation, list views, edit on the fly, several customized layers for data access, etc.I am familiar with asp.net, but not very familiar with dnn.  I installed  dnn on a friends website but never had time to configure and learn more about it.What are the general rules when deciding when to use dnn and when not...

FreeAndNil to use or not to use
After reading these debates about FreeAndNil I separated the people into two categories: The first one says: "You should have a perfect design which prohibits errors from happening. So, if you have a perfect design then FreeAndNil is not necessary so you should not use it. If we see FreeAndNil in your code then it means that your design is not perfect." The second one says: "Yes, we agree that we should have a perfect design which prohibits errors from happening and we do our best in order to create a perfect design. However our practice says that it is better ...

Why using use strict;
Hi all, I know why I should use "use strict;" but what happen if I use "use strict;" then if the code is OK, I delete this line? Shouldn't it work the same without this line if the code has no problems? Thanks. Teddy, orasnita@home.ro why would you want to do that Octavian? "use strict" is a sanity safety net. What your saying is like "I know why I should use safety belt whilst driving my car but what if I took it off?" - of course you can do it but beware of the consequences. joel -----Original Message----- From: ...

To use signatures or not to use?
--001a11c3871a8a8fe2051b7b8b18 Content-Type: text/plain; charset=UTF-8 Dear all, Perl now has subroutine signatures. However, they are marked as experimental feature. As I really like it, I'm always tempted to use it. On the other hand, I don't want to wast efforts to something I have to roll back. So, does anyone know the tendency if signatures are going to stay? I'm not looking for the standard "do if you want to do it because TIMTOWTDI" answer, it will not help me. I'm looking for rumors, or eventually an assessment from the people who did the fe...

Web resources about - How to use TPrinter - embarcadero.cppbuilder.using

Resources last updated: 12/14/2015 4:22:49 AM