473,569 Members | 2,789 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 3119
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("Rea dMe.txt", FileMode::Open) ;
Graphics^ gr;
gr->DrawString() ;

error C2661: 'System::Drawin g::Graphics::Dr awString' : 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
22160
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 end? C:\src\projects\test1>python -c "import sys;print sys.version, 'running on', sys.platform" 2.3.4 (#53, May 25 2004, 21:17:02) running on...
1
4209
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 looked like before execution, try them as queries, etc. Can someone shed some light on this subject? Steve
3
3510
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.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is...
5
4901
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
7542
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. How is it possible? Please help me. Thanks in advance.
6
2821
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 web.config file <httpHandlers> <add verb="*" path="*.imgw" type="Customhandler.Handler,Handler" /> </httpHandlers> following is the code in...
3
2414
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 file field separated by commas or not. I want to pring each line that has an actual date to the right of the word 'DATE:' if it doesnt have a date do...
3
2540
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 program, but I do not know how I would find the default handler to a file type. Any help is greatly appreciated. Code to launch application:...
16
4509
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 screen, when I open a file, the entire contents of the file is in fact being shown...so why can't I print it later? All of this code I am getting from...
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.