首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:‘名称printForm1在当前上下文中不存在’为什么会这样?

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

Stack Overflow用户
提问于 2013-08-28 19:32:33
回答 2查看 743关注 0票数 1

嗨,我从MSDN收到这段代码。我找不出问题,希望你能帮上忙。错误的位置是带有单词printForm1的两行。我的表单实际上被称为Form1,所以我看不到问题。

代码语言:javascript
复制
  [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;

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-28 19:37:37

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

代码语言:javascript
复制
private PrintDocument printDocument1 = new PrintDocument();

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

你也会想做同样的事,只想用printForm1

票数 1
EN

Stack Overflow用户

发布于 2013-08-28 19:37:21

我能想到两种可能性:

如果我读得对,printForm1是PrintDocument对象的一个实例。对不起,如果我在这里没有给你足够的信用,但你确定printForm1实际上是在任何地方申报的吗?

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18496698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档