簡體   English   中英

來自另一個類的 C# PrintPage 函數

[英]C# PrintPage function from another class

問題

我在將 PrintPage 函數(SampleForm_PrintPage)移動到新類(PrintPageDesign)時遇到問題,PrintPage 的設計也使用主窗體中的數據,我無法將數據拉入新類。

為什么?

我將所有 PrintPage 函數移動到單個類,因為應用程序中需要多個頁面設計,當每個頁面設計需要任何更改時,將它們全部放在同一個主窗體中似乎很難審查和更新。

示例代碼

為了簡化我的問題,我在 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移動到類PrintPageDesign ,目前只有 Visual Studio 生成的代碼在類中

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

namespace Sample_Print
{
    class PrintPageDesign
    {
    }
}

我嘗試了幾種方法從主窗體外的文本框中獲取值,但結果為空。

非常感謝任何幫助。

如上所述,您可以使用分部類來分隔Main窗體成員、方法和功能。

  • Shift + Alt + C添加一個新類。 將文件重命名為PrintPageDesign並點擊Add
  • 在新類中,添加partial修飾符並將名稱更改為Main (主窗體的確切名稱)。 請注意,我們在這里創建一個部分類,而不是從Main表單派生

現在您在Main窗體上下文中,您可以訪問它的成員。


例子

窗體類:

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

PrintPageDesign類:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM