简体   繁体   中英

How can I use the Print Dialog

If you go in Visual Studio 2005 to the following (or just do ctrl+p): File ==> Print..

You get a print dialog screen. I want the same in my program, but how?

This dialog box is a so-called common dialog , a built-in Windows dialog that can be used by multiple applications.

To use this dialog box in your C# application, you can use the PrintDialog class. The following MSDN pages contains descriptions as well as some sample code:

You can have a standard print dialog with this:

var printDialog = new PrintDialog();
printDialog.ShowDialog();

... but printing has to be done by yourself ... ;-)

Edit : For all those who still use VisualStudio2005:

PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();

For the CTRL+P shortcut: Add a toolbar (I think it was called ToolStrip) to your form, put an entry in it to wich you assign the shortcut CTRL+P from the properties panel. For the PrintDialog: Add a PrintDialog control to your form and set the Document property to the document that should be printed. Go into the code for the click event of your print entry in the toolbar. Add the code PrintDialog.ShowDialog(); to it, check if the Print button was clicked, and if so, print it using DocumentToPrint.Print(); . Here's an example:

private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {

        // Allow the user to choose the page range he or she would
        // like to print.
        PrintDialog1.AllowSomePages = true;

        // Show the help button.
        PrintDialog1.ShowHelp = true;

        // Set the Document property to the PrintDocument for 
        // which the PrintPage Event has been handled. To display the
        // dialog, either this property or the PrinterSettings property 
        // must be set 
        PrintDialog1.Document = docToPrint;

        DialogResult result = PrintDialog1.ShowDialog();

        // If the result is OK then print the document.
        if (result==DialogResult.OK)
        {
            docToPrint.Print();
        }

    }

Example source: http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.document.aspx

嗯,你可以使用巧妙命名的PrintDialog类....

If your using WinForms to build your UI, then you can use the native PrintDialog control (see here ). So far as I know, that should appear in the Toolbox in the designer mode of a WinForms control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM