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.InteropServices.DllImport("gdi32.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(memoryImage);
   IntPtr dc1 = mygraphics.GetHdc();
   IntPtr dc2 = memoryGraphics.GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}


Can anybody translate this code into Visual C++ .NET? Thanks in advance.
judicoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
drichardsConnect With a Mentor Commented:
It would look like this:
-------------------------------------------
    public:
        [System::Runtime::InteropServices::DllImport("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(memoryImage);
            IntPtr dc1 = mygraphics->GetHdc();
            IntPtr dc2 = memoryGraphics->GetHdc();
            BitBlt(dc2, 0, 0, this->ClientRectangle.Width, this->ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics->ReleaseHdc(dc1);
            memoryGraphics->ReleaseHdc(dc2);
        }

    private:
        System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
        {
           CaptureScreen();
           printDocument1->Print();
        }

    private:
        System::Void printDocument1_PrintPage(System::Object *  sender, System::Drawing::Printing::PrintPageEventArgs *  e)
        {
            e->Graphics->DrawImage(memoryImage, 0, 0);
        }
    };
0
 
drichardsCommented:
I assume you meant managed C++ and not an unmanaged C++ windows app...?
0
 
judicoAuthor Commented:
Yes, that's correct -- managed C++ ...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
judicoAuthor Commented:
Prints just a blank page ... nothing on it ...
0
 
judicoAuthor Commented:
Also, shouldn't there be a dialog popping up to choose printer and other parameters?
0
 
judicoAuthor Commented:
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?
0
 
drichardsCommented:
>> 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".
0
 
judicoAuthor Commented:
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.Sleep(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?
0
 
judicoAuthor Commented:
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::Form
      {      
      public:
            Form1(void)
            {
                  InitializeComponent();
            }
 
      protected:
            void Dispose(Boolean disposing)
            {
                  if (disposing && components)
                  {
                        components->Dispose();
                  }
                  __super::Dispose(disposing);
            }
      private: System::Windows::Forms::Button *  button1;
      public: System::Drawing::Printing::PrintDocument *  printDocument1;


      private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container * 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::Button();
                  this->printDocument1 = new System::Drawing::Printing::PrintDocument();
                  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->OriginAtMargins = true;
                  //
                  // Form1
                  //
                  this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
                  this->BackColor = System::Drawing::Color::LightGray;
                  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::InteropServices::DllImport("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(memoryImage);
            IntPtr dc1 = mygraphics->GetHdc();
            IntPtr dc2 = memoryGraphics->GetHdc();
            BitBlt(dc2, 0, 0, this->ClientRectangle.Width, this->ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics->ReleaseHdc(dc1);
            memoryGraphics->ReleaseHdc(dc2);
        }
      
            
      private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
                   {

                          CaptureScreen();
           printDocument1->Print();
                   }


 private:
        System::Void printDocument1_PrintPage(System::Object *  sender, System::Drawing::Printing::PrintPageEventArgs *  e)
        {
            e->Graphics->DrawImage(memoryImage, 0, 0);
        }
    };


}
0
 
drichardsCommented:
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->AllowSomePages = true;         // Set whatever options you want...
           printDialog1->ShowHelp = true;
           System::Windows::Forms::DialogResult 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->OriginAtMargins = true;
               this->printDocument1->PrintPage += new System::Drawing::Printing::PrintPageEventHandler(this, printDocument1_PrintPage);

Your version is missing the last line.
0
 
judicoAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.