如何在Windows窗体中打印Printdialog和Prinview窗口 [英] How to diplay Printdialog and Prinview windows befor Print starts in Windows Forms

查看:242
本文介绍了如何在Windows窗体中打印Printdialog和Prinview窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在研究Windows Forms中的项目,我要做的是从Excel文件中获取数据并在datagridview中显示这些数据。如果在dgv Row中当选,我必须从选定的行获取数据并将其发送到Html Document,而不是我需要打印该Html文档。打印和evrything正在工作,但我现在需要在打印开始之前显示Printdialog和Prinview窗口,我不知道该怎么做,我在哪里插入Printdialog方法?在我的案例打印中,当我按下按钮时直接启动,但我也想显示Prindialog和Printview窗口。有人可以帮我弄这个吗 ?这是我的代码:



  //  这里我得到选定的行值 
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
string treqingi,gvari,saxeli,telefoni,tarigi,wona;
treqingi = row.Cells [ 0 ]。Value.ToString();
gvari = row.Cells [ 1 ]。Value.ToString();
saxeli = row.Cells [ 2 ]。Value.ToString();
telefoni = row.Cells [ 3 ]。Value.ToString();
tarigi = row.Cells [ 4 ]。Value.ToString();
wona = row.Cells [ 5 ]。Value.ToString();

InsertingVariableIntoHtml(treqingi,saxeli,gvari,telefoni,wona);
}

// HTML页面打印
// 创建WebBrowser实例。
WebBrowser webBrowserForPrinting = new WebBrowser();

// 添加一个在加载后打印文档的事件处理程序。
webBrowserForPrinting.DocumentCompleted + =
new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// 设置Url属性以加载文档。
webBrowserForPrinting.Url = new Uri(System.IO.Directory.GetCurrentDirectory()+ \\intel\\main.html);



}

  private   void  PrintDocument( object  sender,WebBrowserDocumentCompletedEventArgs e)
{
// 现在打印文档已满载。
((WebBrowser)sender).Print( );

// 现在,在任务完成时处理WebBrowser。
((WebBrowser)sender).Dispose();
}

解决方案

可能你的意思是打印预览。不,你不想在打印开始之前显示打印预览,这没有任何意义。您应该在用户需要时显示它;然后用户可能会也可能不会选择打印。打印预览可以使用类 PrintPreviewDialog 显示:

PrintPreviewDialog Class(System.Windows.Forms) [ ^ ]。



如果用户想要在没有打印预览的情况下进行打印,则此人应该可以这样做。但在所有情况下,如果用户选择打印,则需要通过 PrintDialog 选择打印机和打印机设置: PrintDialog类(System.Windows.Controls) [ ^ ]。



基本上,这个MSDN页面解释了如何做到这一切:如何:使用打印预览在Windows窗体中打印 [ ^ ]。



-SA

Hello guys,I am working on my Project in Windows Forms and what I have to do is to get data from Excel File and display this data in datagridview. If in dgv Row is elected, I have to get data from selected Row and send it to Html Document and than I need to Print that Html Document. Also printing and evrything is working but I need now to diplay Printdialog and Prinview window before Print starts and I donn't know how to do that, where have I to insert Printdialog Method?. In my Case Printing start direct when I press a Button, but I want to display Prindialog and Printview window too. Can someone help me with this ? here's my Code:

//Here I am getting Selected Row values
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    string treqingi, gvari, saxeli, telefoni, tarigi, wona;
    treqingi = row.Cells[0].Value.ToString();
    gvari = row.Cells[1].Value.ToString();
    saxeli = row.Cells[2].Value.ToString();
    telefoni = row.Cells[3].Value.ToString();
    tarigi = row.Cells[4].Value.ToString();
    wona = row.Cells[5].Value.ToString();

    InsertingVariableIntoHtml(treqingi, saxeli, gvari, telefoni, wona);
}

// HTML page Printing
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();

// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
    new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(System.IO.Directory.GetCurrentDirectory() + "\\intel\\main.html");


}

private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Print the document now that it is fully loaded.
            ((WebBrowser)sender).Print();

            // Dispose the WebBrowser now that the task is complete. 
            ((WebBrowser)sender).Dispose();
        }

解决方案

Probably you mean "print preview". No, you don't want to show print preview "before print starts", it would not make any sense. You should show it when the user wants; and then the user may or may not chose to print. Print preview can be shown using the class PrintPreviewDialog:
PrintPreviewDialog Class (System.Windows.Forms)[^].

If the user wants to print without print preview, this person should be able to do that. But in all cases, if the user chooses to print, the printer and printer settings needs to be chosen through PrintDialog: PrintDialog Class (System.Windows.Controls)[^].

Basically, this MSDN page explains how to do it all: How to: Print in Windows Forms Using Print Preview[^].

—SA


这篇关于如何在Windows窗体中打印Printdialog和Prinview窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆