PC Review


Reply
Thread Tools Rate Thread

How can I print a file

 
 
Allen
Guest
Posts: n/a
 
      3rd Oct 2009
Hi everybody,

I need your help.

The code below came from a book which teaches how to print a sketch. The
book did not show how to print a 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
("ReadMe.txt")?

I would appreciate 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:ialogResult::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 text.

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);
}



--
Thanks
Allen

 
Reply With Quote
 
 
 
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      3rd Oct 2009
Hi Allen,

You need to do something similar as you do with the image file. Load it
into memory and draw it onto the PrintPage.

To draw the text you can use

string text = File.ReadAllText("ReadMe.txt");
gr.DrawString(text, new Font("Arial", 10), Brushes.Black, 0, 0);

--
Happy Coding!
Morten Wennevik [C# MVP]


"Allen" wrote:

> Hi everybody,
>
> I need your help.
>
> The code below came from a book which teaches how to print a sketch. The
> book did not show how to print a 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
> ("ReadMe.txt")?
>
> I would appreciate 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:ialogResult::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 text.
>
> 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);
> }
>
>
>
> --
> Thanks
> Allen
>

 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      6th Oct 2009
Hi Morten,
I got the error (1) below. Then, I did a little adjustment and then got
error (2) below. Thanks any way:


(1)
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(248) : warning C4832: token
'.' is illegal after UDT 'System::IO::File'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::IO::File'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(248) : error C2275:
'System::IO::File' : illegal use of this type as an expression
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::IO::File'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2065: 'gr' :
undeclared identifier
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2228: left of
'.DrawString' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2248:
'System::Windows::Forms::Control::text' : cannot access private member
declared in class 'System::Windows::Forms::Control'
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\AboutBox.h(23) : see declaration of
'System::Windows::Forms::Control::text'
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2061: syntax
error : identifier 'Font'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : warning C4832: token
'.' is illegal after UDT 'System:rawing::Brushes'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.dll :
see declaration of 'System:rawing::Brushes'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2143: syntax
error : missing ';' before ')'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2143: syntax
error : missing ';' before ')'
1>Build log was saved at "file://c:\Users\Allen\Documents\Visual Studio
2008\Projects\TimeTracking\TimeTracking\Debug\BuildLog.htm"
1>TimeTracking - 10 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

(2)

rted: Project: TimeTracking, Configuration: Debug Win32 ------
1>Compiling...
1>TimeTracking.cpp
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2228: left of
'.DrawString' must have class/struct/union
1> type is 'System:rawing::Graphics ^'
1> did you intend to use '->' instead?
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2248:
'System::Windows::Forms::Control::text' : cannot access private member
declared in class 'System::Windows::Forms::Control'
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\AboutBox.h(23) : see declaration of
'System::Windows::Forms::Control::text'
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2061: syntax
error : identifier 'Font'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : warning C4832: token
'.' is illegal after UDT 'System:rawing::Brushes'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.dll :
see declaration of 'System:rawing::Brushes'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2143: syntax
error : missing ';' before ')'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2143: syntax
error : missing ';' before ')'
1>Build log was saved at "file://c:\Users\Allen\Documents\Visual Studio
2008\Projects\TimeTracking\TimeTracking\Debug\BuildLog.htm"
1>TimeTracking - 5 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


--
Thanks
Allen
"Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
news:577703D2-8739-4B8C-A74E-(E-Mail Removed)...
> Hi Allen,
>
> You need to do something similar as you do with the image file. Load it
> into memory and draw it onto the PrintPage.
>
> To draw the text you can use
>
> string text = File.ReadAllText("ReadMe.txt");
> gr.DrawString(text, new Font("Arial", 10), Brushes.Black, 0, 0);
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>
>
> "Allen" wrote:
>
>> Hi everybody,
>>
>> I need your help.
>>
>> The code below came from a book which teaches how to print a sketch. The
>> book did not show how to print a 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
>> ("ReadMe.txt")?
>>
>> I would appreciate 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:ialogResult::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 text.
>>
>> 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);
>> }
>>
>>
>>
>> --
>> Thanks
>> Allen
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No print file / Print file size 0 bytes =?Utf-8?B?ZmlyZXN0b25l?= Windows XP Print / Fax 1 17th Apr 2006 05:01 PM
How do I print from a file created from sending a print to a file =?Utf-8?B?VGVkIEpvaG5zdG9u?= Microsoft Excel Misc 1 23rd Feb 2006 03:10 AM
Print a list of tasks by performing file, print, print is grayed =?Utf-8?B?Vmlubmll?= Microsoft Outlook Discussion 0 6th May 2005 08:23 PM
How to print a raw file (print ready file) through USB port in MS Windows? johny Windows Networking 0 18th Nov 2004 03:09 AM
Is it Possible: Watch print spooler--if print appears, extract print data as file to disk and delete from spooler RDI Microsoft VB .NET 0 7th Apr 2004 09:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.