Board index » cppbuilder » How to change current printer?

How to change current printer?


2004-10-12 05:06:29 PM
cppbuilder18
Hi there,
I have tried the following code:
Printer()->PrinterIndex=printer->ItemIndex;
Printer()->BeginDoc();
Printer()->Canvas->TextOut(100,100, "This is a test");
Printer()->EndDoc();
However, I get "Printer Selected is Not Valid" exception. I have three
printer drivers and printer->ItemIndex is equal to 1.
Is it correct to change the printer driver by
"Printer()->PrinterIndex=printer->ItemIndex;"? Any other way to change the
current printer? Thank you in advance.
Enoch
 
 

Re:How to change current printer?

Tried with TPrintDialog already?
"MEMDB" < XXXX@XXXXX.COM >wrote in message
Quote
Hi there,

I have tried the following code:

Printer()->PrinterIndex=printer->ItemIndex;
Printer()->BeginDoc();
Printer()->Canvas->TextOut(100,100, "This is a test");
Printer()->EndDoc();

However, I get "Printer Selected is Not Valid" exception. I have three
printer drivers and printer->ItemIndex is equal to 1.

Is it correct to change the printer driver by
"Printer()->PrinterIndex=printer->ItemIndex;"? Any other way to change
the
current printer? Thank you in advance.

Enoch


 

Re:How to change current printer?

The TPrintDialog need to pop up a dialog.
I have tried some methods and the following method can work:
char *Buffer = new char[ 765 ];
unsigned int DeviceMode;
char *DeviceName = Buffer;
char *DriverName = Buffer + 256;
char *Port = Buffer + 512;
Printer()->PrinterIndex=printer->ItemIndex;
Printer()->GetPrinter( DeviceName, DriverName, Port, DeviceMode );
Printer()->SetPrinter( DeviceName, DriverName, Port, 0 );
Printer()->BeginDoc();
Printer()->Canvas->TextOut(100,100, "This is a test");
Printer()->EndDoc();
--
Enoch Ng
"Newsreader" < XXXX@XXXXX.COM >bl
Quote
Tried with TPrintDialog already?

"MEMDB" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>Hi there,
>
>I have tried the following code:
>
>Printer()->PrinterIndex=printer->ItemIndex;
>Printer()->BeginDoc();
>Printer()->Canvas->TextOut(100,100, "This is a test");
>Printer()->EndDoc();
>
>However, I get "Printer Selected is Not Valid" exception. I have three
>printer drivers and printer->ItemIndex is equal to 1.
>
>Is it correct to change the printer driver by
>"Printer()->PrinterIndex=printer->ItemIndex;"? Any other way to change
the
>current printer? Thank you in advance.
>
>Enoch
>
>


 

{smallsort}

Re:How to change current printer?

"MEMDB" < XXXX@XXXXX.COM >wrote:
Quote

[...] I get "Printer Selected is Not Valid" exception. [...]
Is it correct to change the printer driver by
"Printer()->PrinterIndex=printer->ItemIndex;"?
You didn't show what 'printer' was defined as.
Understand that a system can - and often does - have printers
installed that are no longer valid. Also understand that the
names of the printers are often confused with what windows
names them.
My solution to this problem has been to leave it to the user
to select the target printer. If they select an invalid
printer ... too bad - not my problem.
As for your particular problem, use the following code to
figure out what's happening:
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ComboBox1->Clear();
ComboBox1->Items->Assign( Printer()->Printers );
if( ComboBox1->Items->Count>0 ) ComboBox1->ItemIndex = 0;
}
//-------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage( "Printer Index = " +
IntToStr(ComboBox1->ItemIndex) + "\n" +
"Printer Name = " + ComboBox1->Text );
Printer()->PrinterIndex = ComboBox1->ItemIndex;
Printer()->BeginDoc();
Printer()->Canvas->TextOut(100,100, "This is a test");
Printer()->EndDoc();
}
//-------------------------------------------------------------
Note that if the TComboBox::ItemIndex is -1, the setter method
for TPrinter::PrinterIndex will set the printer index to what
ever the default printer is.
~ JD