【问题标题】:C# - Windows Forms - Try to print - nothing is drawn - print dialogC# - Windows 窗体 - 尝试打印 - 未绘制任何内容 - 打印对话框
【发布时间】:2021-01-01 21:11:08
【问题描述】:

您能帮我介绍一下 C# 2010 吗?

我想通过 Windows 窗体中的打印对话框打印一些东西。 但是在 Open XPS 虚拟打印机中的论文中没有绘制任何内容。 我制作了一个带有按钮和richTextBox 的表单。 我使用了来自网站的示例代码,并稍微更改了该代码。

这是图片:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FT_Pnt_01
{
    public partial class Form1 : Form
    {



        PrintDialog PrintDialog1;


        public Form1()
        {
            InitializeComponent();
        }

        // Declare the PrintDocument object.

        private System.Drawing.Printing.PrintDocument docToPrint =

            new System.Drawing.Printing.PrintDocument();




        // This method will set properties on the PrintDialog object and
        // then display the dialog.

        private void button_Print_01_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();
            }


        }


        // The PrintDialog will print the document
        // by handling the document's PrintPage event.


        private void document_PrintPage(object sender,

            System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Insert code to render the page here.
            // This code will be called when the control is drawn.
            // The following code will render a simple
            // message on the printed document.


            string text = "پرینت گرفتن متن در سی شارپ";

            System.Drawing.Font printFont = new System.Drawing.Font

                ("Arial", 35, System.Drawing.FontStyle.Regular);



            MessageBox.Show("e.ToString() = " + e.ToString());




            // Draw the content.


            e.Graphics.Clear(Color.Blue);


            e.Graphics.DrawString(text, printFont,

                System.Drawing.Brushes.Black, 10, 10);


        }

        private void Form1_Load(object sender, EventArgs e)
        {


            PrintDialog1 = new PrintDialog();


        }

    }
}

【问题讨论】:

    标签: c# winforms printing dialog


    【解决方案1】:

    从您提供的示例看来,您没有将方法与 PrintPage 事件处理程序相关联。所以你的 document_PrintPage() 方法永远不会被调用。

    我会补充:

     docToPrint.PrintPage += document_PrintPage;
    

    在表单的构造函数中。

    例子:

    private PrintDocument docToPrint = new PrintDocument();
    
    public Form1()
    {
        InitializeComponent();
        docToPrint.PrintPage += document_PrintPage;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多