【问题标题】:C# - Printing the FormC# - 打印表单
【发布时间】:2010-05-18 19:30:08
【问题描述】:

我正在使用从 MS 到 print a form 的代码,但看起来需要使用 Show/ShowDialog() 才能看到表单。

我正在尝试将代码用于我不想显示的表单。

有什么想法吗?

【问题讨论】:

    标签: c# .net winforms printing


    【解决方案1】:

    最简单的方法是在屏幕外的某个地方打开它,比如

    this.Position=new Point(-100000,-100000);
    

    打印然后关闭它。

    (不要忘记多台显示器,这就是我使用这么大数字的原因)。

    【讨论】:

    • 必须将 StartPosition 设置为 Manual,然后将 Location 设置为 -100000。谢谢
    【解决方案2】:

    可能,您可以使用DrawToBitmap 方法。

    【讨论】:

      【解决方案3】:

      如果您希望以相对简单的方法从表单中打印数据,您可能想尝试这种方式。当我需要从表单中打印某些内容时,我会使用此方法。这使用了一个隐藏的 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;
           }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        相关资源
        最近更新 更多