简体   繁体   English

如何关闭“打印预览”对话框?

[英]How to close a Print Preview Dialog?

I have a document printing function. 我具有文件打印功能。 I am trying to close the Print Preview Dialog form after the user presses the Print button. 我试图在用户按下“打印”按钮后关闭“打印预览对话框”表单。 Once the print button in the print preview dialog is pressed, the event starts the function below to print the document. 按下“打印预览”对话框中的“打印”按钮后,该事件将启动下面的功能以打印文档。 I am expecting the form to close when I call printPreviewDialog1.Close() but it just goes over the line and nothing happens. 我希望在我调用printPreviewDialog1.Close()时关闭该窗体,但是它只是越过了一行而没有任何反应。

But it doesn't close the print preview dialog form after the print job is done. 但是在完成打印作业后,它不会关闭打印预览对话框表单。

    public void _start_Print(object sender, EventArgs e)
    {
        printDocument1.Print();

    }

Added as requested in the comments. 根据评论中的要求添加。 Initializing Print Preview Dialog 初始化打印预览对话框

    private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        printPreviewDialog1.Document = printDocument1;

        ToolStrip ts = new ToolStrip();
        ts.Name = "wrongToolStrip";
        foreach (Control ctl in printPreviewDialog1.Controls)
        {
            if (ctl.Name.Equals("toolStrip1"))
            {
                ts = ctl as ToolStrip;
                break;
            }
        }

        ToolStripButton printButton = new ToolStripButton();
        ToolStripButton closeButton = new ToolStripButton();

        foreach (ToolStripItem tsi in ts.Items)
        {
            if (tsi.Name.Equals("printToolStripButton"))
            {
                printButton = tsi as ToolStripButton;
            }
            else if (tsi.Name.Equals("closeToolStripButton"))  // idk if this is the name of the close button im trying to programmatically close it after printing
            {
                closeButton = tsi as ToolStripButton;
            }
        }

        ts.Items.Remove(printButton);
        ToolStripButton b = new ToolStripButton();
        b.ImageIndex = printButton.ImageIndex;
        b.Visible = true;
        ts.Items.Insert(0, b);
        b.Click += new EventHandler(this._start_Print);

        printPreviewDialog1.WindowState = FormWindowState.Maximized;
        printPreviewDialog1.ShowDialog();
        printPreviewDialog1.Dispose(); //< doesnt do anything 
        closeButton.PerformClick(); // < doesn't do anything, possibly using wrong name for toolstripbutton

    }

Initializing Print Document as requested in the comments 根据注释中的要求初始化打印文档

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawRectange(Pens.Black, 60, 60, 60,60);
    }

Solved. 解决了。

I created a Global ToolStripButton and initialized this button with the closeToolStripButton in the printPreviewDialog . 我创建了一个Global ToolStripButton并在closeToolStripButton中使用printPreviewDialog初始化了此按钮。

Then I programmatically clicked this button after the print job was done. 然后,在完成打印作业后,我以编程方式单击了此按钮。

    private ToolStripButton closeButton = new ToolStripButton();  <-- global variable

    private void btnPrint_Click(object sender, EventArgs e)
    {
        foreach (ToolStripItem tsi in ts.Items)
        { 
            if (tsi.Name.Equals("closeToolStripButton"))
            {
                closeButton = tsi as ToolStripButton;
            }
            else if (tsi.Name.Equals("printToolStripButton"))
            {
                printButton = tsi as ToolStripButton;
            }
        }

        ts.Items.Remove(printButton);
        ToolStripButton b = new ToolStripButton();
        b.ImageIndex = printButton.ImageIndex;
        b.Visible = true;
        ts.Items.Insert(0, b);
        b.Click += new EventHandler(this._start_Printer); //<-- this starts the printer event where i "PerformClick() on the initialized closeButton"

        printprevDialog.WindowState = FormWindowState.Maximized;
        printprevDialog.ShowDialog();

    }


    public void _start_Printer(object sender, EventArgs e) // <--- then i just performed the close click here right after i hit the print button
    {
        printDocument1.Print();
        closeButton.PerformClick(); // <-- this way im not violating cross thread operations
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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