Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-15
11
Medium Priority
?
741 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

885 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