Solved

Can anybody translate this printing example from C# into Visual C++ .NET?

Posted on 2004-08-15
11
734 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:judico
  • 7
  • 4
11 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11805088
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
 
LVL 19

Expert Comment

by:drichards
ID: 11805100
I assume you meant managed C++ and not an unmanaged C++ windows app...?
0
 

Author Comment

by:judico
ID: 11805206
Yes, that's correct -- managed C++ ...
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:judico
ID: 11805274
Prints just a blank page ... nothing on it ...
0
 

Author Comment

by:judico
ID: 11805317
Also, shouldn't there be a dialog popping up to choose printer and other parameters?
0
 

Author Comment

by:judico
ID: 11805361
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
 
LVL 19

Expert Comment

by:drichards
ID: 11806119
>> 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
 

Author Comment

by:judico
ID: 11806149
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
 

Author Comment

by:judico
ID: 11806178
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
 
LVL 19

Expert Comment

by:drichards
ID: 11807044
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
 

Author Comment

by:judico
ID: 11807457
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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question