错误:'当前上下文中不存在名称printForm1'为什么会这样?

时间:2013-08-28 19:32:33

标签: c#

您好我从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;

    }

2 个答案:

答案 0 :(得分:1)

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

private PrintDocument printDocument1 = new PrintDocument();

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

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

答案 1 :(得分:0)

我能想到的两种可能性:

如果我正确阅读,printForm1是PrintDocument对象的实例。对不起,如果我没有给你足够的信用,但你确定printForm1实际上是在任何地方宣布的吗?

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