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 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.
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
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 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...
|
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
|
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...
|
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.
How is it possible? Please help me.
Thanks in advance.
| |
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
| |