0

我正在尝试在水平或 x 位置大于计数器时添加页面以保持右侧边距。当我运行代码时,我最终陷入了数百个页面的无限循环,所有页面都显示相同的第一页图形。认为这可能与我对 HasMorePages 缺乏了解有关。我可以使用一些帮助。谢谢。

    public static class PrintWave
{
    public static void PrintPreWave()
    {

        PrintDocument pd = new PrintDocument();

        if (WaveTools.MySettings == null)
        {
            pd.DefaultPageSettings.Landscape = true;
        }
        else
        {
            pd.DefaultPageSettings = WaveTools.MySettings;
        }
        pd.OriginAtMargins = true;

        pd.PrintPage += new PrintPageEventHandler(OnPrintPage);
        PrintDialog dlg = new PrintDialog();
        PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog();
        printPreviewDlg.Document = pd;

        Form p = (Form)printPreviewDlg;
        p.WindowState = FormWindowState.Maximized;
        printPreviewDlg.ShowDialog();


    }

    private static void OnPrintPage(object sender, PrintPageEventArgs e)
    {
        string MyTag = string.Empty;
        MyTag = WaveActions.ActiveId;
        Wave MyWave = WaveHolder.FindWave(MyTag);
        int MyCount = 0;

        int xOffset = e.MarginBounds.Location.X;

        int yOffset = e.MarginBounds.Location.Y;


        if (MyWave != null)
        {
            Graphics g = e.Graphics;
            g.SetClip(e.PageBounds);

            Pen MyPen = new Pen(WaveTools.WaveColor, WaveTools.PenWidth);

            float dx = (float)e.PageBounds.Width / MyWave.NumSamples;
            float dy = (float)e.PageBounds.Height / 255;


            if (MyWave.Normal == false)
             {
               g.ScaleTransform(dx, dy);

            }



            for (int i = 0; i < MyWave.NumSamples - 1; i++)
            {





                g.DrawLine(MyPen, i, MyWave.Data[i], i + 1, MyWave.Data[i + 1]);
                MyCount = MyCount + 1;

                if (MyCount > e.MarginBounds.Width)
                {

                    e.HasMorePages = true;
                    MyCount = 0; 
                    return;
                }
                else
                {

                    e.HasMorePages = false;
                    return;

                }


            }


        }


    }

}

}

4

1 回答 1

1
       for (int i = 0; i < MyWave.NumSamples - 1; i++)

这是核心问题陈述,每次调用 PrintPage 时都从 0 开始。您需要从上一页中断的地方继续。使i变量成为类的字段而不是局部变量。实现 BeginPrint 事件以将其设置为零。

需要删除循环内的 else 子句。

于 2012-04-21T21:50:16.800 回答