[英]C# - Printing the Form
可能可以使用DrawToBitmap方法。
如果您希望以一種相對簡單的方法從表單中打印出數據,則可能需要嘗試這種方式。 當我需要從表單中打印某些內容時,可以使用此方法。 這使用了隱藏的WebBrowser控件,效果很好。
抱歉,該示例來自C ++項目,但可以很好地轉換為C#。
private: System::Void printButton_Click(System::Object^ sender, System::EventArgs^ e) {
StringBuilder^ html = gcnew StringBuilder();
html->Append( "<html><head></head><body>" );
html->Append( "<h1>Children Clocked In</h1>" );
html->Append( "<table>" );
html->Append( "<tr><td>Last Name</td><td>First Name</td><td>Classroom</td><td>Program</td><td>In Time</td></tr>" );
for each ( DataGridViewRow^ row in children->SelectedRows )
{
html->AppendFormat( "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>",
row->Cells[2]->Value->ToString(),
row->Cells[3]->Value->ToString(),
row->Cells[4]->Value->ToString(),
row->Cells[5]->Value->ToString(),
Convert::ToDateTime(row->Cells[6]->Value).ToString("h:mm tt") );
}
html->Append( "</table>" );
html->Append( "</body></html>" );
WebBrowser^ webBrowser = gcnew WebBrowser();
webBrowser->Visible = false;
webBrowser->Parent = this;
webBrowser->DocumentCompleted += gcnew System::Windows::Forms::WebBrowserDocumentCompletedEventHandler(this, &FormChildrenClockedIn::webBrowser1_DocumentCompleted);
webBrowser->DocumentText = html->ToString();
}
private: System::Void webBrowser1_DocumentCompleted(System::Object^ sender, System::Windows::Forms::WebBrowserDocumentCompletedEventArgs^ e) {
((WebBrowser^)sender)->ShowPrintPreviewDialog();
delete (WebBrowser^)sender;
}
最簡單的方法是像在屏幕外的某個地方一樣打開它
this.Position=new Point(-100000,-100000);
打印,然后將其關閉。
(不要忘記使用多台顯示器,這就是為什么我使用這么大的數字)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.