如何在C++ CLR WinForms项目中打印Panel内容并实现打印预览?
实现CLR WinForms Panel打印及预览(C++/CLI)
核心实现步骤
- 添加必要控件:在窗体中添加
PrintDocument和PrintPreviewDialog控件(也可在代码中动态实例化)。 - 处理PrintDocument的PrintPage事件:将Panel的内容绘制到打印画布上。
- 触发打印预览:通过按钮点击事件打开预览对话框。
完整代码示例
1. 窗体头文件(.h)中的声明
#pragma once namespace YourAppNamespace { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class MainForm : public System::Windows::Forms::Form { public: MainForm(void) { InitializeComponent(); } protected: ~MainForm() { if (components) { delete components; } } private: System::Windows::Forms::Panel^ panel1; System::Windows::Forms::TextBox^ textBox1; System::Windows::Forms::Label^ label1; System::Windows::Forms::Button^ btnPreviewPrint; System::Drawing::Printing::PrintDocument^ printDocument1; System::Windows::Forms::PrintPreviewDialog^ printPreviewDialog1; System::ComponentModel::Container ^components; void InitializeComponent(void); private: System::Void printDocument1_PrintPage(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e); private: System::Void btnPreviewPrint_Click(System::Object^ sender, System::EventArgs^ e); }; }
2. 窗体实现文件(.cpp)中的代码
#include "MainForm.h" namespace YourAppNamespace { void MainForm::InitializeComponent(void) { this->panel1 = (gcnew System::Windows::Forms::Panel()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->btnPreviewPrint = (gcnew System::Windows::Forms::Button()); this->printDocument1 = (gcnew System::Drawing::Printing::PrintDocument()); this->printPreviewDialog1 = (gcnew System::Windows::Forms::PrintPreviewDialog()); this->panel1->SuspendLayout(); this->SuspendLayout(); // Panel设置 this->panel1->Controls->Add(this->textBox1); this->panel1->Controls->Add(this->label1); this->panel1->Location = System::Drawing::Point(20, 20); this->panel1->Size = System::Drawing::Size(300, 200); this->panel1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle; // Label设置 this->label1->Text = L"测试标签"; this->label1->Location = System::Drawing::Point(10, 10); this->label1->Size = System::Drawing::Size(100, 20); // TextBox设置 this->textBox1->Text = L"测试文本内容"; this->textBox1->Location = System::Drawing::Point(10, 40); this->textBox1->Size = System::Drawing::Size(280, 20); // 打印预览按钮 this->btnPreviewPrint->Text = L"打印预览"; this->btnPreviewPrint->Location = System::Drawing::Point(20, 230); this->btnPreviewPrint->Click += gcnew System::EventHandler(this, &MainForm::btnPreviewPrint_Click); // PrintDocument事件绑定 this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler(this, &MainForm::printDocument1_PrintPage); // 窗体设置 this->ClientSize = System::Drawing::Size(340, 270); this->Controls->Add(this->panel1); this->Controls->Add(this->btnPreviewPrint); this->Text = L"Panel打印示例"; this->panel1->ResumeLayout(false); this->panel1->PerformLayout(); this->ResumeLayout(false); } // PrintPage事件处理:绘制Panel内容到打印页面 System::Void MainForm::printDocument1_PrintPage(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e) { // 创建Panel的位图 Bitmap^ panelBitmap = gcnew Bitmap(panel1->Width, panel1->Height); panel1->DrawToBitmap(panelBitmap, panel1->ClientRectangle); // 计算缩放比例,使Panel内容适配打印页面的可打印区域 float scaleX = e->MarginBounds.Width / (float)panelBitmap->Width; float scaleY = e->MarginBounds.Height / (float)panelBitmap->Height; float scale = Math::Min(scaleX, scaleY); // 计算绘制位置,居中显示 int x = e->MarginBounds.Left + (e->MarginBounds.Width - (int)(panelBitmap->Width * scale)) / 2; int y = e->MarginBounds.Top + (e->MarginBounds.Height - (int)(panelBitmap->Height * scale)) / 2; // 绘制缩放后的Panel位图 e->Graphics->DrawImage(panelBitmap, x, y, (int)(panelBitmap->Width * scale), (int)(panelBitmap->Height * scale)); delete panelBitmap; // 如果只有一页,设置HasMorePages为false e->HasMorePages = false; } // 打印预览按钮点击事件 System::Void MainForm::btnPreviewPrint_Click(System::Object^ sender, System::EventArgs^ e) { // 关联PrintDocument到预览对话框 printPreviewDialog1->Document = printDocument1; // 显示预览对话框 printPreviewDialog1->ShowDialog(this); } }
关键说明
- DrawToBitmap方法:将Panel及其子控件的内容渲染到位图中,这是实现控件内容打印的核心。
- 缩放适配:通过计算打印页面可打印区域与Panel的比例,确保内容不会超出页面,同时保持居中显示。
- PrintPreviewDialog:只需关联对应的PrintDocument,调用
ShowDialog即可打开预览窗口,无需额外处理预览逻辑。
内容的提问来源于stack exchange,提问作者Routine Programmer
相关产品推荐
相关产品推荐

