不使用mfc框架和文档视图支持的情况下如何进行打印?我是第一次做打印。很多问题不是太明白。
如何获得打印机的dc?用CPrintDialog类的成员函数可否实现?CreatePrinterDC()和GetPrinterDC()可以么?在那种情况下使用?
CChildView类有没有支持打印的相关函数?
请各位高手指点.分数继续加.

解决方案 »

  1.   

    可以直接使用API调用。
    PrintDlg(),StartDoc(),EndDoc()...
      

  2.   

    void CDlgkk::DY()
    {
     CDC *pDC;   //pDC
       
    //打印机的初始化设置
    CPrintDialog dlg(FALSE,PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,this); 
       
    CRuixingZJApp* pApp = static_cast<CRuixingZJApp*>( ::AfxGetApp() );
    long Len;
    WORD nWord = 0;
    HANDLE hPrinter;

    DEVMODE* pDevMode;
    DEVNAMES* lpDevNames;
    LPTSTR lpszDeviceName;

        ///////////////////////////////////////////////////////////////////
        if(pApp->GetPrinterDeviceDefaults(&dlg.m_pd) == 0)
    {
    ::AfxMessageBox("错误!当前没有连接打印机!",MB_OK);
    return;
    }
    pDevMode = (DEVMODE*)GlobalLock(dlg.m_pd.hDevMode);
    lpDevNames = (LPDEVNAMES)GlobalLock(dlg.m_pd.hDevNames);
    lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset; if (::OpenPrinter((char *) pDevMode->dmDeviceName, &hPrinter, NULL) == FALSE)
    return;
    Len = DocumentProperties(NULL, NULL, lpszDeviceName, pDevMode, NULL, 0);
    pDevMode->dmPaperSize = DMPAPER_A4;
    lstrcpy((char *)pDevMode->dmFormName,"A4 210 *297 mm");    Len = DocumentProperties(NULL, hPrinter, lpszDeviceName, NULL, pDevMode, DM_IN_BUFFER);
    GlobalUnlock(dlg.m_pd.hDevMode);
    GlobalUnlock(dlg.m_pd.hDevNames); ////////////////////////////////////////////////////////////////////////////////
        if (dlg.CreatePrinterDC() == NULL) 
    return ;
    if (dlg.DoModal() == IDOK)
    {   
    m_pPrintDC.Attach(dlg.m_pd.hDC); 
    if(m_pPrintDC.GetSafeHdc() == NULL)
    {
    ::AfxMessageBox("当前没有连接打印机!", MB_OK);
    return;
    } pDC = &m_pPrintDC;
    ASSERT_VALID( pDC ); DOCINFO di; 
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "微机监测实时数据表格";
    di.lpszOutput = NULL; 
    di.lpszDatatype = NULL;
    di.fwType = 0;


    //取打印机的横方向和纵方向的分辨率
    //即每英寸点数
    cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
    cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
    //取打印纸张高度和宽度
    nPageHeight = pDC->GetDeviceCaps(VERTRES);
    nPageWidth = pDC->GetDeviceCaps(HORZRES); m_CharWidth = (long)((float)(nPageWidth*37/40)/(float)80);
    m_LineHeight = (long)((float)(nPageHeight*19/20)/(float)80); //==========================================
    CFont font,*oldFont,titleFont; //设定新字体
    m_lf.lfWidth = m_CharWidth;
    m_lf.lfHeight = m_LineHeight;
    m_lf.lfCharSet = GB2312_CHARSET;
    m_lf.lfOutPrecision = 3;
    m_lf.lfClipPrecision = 2;
    m_lf.lfQuality = PROOF_QUALITY;
    m_lf.lfPitchAndFamily = 49;
    m_lf.lfEscapement = 0;
    m_lf.lfOrientation = 0;
    m_lf.lfUnderline = 0;
    m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_ROMAN;
    font.CreateFont(m_lf.lfHeight,
    m_lf.lfWidth,
    m_lf.lfEscapement,
    m_lf.lfOrientation,
    FW_NORMAL,
    FALSE,
    m_lf.lfUnderline,
    0,
    m_lf.lfCharSet,
    m_lf.lfOutPrecision,
    m_lf.lfClipPrecision,
    m_lf.lfQuality,
    m_lf.lfPitchAndFamily,
    "宋体");
    LOGFONT curLogFont, newLogFont;

    //取当前字体
    font.GetLogFont( &curLogFont );
    newLogFont = curLogFont; //保存旧字体
    if (!titleFont.CreateFontIndirect(&newLogFont))
    return;
    oldFont = pDC->SelectObject(&titleFont);
            
    int  Height = 400;   
    int  ye = 1;//第一页
            
    if (pDC->StartDoc(&di) > 0)
    {
    //打印整页
    while (ye < m_ye)
    {
    pDC->StartPage();
    pDC->MoveTo(nPageWidth/20, m_LineHeight);
    DrawTable(pDC, Height, ye, 35);
    pDC->EndPage();
    ye = ye + 1;
    }

    //没有数据
    if ((m_ye == 1)&&(m_other == 0))
    {return;}

    //打印剩下的数据或不为一页的数据
    if (((m_ye == 1)&&(m_other > 0))||(ye == m_ye)) 
    {
    pDC->StartPage();
    pDC->MoveTo(nPageWidth/20, m_LineHeight);
    DrawTable(pDC, Height, ye, m_other);
    pDC->EndPage();
    }
                pDC->EndDoc();
    }
    pDC->SelectObject(oldFont);
    m_pPrintDC.Detach();
    }
    }一个例子,看看吧
    希望对你有所帮助
      

  3.   

    有这样一段代码,编译执行都没问题,就是打印机没有反应.但是如果不连接打印机,就会提示没有连接打印机.//////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    void CDlgDlg::OnOK() 
    {
    // TODO: Add extra validation here
    CPrintDialog dlgPrint(false,PD_ALLPAGES,this); //
    if(dlgPrint.DoModal()==IDOK)
    {
    CDC dcPrint;
    dcPrint.Attach(dlgPrint.GetPrinterDC()); //creat and fill DOCINFO struct
    DOCINFO myPrintJob;
    myPrintJob.cbSize=sizeof(myPrintJob);
    myPrintJob.lpszDocName="myPrintJob";
    myPrintJob.lpszOutput=NULL;
    myPrintJob.lpszDatatype=NULL;
    myPrintJob.fwType=NULL;
    //start printing the document
    if(dcPrint.StartDoc(&myPrintJob)>=0)
    {
    //**start page
    dcPrint.StartPage(); //start drawing
    dcPrint.TextOut(0,0,"我要打印,靠.Fuck"); //throw the page
    dcPrint.EndPage(); //close the document
    dcPrint.EndDoc();
    }
    //delete the printer device context
    dcPrint.DeleteDC();
    }
    CDialog::OnOK();
    }