Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2009, 05:13 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
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);
     }

 
Old October 6th, 2009, 09:54 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
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
 
Old January 15th, 2010, 01:32 PM
Authorized User
 
Join Date: Jan 2010
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
Red face question for you?

I can see you've been doing this for some time. could you tell me how you got the code in the scroll boxes? I would be most appreciative.
 
Old January 15th, 2010, 02:16 PM
jminatel's Avatar
Wrox Staff
Points: 18,059, Level: 58
Points: 18,059, Level: 58 Points: 18,059, Level: 58 Points: 18,059, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2003
Posts: 1,906
Thanks: 62
Thanked 139 Times in 101 Posts
Default

Use the "code" tag. :)
Please see the FAQ: http://p2p.wrox.com/faq.php
__________________
Jim Minatel
Associate Publisher, WROX - A Wiley Brand
Did someone here help you? Click on their post!
The Following User Says Thank You to jminatel For This Useful Post:
abilify (January 15th, 2010)
 
Old January 15th, 2010, 02:48 PM
Authorized User
 
Join Date: Jan 2010
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
Default like this, maybe we'll see

Code:
<script type="text/javascript">
<!--
    alert("Thanks for your help!");
//-->
</script>
 
Old January 16th, 2010, 06:16 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is a question there, which is not directed to anyone. In case it was for me. I got the code from many participants just like you and me from different forums.





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 04: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 09:00 AM
Getting info about file type handler app ejan C# 2 May 28th, 2006 08:15 AM
Print to File furjaw Visual Basic 2005 Basics 0 May 2nd, 2006 12:29 AM
Print and print preview file on the website withou appleLover General .NET 0 February 19th, 2005 02:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.