judico
asked on
Can anybody translate this printing example from C# into Visual C++ .NET?
There is an example in:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vclrfcodeprintingformvisualc.asp
for printing a form in C#:
[System.Runtime.InteropSer vices.DllI mport("gdi 32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryI mage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width , this.ClientRectangle.Heigh t, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1) ;
memoryGraphics.ReleaseHdc( dc2);
}
private void printDocument1_PrintPage(S ystem.Obje ct sender, System.Drawing.Printing.Pr intPageEve ntArgs e)
{
e.Graphics.DrawImage(memor yImage, 0, 0);
}
private void printButton_Click(System.O bject sender, System.EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
Can anybody translate this code into Visual C++ .NET? Thanks in advance.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vclrfcodeprintingformvisualc.asp
for printing a form in C#:
[System.Runtime.InteropSer
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryI
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(
}
private void printDocument1_PrintPage(S
{
e.Graphics.DrawImage(memor
}
private void printButton_Click(System.O
{
CaptureScreen();
printDocument1.Print();
}
Can anybody translate this code into Visual C++ .NET? Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I assume you meant managed C++ and not an unmanaged C++ windows app...?
ASKER
Yes, that's correct -- managed C++ ...
ASKER
Prints just a blank page ... nothing on it ...
ASKER
Also, shouldn't there be a dialog popping up to choose printer and other parameters?
ASKER
I want to accept the answer but would like to know should I open another question to resolve the outstanding issues or we should continue here?
>> Prints just a blank page ... nothing on it ...
What's on your form? When I do it it prints the form surface including the "Print" button that I used to launch the print. If you run it in the debugger and break inside the 'Capture' method and step through, the form goes behind the debugger window and it actually prints the portion of the debugger screen that is on top of the form.
>> Also, shouldn't there be a dialog popping up to choose printer and other parameters?
No, it prints with the default printer and settings. You need to show the print dialog if you want it - it's in the toolbox along with the "PrintDocument".
What's on your form? When I do it it prints the form surface including the "Print" button that I used to launch the print. If you run it in the debugger and break inside the 'Capture' method and step through, the form goes behind the debugger window and it actually prints the portion of the debugger screen that is on top of the form.
>> Also, shouldn't there be a dialog popping up to choose printer and other parameters?
No, it prints with the default printer and settings. You need to show the print dialog if you want it - it's in the toolbox along with the "PrintDocument".
ASKER
Yes, that's exactly what I have too -- form surface and a print button. But nothing gets printed on the paper.
I placed a breakpoint in System.Threading.Thread.Sl eep(100) but I still got a blank page. A blank page was obtained also when I put a breakpoint in CaptureScreen();.
How do you show the PrintDialog?
I placed a breakpoint in System.Threading.Thread.Sl
How do you show the PrintDialog?
ASKER
Maybe I'm not placing the printing code properly. Here's what I have:
#pragma once
namespace PRINTING
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Fo rm
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing );
}
private: System::Windows::Forms::Bu tton * button1;
public: System::Drawing::Printing: :PrintDocu ment * printDocument1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Co ntainer * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Bu tton();
this->printDocument1 = new System::Drawing::Printing: :PrintDocu ment();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(176 , 64);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// printDocument1
//
this->printDocument1->Orig inAtMargin s = true;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->BackColor = System::Drawing::Color::Li ghtGray;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this-> button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
public:
[System::Runtime::InteropS ervices::D llImport(" gdi32.dll" )]
static long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private:
System::Drawing::Bitmap *memoryImage;
private:
void CaptureScreen()
{
Graphics *mygraphics = this->CreateGraphics();
System::Drawing::Size s = this->Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics *memoryGraphics = Graphics::FromImage(memory Image);
IntPtr dc1 = mygraphics->GetHdc();
IntPtr dc2 = memoryGraphics->GetHdc();
BitBlt(dc2, 0, 0, this->ClientRectangle.Widt h, this->ClientRectangle.Heig ht, dc1, 0, 0, 13369376);
mygraphics->ReleaseHdc(dc1 );
memoryGraphics->ReleaseHdc (dc2);
}
private: System::Void button1_Click(System::Obje ct * sender, System::EventArgs * e)
{
CaptureScreen();
printDocument1->Print();
}
private:
System::Void printDocument1_PrintPage(S ystem::Obj ect * sender, System::Drawing::Printing: :PrintPage EventArgs * e)
{
e->Graphics->DrawImage(mem oryImage, 0, 0);
}
};
}
#pragma once
namespace PRINTING
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Fo
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing
}
private: System::Windows::Forms::Bu
public: System::Drawing::Printing:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Co
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Bu
this->printDocument1 = new System::Drawing::Printing:
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(176
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this,
//
// printDocument1
//
this->printDocument1->Orig
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->BackColor = System::Drawing::Color::Li
this->ClientSize = System::Drawing::Size(292,
this->Controls->Add(this->
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
public:
[System::Runtime::InteropS
static long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private:
System::Drawing::Bitmap *memoryImage;
private:
void CaptureScreen()
{
Graphics *mygraphics = this->CreateGraphics();
System::Drawing::Size s = this->Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics *memoryGraphics = Graphics::FromImage(memory
IntPtr dc1 = mygraphics->GetHdc();
IntPtr dc2 = memoryGraphics->GetHdc();
BitBlt(dc2, 0, 0, this->ClientRectangle.Widt
mygraphics->ReleaseHdc(dc1
memoryGraphics->ReleaseHdc
}
private: System::Void button1_Click(System::Obje
{
CaptureScreen();
printDocument1->Print();
}
private:
System::Void printDocument1_PrintPage(S
{
e->Graphics->DrawImage(mem
}
};
}
To use the PrintDialog, use the toolbox to ad a PrintDialog to the form and change button1_Click code to this:
CaptureScreen();
printDialog1->Document = printDocument1;
printDialog1->AllowSomePag es = true; // Set whatever options you want...
printDialog1->ShowHelp = true;
System::Windows::Forms::Di alogResult res = printDialog1->ShowDialog() ;
if ( res == DialogResult::OK )
{
printDocument1->Print();
}
As for the blank page, somehow you removed the code that sets the print handler so you were never calling the code to write the form image to the print document. In InitializeComponents there is a "PrintDocument1" section that should look like this:
//
// printDocument1
//
this->printDocument1->Orig inAtMargin s = true;
this->printDocument1->Prin tPage += new System::Drawing::Printing: :PrintPage EventHandl er(this, printDocument1_PrintPage);
Your version is missing the last line.
CaptureScreen();
printDialog1->Document = printDocument1;
printDialog1->AllowSomePag
printDialog1->ShowHelp = true;
System::Windows::Forms::Di
if ( res == DialogResult::OK )
{
printDocument1->Print();
}
As for the blank page, somehow you removed the code that sets the print handler so you were never calling the code to write the form image to the print document. In InitializeComponents there is a "PrintDocument1" section that should look like this:
//
// printDocument1
//
this->printDocument1->Orig
this->printDocument1->Prin
Your version is missing the last line.
ASKER
Beautiful ... Thank you so much. Now I am starting to see the light at the end of the tunnel for my project thanks to rare individuals like you. Keep up the good work and all the best.
judico
judico