我用list视图创建了一个SDI程序,往里面添加了一些项后,想打印出来,可是按"文件"->"打印预览"后却发现是一片空白,请问怎样才能"所见即所得"呢?

解决方案 »

  1.   

    是的。网上有个ListCtrl的打印程序,可以找找。我也正好做过一个,需要可以提供。
      

  2.   


    int PrintListCtrl(CDC *pDC, CListCtrl *pPrintList, int nPage, CRect *prcPrint)
    {
        static CArray<int, int> aPageItem;
        if (!pDC)
        {
            aPageItem.RemoveAll();
            aPageItem.Add(0);
            return 0;
        }    CFont *pFont = pPrintList->GetFont();
        LOGFONT lgFont;
        pFont->GetLogFont(&lgFont);
        //lstrcpy(lgFont.lfFaceName, _T("黑体"));
        lgFont.lfWeight = FW_BOLD;
        pFont = new CFont;
        pFont->CreateFontIndirect(&lgFont);    CFont *pFontOld = pDC->SelectObject(pFont);
        CPoint ptOrg = pDC->GetWindowOrg();    CHeaderCtrl *pHeader = pPrintList->GetHeaderCtrl();
        TEXTMETRIC /*tmCtl, */tmDC;
        //pPrintList->GetDC()->GetTextMetrics(&tmCtl);
        pDC->GetTextMetrics(&tmDC);
        int nWidth = 0, nMargin = tmDC.tmHeight, nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin;
        //double dRatio = (double)tmDC.tmHeight / (double)tmCtl.tmHeight;    HDITEM hiItem = {0};
        TCHAR  lpBuffer[256] = {0};
        hiItem.mask = HDI_TEXT | HDI_WIDTH;
        hiItem.pszText = lpBuffer;
        hiItem.cchTextMax = 250;
        int nColumn = pHeader->GetItemCount();
        CRect *prcItem = new CRect[nColumn];
        CString *pstrItem = new CString[nColumn];
        for (int i = 0; i < nColumn; i++)
        {
            pHeader->GetItem(i, &hiItem);
            prcItem[i].SetRect(nWidth, 0, nWidth + hiItem.cxy, tmDC.tmHeight);
            nWidth += hiItem.cxy;
            pstrItem[i] = hiItem.pszText;
            prcItem[i].bottom = prcItem[i].top +
                pDC->DrawText(pstrItem[i], prcItem[i], DT_CENTER | DT_WORDBREAK | DT_CALCRECT);
            prcItem[i].right = prcItem[i].left + hiItem.cxy;
            if (prcItem[i].Height() > nHeight)
                nHeight = prcItem[i].Height() + nMargin;
        }
        CSize szArea = pDC->GetWindowExt();
        int nWidthArea = abs(szArea.cx);
        if (nWidth > nWidthArea)
        {
            double dRatioWidth = (double)szArea.cx / (double)nWidth;
            prcItem[0].right = prcItem[0].left + int(prcItem[0].Width() * dRatioWidth);
            nWidth = prcItem[0].Width();
            for (int i = 1; i < nColumn; i++)
            {
                prcItem[i].right = prcItem[i - 1].right + int(prcItem[i].Width() * dRatioWidth);
                prcItem[i].left = prcItem[i - 1].right;
                nWidth += prcItem[i].Width();
            }
        }
        pDC->SetWindowOrg((nWidth - nWidthArea) / 2, ptOrg.y);
        for (int iCol = 0; iCol < nColumn; iCol++)
        {
            CRect rcBorder(prcItem[iCol]);
            rcBorder.bottom = rcBorder.top + nHeight;
            pDC->Rectangle(rcBorder);
            prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2);
            pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK);
        }
        pDC->SelectObject(pFontOld);
        pFont->DeleteObject();
        delete pFont;    pDC->GetTextMetrics(&tmDC);
        nMargin = tmDC.tmHeight;
        nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin;    int nTop = nHeight;
        int iRow = 0;
        for (iRow = aPageItem[nPage - 1]; iRow < pPrintList->GetItemCount(); iRow++)
        {
            nHeight = tmDC.tmHeight + nMargin;
            for (int iCol = 0; iCol < nColumn; iCol++)
            {
                pstrItem[iCol] = pPrintList->GetItemText(iRow, iCol);
                prcItem[iCol].top = nTop;
                prcItem[iCol].bottom = nTop + tmDC.tmHeight;
                nWidth = prcItem[iCol].Width();
                prcItem[iCol].bottom = prcItem[iCol].top +
                    pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK | DT_CALCRECT);
                prcItem[iCol].right = prcItem[iCol].left + nWidth;
                if (prcItem[iCol].Height() > nHeight)
                    nHeight = prcItem[iCol].Height() + nMargin;
            }
            nTop += nHeight;
            nWidth = prcItem[iCol - 1].right;        for (int iCol = 0; iCol < nColumn; iCol++)
            {
                CRect rcBorder(prcItem[iCol]);
                rcBorder.bottom = rcBorder.top + nHeight;
                pDC->Rectangle(rcBorder);
                prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2);
                pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK);
            }
            if (nTop + ptOrg.y - pDC->GetWindowOrg().y > szArea.cy - 160)
                break;
        }
        aPageItem.Add(iRow);    delete []pstrItem;
        delete []prcItem;    if (prcPrint)
            prcPrint->SetRect(ptOrg.x - pDC->GetWindowOrg().x, ptOrg.y - pDC->GetWindowOrg().y,
                nWidth + ptOrg.x - pDC->GetWindowOrg().x, nTop + ptOrg.y - pDC->GetWindowOrg().y);    pDC->SetWindowOrg(ptOrg);    return iRow;
    }
      

  3.   

    上面这段程序是不是要在classwizard里面添加的?
      

  4.   

    本人也是這個問題..建立了個SDI程序,..建工程時,已經加入了打印功能,
    我加入個畫圖事件,每次畫的圖都不一樣, 可是在我打印預覽時,是空白,---我又將畫圖的代碼加到OnDraw(CDC* pDC)中去,印預覽時,也是空白,......
      

  5.   

    PrintListCtrl是一个全局函数。当然也可以作为成员函数使用。