来自另一个 class 的 C# PrintPage 函数

C# PrintPage function from another class

问题

我在将 PrintPage 函数 (SampleForm_PrintPage) 移动到新的 class (PrintPageDesign)[=48 时遇到问题=] PrintPage 的设计也使用主窗体中的数据,我无法将数据拉入新的 class.

为什么?

我将所有 PrintPage 函数移动到单独的 classes,因为应用程序需要多个页面设计,当每个页面设计都需要时,将它们全部放在同一个主窗体中似乎很难审查和更新任何变化。

示例代码

为了简化我的问题,我在 Visual Basic 中创建了一个示例解决方案,

Form1.cs(表格代码):

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Sample_Print
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void BTN_Print_Click(object sender, EventArgs e)
        {
            PrintDialog PD_SamplePage = new PrintDialog();
            PrintDocument Doc_SamplePage = new PrintDocument();
            Doc_SamplePage.PrintPage += SampleForm_PrintPage;
            PD_SamplePage.Document = Doc_SamplePage;
            Doc_SamplePage.Print();
        }

        protected void SampleForm_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.DrawString(TB_Name.Text.ToString(), new Font("Roboto Condensed",12, FontStyle.Bold), Brushes.Black, 10, 10);
        }

    }
}

要求

我想移动函数 SampleForm_PrintPage 到 class PrintPageDesign ,目前只有 visual studio 生成的代码在 class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sample_Print
{
    class PrintPageDesign
    {
    }
}

我尝试了几种方法从主窗体外部的文本框中获取值,但结果为空。

非常感谢任何帮助。

如上所述,您可以使用部分 classes 来分隔 Main 表单成员、方法和功能。

  • Shift+Alt+C添加新的class .将文件重命名为 PrintPageDesign 并点击 Add.
  • 在新的class中,添加partial修饰符并将名称更改为Main(与主窗体的确切名称)。请注意,我们在这里创建 partial class 而不是 deriving from the Main form.

现在您位于 Main 表单上下文中,您可以访问其成员。


例子

表格class:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Sample_Print
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void BTN_Print_Click(object sender, EventArgs e) => PrintJob1();
    }
}

PrintPageDesignclass:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.ComponentModel;

namespace Sample_Print
{
    partial class Main
    {
        private void PrintJob1(bool preview = false)
        {
            using (var doc = new PrintDocument())
            {
                doc.PrintPage += (s, e) =>
                {
                    var g = e.Graphics;
                    var r = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, 
                        e.MarginBounds.Width, 32);

                    using (var sf = new StringFormat())
                    using (var fnt = new Font("Roboto Condensed", 12, FontStyle.Bold))
                    {
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Center;

                        g.DrawString(TB_Name.Text, fnt, Brushes.Black, r, sf);
                        r.Y += r.Height;

                        foreach (Control c in Controls)
                        {
                            g.DrawString(c.Name, fnt, Brushes.Black, r, sf);
                            r.Y += r.Height;
                        }

                        // ...
                    }
                };

                if (preview)
                    using (var ppd = new PrintPreviewDialog() { Document = doc })
                        ppd.ShowDialog();
                else
                {
                    using (var pd = new PrintDialog() { Document = doc })
                    {
                        if (pd.ShowDialog() == DialogResult.OK)
                            pd.Document.Print();
                    }
                }
            }
        }
    }
}