且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows窗体中打印Printdialog和Prinview窗口

更新时间:2023-12-06 12:41:40

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