在我的代码中实现打印对话框

Implement print dialog in my code

我一直在尝试自己解决这个问题,但我还没有弄明白。我想在按下 btn_print 按钮时打开一个打印对话框 window。我已经标注了我认为不再需要的一行,因为它定义了打印页面的大小。

谁能看看我的代码并告诉我我能做什么?

private void btn_print_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            PrintDocument pd = new PrintDocument();
            //pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            pd.Print();
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while printing", ex.ToString());
        }
    }

    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {

    }

尝试这样的事情:

PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

System.Windows.Forms.PrintDialog p = new System.Windows.Forms.PrintDialog();
p.Document = pd;
if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    pd.Print();