How can I use print handler to pring a file? | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| |
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. -
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. -
-
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);
-
}
-
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: How can I use print handler to pring a file?
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.
| | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| | | re: How can I use print handler to pring a file?
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
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: How can I use print handler to pring a file?
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.
| | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| | | re: How can I use print handler to pring a file?
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
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: How can I use print handler to pring a file?
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.
| | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| | | re: How can I use print handler to pring a file?
Thanks everybody. I solved like this: -
-
//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 );
-
}
-
-
| | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| | | re: How can I use print handler to pring a file?
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?
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,148
| | | re: How can I use print handler to pring a file?
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.
| | Newbie | | Join Date: Oct 2009 Location: USA
Posts: 7
| | | re: How can I use print handler to pring a file?
Hi Banfa,
Thanks for your continual support.
I would appreciate if you could indulge me with a little of your details.
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|