473,511 Members | 14,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I use print handler to pring a file?

9 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. private: System::Void printToolStripMenuItem_Click(System::Object ^  sender, System::EventArgs ^  e)
  2.  {
  3.  // The PrintDocument holds the settings
  4.  PrintDocument^ pdoc = gcnew PrintDocument();
  5.  
  6. // Create a dialog and attach it to the document
  7. PrintDialog^ pd = gcnew PrintDialog();
  8.  
  9. pd->Document = pdoc;
  10.  
  11. // Show the dialog
  12. if (pd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
  13.  
  14. {
  15. // Add the page handler
  16.  
  17. pdoc->PrintPage += gcnew PrintPageEventHandler(this,&Form1::PrintAPage);
  18. // Print the page
  19. pdoc->Print();
  20.  
  21. }
  22. else
  23.  MessageBox::Show("Print cancelled", "Information");
  24. }
  25.  
  26. void PrintAPage(Object^ pSender, PrintPageEventArgs^ pe)
  27. {
  28.  
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Graphics* gr = pe->Graphics;
  3.      Pen* pen1 = new Pen(Color::Black);
  4.  
  5.      // Draw the image
  6.      Bitmap* bmp = new Bitmap(S"ramp1.gif");
  7.      gr->DrawImage(bmp, 10,10);
  8.  
  9.      for(int i=0; i<list->Count; i++)
  10.      {
  11.       Line* pl = dynamic_cast<Line*>(list->get_Item(i));
  12.       gr->DrawLine(pen1, pl->p1.X,pl->p1.Y, pl->p2.X,pl->p2.Y);
  13.      }
  14.  
Oct 3 '09 #1
9 3107
Banfa
9,065 Recognized Expert Moderator Expert
You will need to use something like the File class from the System.Io name space to load the file.

Then you can use gr->DrawString(...) to display the loaded text.
Oct 4 '09 #2
Autostrad
9 New Member
Hi Banfa,

I appreciate your help.

I did the following and Ihave the following error.

FileStream^ fs = gcnew FileStream("ReadMe.txt", FileMode::Open);
Graphics^ gr;
gr->DrawString();

error C2661: 'System::Drawing::Graphics::DrawString' : no overloaded function takes 0 arguments
Oct 4 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
That is because you tried to call DrawString with no parameters for which no function exists. You have to say where and what to draw using what font.

I suggest you read the documentation on DrawString, you will find it at the end of the link in my previous post.
Oct 5 '09 #4
Autostrad
9 New Member
I went there already and the instruction wants me to show something about brush and other things. I do not want to forget to let you know that I do not want to draw anything. The book has a program to print what you draw and I did it very well. But the problem is; I do not want to draw or even print a graphic file. All I want to do is to print a simple text file. The thing that frustrated me is I can do the hard thing by following the book, But cannot modify it to make it simpler enough to just print a simple text file from the already saved file.

I really thank you a lot for trying to help
Oct 5 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
You will need to have a brush and a font to draw your text because you may think you do not want to draw anything, just print next but in fact the way you print text is to draw it using a brush and font.

I am now moving this to the .NET forum to see if anyone there has a better idea.
Oct 5 '09 #6
Autostrad
9 New Member
Thanks everybody. I solved like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Visual C++ Copy Code 
  3. #using <System.dll>
  4. #using <System.Windows.Forms.dll>
  5. #using <System.Drawing.dll>
  6.  
  7. using namespace System;
  8. using namespace System::IO;
  9. using namespace System::Drawing;
  10. using namespace System::Drawing::Printing;
  11. using namespace System::Windows::Forms;
  12.  
  13. public ref class PrintingExample: public System::
  14. Windows::Forms::Form
  15. {
  16. private:
  17.    System::ComponentModel::Container^ components;
  18.    System::Windows::Forms::Button^ printButton;
  19.    System::Drawing::Font^ printFont;
  20.    StreamReader^ streamToPrint;
  21.  
  22. public:
  23.    PrintingExample()
  24.       : Form()
  25.    {
  26.  
  27.       // The Windows Forms Designer requires the following call.
  28.       InitializeComponent();
  29.    }
  30.  
  31.  
  32. private:
  33.  
  34.    // The Click event is raised when the user clicks the Print button.
  35.    void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
  36.    {
  37.       try
  38.       {
  39.          streamToPrint = gcnew StreamReader( "C:\\My Documents\\MyFile.txt" );
  40.          try
  41.          {
  42.             printFont = gcnew System::Drawing::Font( "Arial",10 );
  43.             PrintDocument^ pd = gcnew PrintDocument;
  44.             pd->PrintPage += gcnew PrintPageEventHandler( this, &PrintingExample::pd_PrintPage );
  45.             pd->Print();
  46.          }
  47.          finally
  48.          {
  49.             streamToPrint->Close();
  50.          }
  51.  
  52.       }
  53.       catch ( Exception^ ex ) 
  54.       {
  55.          MessageBox::Show( ex->Message );
  56.       }
  57.  
  58.    }
  59.  
  60.  
  61.    // The PrintPage event is raised for each page to be printed.
  62.    void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
  63.    {
  64.       float linesPerPage = 0;
  65.       float yPos = 0;
  66.       int count = 0;
  67.       float leftMargin = (float)ev->MarginBounds.Left;
  68.       float topMargin = (float)ev->MarginBounds.Top;
  69.       String^ line = nullptr;
  70.  
  71.       // Calculate the number of lines per page.
  72.       linesPerPage = ev->MarginBounds.Height / printFont->GetHeight( ev->Graphics );
  73.  
  74.       // Print each line of the file.
  75.       while ( count < linesPerPage && ((line = streamToPrint->
  76. ReadLine()) != nullptr) )
  77.       {
  78.          yPos = topMargin + (count * printFont->GetHeight
  79. ( ev->Graphics ));
  80.          ev->Graphics->DrawString( line, printFont, Brushes::
  81. Black, leftMargin, yPos, gcnew StringFormat );
  82.          count++;
  83.       }
  84.  
  85.  
  86.       // If more lines exist, print another page.
  87.       if ( line != nullptr )
  88.             ev->HasMorePages = true;
  89.       else
  90.             ev->HasMorePages = false;
  91.    }
  92.  
  93.  
  94.    // The Windows Forms Designer requires the following procedure.
  95.    void InitializeComponent()
  96.    {
  97.       this->components = gcnew System::ComponentModel::Container;
  98.       this->printButton = gcnew System::Windows::Forms::Button;
  99.       this->ClientSize = System::Drawing::Size( 504, 381 );
  100.       this->Text = "Print Example";
  101.       printButton->ImageAlign = System::Drawing::ContentAlignment::
  102. MiddleLeft;
  103.       printButton->Location = System::Drawing::Point( 32, 110 );
  104.       printButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
  105.       printButton->TabIndex = 0;
  106.       printButton->Text = "Print the file.";
  107.       printButton->Size = System::Drawing::Size( 136, 40 );
  108.       printButton->Click += gcnew System::EventHandler( this, &PrintingExample::printButton_Click );
  109.       this->Controls->Add( printButton );
  110.    }
  111.  
  112. };
  113.  
  114.  
  115. // This is the main entry point for the application.
  116. int main()
  117. {
  118.    Application::Run( gcnew PrintingExample );
  119. }
  120.  
  121.  
Oct 7 '09 #7
Autostrad
9 New Member
From the code mentioned; I was thinking the printing problem is solved as soon as the printer started to print. Unfortunately, although it worked well, but I found that the printer will print without writing the empty space. For example if the file to be printed has the following line:

1 2 3

It will print it as:
123.

Can anybody tell me how to fix that?
Oct 10 '09 #8
Banfa
9,065 Recognized Expert Moderator Expert
Counts line your variable count is being incremented for blank lines. That being the case you may want to check the documentation for streamToPrint->ReadLine() and see if it returns blank lines to the program.
Oct 11 '09 #9
Autostrad
9 New Member
Hi Banfa,

Thanks for your continual support.
I would appreciate if you could indulge me with a little of your details.
Oct 11 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
22147
by: Paul Watson | last post by:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstanding this feature? How can I use print and not have a newline appended at the...
1
4189
by: Steve Leferve | last post by:
Hey folks -- For some reason, debug.print is not placing any text in my locals window. I'm creating some SQL statements, and I usually like to throw them in the window so I can see what they...
3
3507
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
5
4897
by: zl2k | last post by:
hi, all I am trying to moniter the runing of a program by printing out some characters in a line at each stage. say, //program part1 cout<<"part1 "; //program part2 cout<<part2 "; ....
43
7538
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
6
2815
by: =?Utf-8?B?cHJhZGVlcF9UUA==?= | last post by:
I am trying to create a simple HTTP handler in ASP.net 2.0. I am using VS 2005. I am trying to handle a custom extension file givein in the URL. I have also created the following entry in the...
3
2406
by: itdaddy | last post by:
hey perl gurus! i am new to this forum cause i need help. I have done many scripts. but i want to use perl to do this: What I want to do is this. I have a QRP file that I can convert to a txt...
3
2533
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
16
4500
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.