1

嗨,我从 MSDN 获得了此代码。我无法弄清楚问题所在,希望您能提供帮助。错误位于带有 printForm1 字样的两行中。我的表单实际上称为 Form1,所以我看不到问题所在。

  [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
        Graphics mygraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
    }

    private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

    private void btnCreate_Click(object sender, EventArgs e)
    {
        btncustCont.Visible = false; 
        btnprintCost.Visible = false;
        btnpdfCont.Visible = false;
        btnpdfBrochureCont.Visible = false;
        btnpaperCont.Visible = false;
        btnduplicateCont.Visible = false;
        btntypeCont.Visible = false;
        btnCalcCost.Visible = false;
        btnprintCost.Visible = false;

        Application.DoEvents();
        CaptureScreen();
        printForm1.PrinterSettings.DefaultPageSettings.Landscape = true;
        printForm1.Print();

        btncustCont.Visible = true; 
        btnprintCost.Visible = true;
        btnpdfCont.Visible = true;
        btnpdfBrochureCont.Visible = true;
        btnpaperCont.Visible = true;
        btnduplicateCont.Visible = true;
        btntypeCont.Visible = true;
        btnCalcCost.Visible = true;
        btnprintCost.Visible = true;

    }
4

2 回答 2

1

看看这篇 MSDN 文章如何在类的顶部声明 printDocument1:

private PrintDocument printDocument1 = new PrintDocument();

http://msdn.microsoft.com/en-us/library/vstudio/6he9hz8c(v=vs.100).aspx

你想做同样的事情,只需要 printForm1

于 2013-08-28T19:37:37.820 回答
0

我能想到的两种可能:

如果我没看错的话,printForm1是 PrintDocument 对象的一个​​实例。抱歉,如果我在这里没有给你足够的信任,但你确定 printForm1 实际上是在任何地方声明的吗?

否则,请确保您的 (codeBehind).cs 文件实际上继承了包含此元素的页面。

于 2013-08-28T19:37:21.507 回答