[C++ CLI] Print example

Hello, i need help or short code for look how can i print document (printDocument toolbox). For example when i have one picture and two strings, how can i print this?

Thanks a lot :)
closed account (48bpfSEw)
You can ask Mrs. Google, too ^^

https://www.google.de/search?q=cpp+print&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&ei=a1ZDV7-YNKKK8QeC36eQBw

the best answer of the year. i dont know about .... printing to console or somewhere else, if you can read, its called "printDocument", but maybe is better first to read and then answer.
closed account (48bpfSEw)
don't waste your engergy (=discontent) in fighting against someone,
use it to solve your problem instead!
this is only normal question in time, everytime i post question then i making solutions by self and in almost all times i make solution faster by myself (its better and good for learning), but this question about printDocument, because i found only C# examples but in C++ it's don't work, for sure i change syntax from C# (.) to C++ (->) but still have problem with e->Graphics.... to format string into print document.
closed account (48bpfSEw)
you are welcome. I know how frustrating coding can be...

Is this link your usefull for your purposes:

http://stackoverflow.com/questions/8956824/whats-equivalent-to-printdocument-of-c-sharp-in-c-c
because i found only C# examples but in C++ it's don't work, for sure i change syntax from C# (.) to C++ (->) but still have problem with e->Graphics.... to format string into print document.


Can you show the c# code and your translation.
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
private: System::Void button_printpreview_Click(System::Object^  sender, EventArgs^ e, PrintPageEventArgs^ ev) {
		if (richTextBox_text->Text == "")
		{
			MessageBox::Show("Obsah nemoze byt prazdny");
		}
		else
		{
			printFont = gcnew System::Drawing::Font("Arial", 10);
			ev->Graphics->DrawString("test string", printFont, Brushes::Black, ev->MarginBounds.Left, 0.0, gcnew StringFormat);
			
			printPreviewDialog1->ShowDialog();
		}
	}

but i can't use PrintPageEventArgs^ ev in button because then i have error onClick event : http://prntscr.com/b86mfu
but i can't use PrintPageEventArgs^ ev in button

Normally the printing is done in the PrintPage(object sender^, PrintPageEventArgs^ e) event of the PrintDocument
and when i need printpreview first? how can i achieve this? first i need format strings and images for printing and after this i can use preview and then print, or its little different?
There is a PrintPreviewDialog in the toolbox.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

namespace WinFormDemo {

	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
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
  private: System::Drawing::Printing::PrintDocument^  printDocument1;
  protected:
  private: System::Windows::Forms::PrintPreviewDialog^  printPreviewDialog1;
  private: System::Windows::Forms::Button^  button1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
      System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager (Form1::typeid));
      this->printDocument1 = (gcnew System::Drawing::Printing::PrintDocument ());
      this->printPreviewDialog1 = (gcnew System::Windows::Forms::PrintPreviewDialog ());
      this->button1 = (gcnew System::Windows::Forms::Button ());
      this->SuspendLayout ();
      // 
      // printDocument1
      // 
      this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler (this, &Form1::printDocument1_PrintPage);
      // 
      // printPreviewDialog1
      // 
      this->printPreviewDialog1->AutoScrollMargin = System::Drawing::Size (0, 0);
      this->printPreviewDialog1->AutoScrollMinSize = System::Drawing::Size (0, 0);
      this->printPreviewDialog1->ClientSize = System::Drawing::Size (400, 300);
      this->printPreviewDialog1->Enabled = true;
      this->printPreviewDialog1->Name = L"printPreviewDialog1";
      this->printPreviewDialog1->Visible = false;
      // 
      // button1
      // 
      this->button1->AutoSize = true;
      this->button1->Location = System::Drawing::Point (75, 45);
      this->button1->Name = L"button1";
      this->button1->Size = System::Drawing::Size (253, 56);
      this->button1->TabIndex = 0;
      this->button1->Text = L"PrintPreview";
      this->button1->UseVisualStyleBackColor = true;
      this->button1->Click += gcnew System::EventHandler (this, &Form1::button1_Click);
      // 
      // Form1
      // 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::None;
      this->BackColor = System::Drawing::SystemColors::ActiveCaption;
      this->ClientSize = System::Drawing::Size (622, 435);
      this->Controls->Add (this->button1);
      this->Font = (gcnew System::Drawing::Font (L"Microsoft Sans Serif", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
        static_cast<System::Byte>(0)));
      this->Name = L"Form1";
      this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
      this->Text = L"Form1";
      this->ResumeLayout (false);
      this->PerformLayout ();

    }
#pragma endregion
  private: System::Void button1_Click (System::Object^  sender, System::EventArgs^  e) 
  {
    printPreviewDialog1->Document = printDocument1;
    printPreviewDialog1->ShowDialog ();
  }
  private: System::Void printDocument1_PrintPage (System::Object^  sender, System::Drawing::Printing::PrintPageEventArgs^  e) 
  {
    e->Graphics->DrawString (L"Hello World", this->Font, Brushes::Red, 50, 50);
  }
  };
}
Thanks a lot :)
Topic archived. No new replies allowed.