p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 3rd, 2009, 06:13 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old October 6th, 2009, 10:54 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Handler to read and load a PDF file Mehdi6002 ASP.NET 2.0 Professional 0 May 8th, 2007 05:39 PM
Handler to load an Image file -PDF Mehdi6002 BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 May 8th, 2007 10:00 AM
Getting info about file type handler app ejan C# 2 May 28th, 2006 09:15 AM
Print to File furjaw Visual Basic 2005 Basics 0 May 2nd, 2006 01:29 AM
Print and print preview file on the website withou appleLover General .NET 0 February 19th, 2005 02:24 AM



All times are GMT -4. The time now is 05:19 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc