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);
-
}
-
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.
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
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.
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
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.
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 );
-
}
-
-
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?
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.
Hi Banfa,
Thanks for your continual support.
I would appreciate if you could indulge me with a little of your details.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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.*/...
|
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 ";
....
|
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.
...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
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...
| |