|
|
 |
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
 | This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

October 3rd, 2009, 06:13 PM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How can I use print handler to pring a file?
Hi everybody,
I need your help.
The code below is good if I want to print a sketch. The book did not show how to print a simple text file. Right now if I run this program (as is)it will show the printer dialog box. If I click OK (to print), the printer will print an empty paper. How can I use this code to print a file?
I would apprciate it if you could help.
Code:
private: System::Void printToolStripMenuItem_Click(System::Object ^ sender, System::EventArgs ^ e)
{
// The PrintDocument holds the settings
PrintDocument^ pdoc = gcnew PrintDocument();
// Create a dialog and attach it to the document
PrintDialog^ pd = gcnew PrintDialog();
pd->Document = pdoc;
// Show the dialog
if (pd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
// Add the page handler
pdoc->PrintPage += gcnew PrintPageEventHandler(this,&Form1::PrintAPage);
// Print the page
pdoc->Print();
}
else
MessageBox::Show("Print cancelled", "Information");
}
void PrintAPage(Object^ pSender, PrintPageEventArgs^ pe)
{
}
If I put the following code inside the empty function (PrintAPage)I will be ablble to draw and print what I draw. But what I need is to print an already written and saved simple text File.
Code:
Graphics* gr = pe->Graphics;
Pen* pen1 = new Pen(Color::Black);
// Draw the image
Bitmap* bmp = new Bitmap(S"ramp1.gif");
gr->DrawImage(bmp, 10,10);
for(int i=0; i<list->Count; i++)
{
Line* pl = dynamic_cast<Line*>(list->get_Item(i));
gr->DrawLine(pen1, pl->p1.X,pl->p1.Y, pl->p2.X,pl->p2.Y);
}
__________________
Thanks
Allen
|

October 6th, 2009, 10:54 PM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I solved the problem. In case some one faces the same problem the solution is:
Code:
//Visual C++ Copy Code
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::IO;
using namespace System::Drawing;
using namespace System::Drawing::Printing;
using namespace System::Windows::Forms;
public ref class PrintingExample: public System::Windows::Forms::Form
{
private:
System::ComponentModel::Container^ components;
System::Windows::Forms::Button^ printButton;
System::Drawing::Font^ printFont;
StreamReader^ streamToPrint;
public:
PrintingExample()
: Form()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
private:
// The Click event is raised when the user clicks the Print button.
void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
try
{
streamToPrint = gcnew StreamReader( "C:\\My Documents\\MyFile.txt" );
try
{
printFont = gcnew System::Drawing::Font( "Arial",10 );
PrintDocument^ pd = gcnew PrintDocument;
pd->PrintPage += gcnew PrintPageEventHandler( this, &PrintingExample::pd_PrintPage );
pd->Print();
}
finally
{
streamToPrint->Close();
}
}
catch ( Exception^ ex )
{
MessageBox::Show( ex->Message );
}
}
// The PrintPage event is raised for each page to be printed.
void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = (float)ev->MarginBounds.Left;
float topMargin = (float)ev->MarginBounds.Top;
String^ line = nullptr;
// Calculate the number of lines per page.
linesPerPage = ev->MarginBounds.Height / printFont->GetHeight( ev->Graphics );
// Print each line of the file.
while ( count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr) )
{
yPos = topMargin + (count * printFont->GetHeight( ev->Graphics ));
ev->Graphics->DrawString( line, printFont, Brushes::Black, leftMargin, yPos, gcnew StringFormat );
count++;
}
// If more lines exist, print another page.
if ( line != nullptr )
ev->HasMorePages = true;
else
ev->HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
void InitializeComponent()
{
this->components = gcnew System::ComponentModel::Container;
this->printButton = gcnew System::Windows::Forms::Button;
this->ClientSize = System::Drawing::Size( 504, 381 );
this->Text = "Print Example";
printButton->ImageAlign = System::Drawing::ContentAlignment::MiddleLeft;
printButton->Location = System::Drawing::Point( 32, 110 );
printButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
printButton->TabIndex = 0;
printButton->Text = "Print the file.";
printButton->Size = System::Drawing::Size( 136, 40 );
printButton->Click += gcnew System::EventHandler( this, &PrintingExample::printButton_Click );
this->Controls->Add( printButton );
}
};
// This is the main entry point for the application.
int main()
{
Application::Run( gcnew PrintingExample );
}
This code is ready to compile
Thanks
__________________
Thanks
Allen
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |