Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试创建一个自定义打印类,应用程序在其中通知何时打印页面_C#_Printing - Fatal编程技术网

C# 尝试创建一个自定义打印类,应用程序在其中通知何时打印页面

C# 尝试创建一个自定义打印类,应用程序在其中通知何时打印页面,c#,printing,C#,Printing,我正在为我的工作场所构建一系列应用程序,因此我正在尝试创建我自己的打印类,我可以为我的所有应用程序提供参考 问题是,我试图找出一种方法,让应用程序告诉类何时打印页面,但我找不到这样做的方法 例如,到目前为止,我所掌握的是: // Report Variables private bool bPrinting = false; private int iPage = 0; private float fOverflow = 0.00F; private st

我正在为我的工作场所构建一系列应用程序,因此我正在尝试创建我自己的打印类,我可以为我的所有应用程序提供参考

问题是,我试图找出一种方法,让应用程序告诉类何时打印页面,但我找不到这样做的方法

例如,到目前为止,我所掌握的是:

    // Report Variables
    private bool bPrinting = false;
    private int iPage = 0;
    private float fOverflow = 0.00F;
    private string sPrintLine = null;
    private Font fontTmpFont = null;
    private PrintPageEventArgs ppeaEv = null;
    private Margins mMargins = new System.Drawing.Printing.Margins(25, 25, 25, 25); // Set wide margins

    // Clear the print line
    public void LineClear()
    {
        sPrintLine = null;
    }

    // Insert a string into the print line at the specified position within the line
    public void LineInsert(string _InsertString, int _InsertPosition)
    {
        if (sPrintLine.Length <= _InsertPosition)
            sPrintLine = sPrintLine.PadLeft(_InsertPosition) + _InsertString;
        else if (sPrintLine.Length <= (_InsertPosition + _InsertString.Length))
            sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString;
        else
            sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString + sPrintLine.Substring(_InsertPosition + _InsertString.Length);
    }

    // Check to see if the line we're trying to print is at the end of the page
    public bool AtEndOfPage()
    {
        return AtEndOfPage(new Font("Courier", 10));
    }
    public bool AtEndOfPage(Font _Font)
    {
        if ((fOverflow + _Font.GetHeight(ppeaEv.Graphics)) > ppeaEv.MarginBounds.Height)
            return true;
        else
            return false;
    }

    // Attempt to print the line
    public void LinePrint()
    {
        LinePrint(null, null);
    }
    public void LinePrint(Font _Font)
    {
        LinePrint(_Font, null);
    }
    public void LinePrint(Font _Font, Brush _Color)
    {
        if (_Font == null)
            _Font = new Font("Courier", 10);
        if (_Color == null)
            _Color = Brushes.Black;

        ppeaEv.Graphics.DrawString(sPrintLine, _Font, _Color,
            ppeaEv.MarginBounds.Left, ppeaEv.MarginBounds.Top + fOverflow,
            new StringFormat()); // 'Draw' line on page
        fOverflow += _Font.GetHeight(ppeaEv.Graphics);
    }

    //  We are done with the report, tell the Print Service to finish up
    public void EndReport()
    {
        ppeaEv.HasMorePages = false;
    }

    //  This is what gets called when the user clicks on 'Print'
    private void Print_Click(object sender, EventArgs e)
    {
        PrintDocument printDocument = new PrintDocument();
        printDocument.DefaultPageSettings.Margins = mMargins;   // Set margins for pages
        printDocument.DefaultPageSettings.Landscape = false;    // Set Portrait mode
        printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
        printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);

        PrintDialog printDialog = new PrintDialog();
        printDialog.Document = printDocument;                   // Set the Document for this print dialog
        printDialog.AllowSomePages = true;                      // Allow the user to select only some pages to print
        printDialog.ShowHelp = true;                            // Allow help button

        DialogResult result = printDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDocument.Print(); // Raises PrintPage event
        }

        printDocument.Dispose();
        printDialog.Dispose();
    }

    void printDocument_BeginPrint(object sender, PrintEventArgs e)
    {
        iPage = 0;
        fOverflow = 0.00F;

    }

    void printDocument_EndPrint(object sender, PrintEventArgs e)
    {

    }
正如您可能知道的,我正在尝试在类外部填充页面,然后让应用程序告诉类何时在显示页面时打印页面

我知道printDocument.PrintPage,但这似乎不是我需要的;它在方法内部构建页面,然后打印它。我不会在这个方法中构建打印页面。 我还尝试在一个页面上允许使用多种字体

有办法做到这一点吗

提前谢谢大家,,
Robert

我认为您认为使用PrintPage事件的局限性是不对的。与绘制事件一样,它是打印的起点,但如果您传递其参数,尤其是图形对象,则可以让任意数量的通用方法创建输出。-真正的挑战是定义一种格式,让您的所有应用程序都可以准备和提供其数据及其格式,包括字体、段落格式以及打印应用程序不需要的内容你可以先设计一个类PrintAction来保存这些要收集的东西…在一个从PrintPage事件处理的列表中..我不确定我是否完全理解你-:对不起-我正在尝试,除非我误解了PrintPage事件,这是很有可能的,当它准备好填充页面时,它会被提升。然后,“printDocument\u PrintPage”方法填充页面,然后打印页面。我需要相反的东西;我需要在页面已填充时告诉打印事件,因为这是使用“LinePrint”方法完成的,然后再打印。等等-你是说我应该首先在一个包含字体、格式等的列表中创建页面,然后在完成后调用Print类?如果是这样的话,那么我有点失望,因为我希望这门课能比这门课更直接。是的,有点。您可以准备在列表中打印所需的所有数据,然后让printpage处理这样的列表。这也是在屏幕绘图中所做的,尽管原因不同:每当屏幕需要更新时,绘图必须重复进行。并行的是,对于这两种情况,实际的图形数据必须存储在paint/printpage方法可以找到并处理它们的地方。。