怎么让打印输出如下这样打印格式,http://my.nbip.net/homepage/design/dy.htm我是想从白色的最左上角开始打印,请问怎么定坐标和设置自定义打印页面大小,有没有示例的代码,谢谢
说明:
纸张大小(464*630像素)
打印区域(390*503像素)
页边距:上76 ;下51 ;左54 ;右20 ;////////////给高手送分/////////给高手送分/////////给高手送分/////////

解决方案 »

  1.   

    你真有意思,谢谢,在线等待ING:P
      

  2.   

    打印就象做图一样,使用DC就行,在指定的区域输出文字就行呀BOOL CDiskInfoView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    BOOL bResult;
    CWinApp* pApp = AfxGetApp(); // ask our app what the default printer is
    // if there isn't any, punt to MFC so it will generate an error if (!pApp->GetPrinterDeviceDefaults(&pInfo->m_pPD->m_pd) ||
     pInfo->m_pPD->m_pd.hDevMode == NULL)
    return DoPreparePrinting(pInfo); HGLOBAL hDevMode = pInfo->m_pPD->m_pd.hDevMode;
    HGLOBAL hDevNames = pInfo->m_pPD->m_pd.hDevNames; DEVMODE* pDevMode = (DEVMODE*) ::GlobalLock(hDevMode);
    DEVNAMES* pDevNames = (DEVNAMES*) ::GlobalLock(hDevNames); LPCSTR pstrDriverName = ((LPCSTR) pDevNames)+pDevNames->wDriverOffset;
    LPCSTR pstrDeviceName = ((LPCSTR) pDevNames)+pDevNames->wDeviceOffset;
    LPCSTR pstrOutputPort = ((LPCSTR) pDevNames)+pDevNames->wOutputOffset; CDC dcPrinter;
    if (dcPrinter.CreateDC(pstrDriverName, pstrDeviceName, pstrOutputPort, NULL))
    {
    int nPages;
    int nRet = GetListCtrl().GetItemCount();
    if (nRet <= 37)
    {
    nPages = 1;
    }
    else
    {
    nRet -= 37;
    if (nRet % 40 == 0)
    {
    nPages = nRet / 40 +1;
    }
    else
    {
    nPages = nRet / 40 +2;
    }
    }

    pInfo->SetMinPage(1);
    pInfo->SetMaxPage(nPages);
    dcPrinter.DeleteDC();
    bResult = DoPreparePrinting(pInfo);
    }
    else
    {
    MessageBox("Could not create printer DC");
    bResult = FALSE;
    } ::GlobalUnlock(hDevMode);
    ::GlobalUnlock(hDevNames); return bResult;
    }void CDiskInfoView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
    {
    if(pDC->IsPrinting()) 

    LPDEVMODE  pDevMode; 
    pDevMode=pInfo->m_pPD->GetDevMode(); 
    pDevMode->dmOrientation = DMORIENT_LANDSCAPE; 
    pDC->ResetDC(pDevMode); 
    }  CListCtrl &list = GetListCtrl();
    nHorRes = 1000;//pDC->GetDeviceCaps(HORZRES);
    nVerRes = 740;//pDC->GetDeviceCaps(VERTRES);
    nXMargin = 2;
    nYMargin = 2;
    m_logfont.lfHeight = 15;
    m_logfont.lfPitchAndFamily = FIXED_PITCH; m_pFont = new CFont;
    m_pFont->CreateFontIndirect(&m_logfont);
    m_pPrintFont = NULL;
    m_pPrintFont = pDC->SelectObject(m_pFont);
    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    nCharHeight = tm.tmHeight;
    int nCharWidth = tm.tmAveCharWidth;

    CHeaderCtrl* pHeader = list.GetHeaderCtrl();
    //获得行,列的个数
    nColCount = pHeader->GetItemCount();
    nLineCount = list.GetItemCount();

    list.GetColumnOrderArray(ColOrderArray, nColCount);
    int nColX =nXMargin*nCharWidth; //检索各列的信息,确定列标题的内容长度。
    for(int i =0 ; i< nColCount; i++)
    {
    ca[i].nColIndex = ColOrderArray[i];
    LVCOLUMN lvc;
    char text[100];
    lvc.mask = LVCF_WIDTH|LVCF_TEXT;
    lvc.pszText = text;
    lvc.cchTextMax = 100;
    list.GetColumn(ca[i].nColIndex, &lvc);
    ca[i].strColText = lvc.pszText;
    ca[i].nSubItemIndex = lvc.iSubItem;
    ca[i].nPrintX = nColX;
    nColX += nCharWidth * strlen(ca[i].strColText);
    } DOCINFO di;
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "ListCtrl Data Printing"; 
    di.lpszOutput = (LPTSTR) NULL; 
    di.lpszDatatype = (LPTSTR) NULL; 
    di.fwType = 0; 
    //调整各列的宽度,以使各列在后面的打印输出时更均匀的打印在纸上。
    int space = (nHorRes-nXMargin*nCharWidth-nColX) / (nColCount -1); for(i =1; i<nColCount; i++)
    {
    ca[i].nPrintX += i*space;
    }
    ca[1].nPrintX -= 20;
    ca[2].nPrintX -= 20;
    ca[3].nPrintX += 40;
    ca[4].nPrintX += 20;
    }
      

  3.   

    void CDiskInfoView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
    {
    CListView::OnPrepareDC(pDC, pInfo); pDC->SetMapMode(MM_ANISOTROPIC);  //转换坐标映射方式
    CSize size = CSize(800, 560);
    pDC->SetWindowExt(size);  //确定窗口大小

    //得到实际设备每逻辑英寸的象素数量
    int xLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSX); 
    int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);

    //得到设备坐标和逻辑坐标的比例
    long xExt = (long)size.cx * xLogPixPerInch/96 ;
    long yExt = (long)size.cy * yLogPixPerInch/96 ;
    pDC->SetViewportExt((int)xExt, (int)yExt);
    //确定视口大小
    }void CDiskInfoView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
    {
    CListCtrl &list = GetListCtrl(); int nMaxLinePerPage;// = nVerRes/nCharHeight - 3;
    int nY = 10;
    //输出各列的数据
    nCurPage = pInfo->m_nCurPage;
    if (nCurPage == 1)
    {
    PrintTitlePage(pDC, pInfo);
    nY += 53;
    nMaxLinePerPage = 37;
    m_pPrintListID = 0; //输出列标题
    for(int i =0; i<nColCount; i++)
    pDC->TextOut(ca[i].nPrintX, nY, 
    ca[i].strColText, strlen(ca[i].strColText)); nY += nCharHeight + 1;
    pDC->MoveTo(0, nY);
    pDC->LineTo(nHorRes * 2, nY);
    nY += 2;
    }
    else
    {
    nMaxLinePerPage = 40;
    m_pPrintListID = 37 + (nCurPage - 2) * nMaxLinePerPage; //输出列标题
    for(int i =0; i<nColCount; i++)
    pDC->TextOut(ca[i].nPrintX, nY, 
    ca[i].strColText, strlen(ca[i].strColText));
    nY += nCharHeight + 2;
    pDC->MoveTo(0, nY);
    pDC->LineTo(nHorRes * 2, nY);
    nY +=  2;
    } for(int i =m_pPrintListID; i<nLineCount; i++)
    {
    if (nCurPage == 1)
    {
    if (i >= nMaxLinePerPage)
    break;
    }
    else
    {
    if (i >= ((nCurPage - 1) * nMaxLinePerPage + 37))
    break;
    } for(int j =0; j<nColCount; j++)
    {
    CString subitem = list.GetItemText(i, ca[j].nColIndex);
    pDC->TextOut(ca[j].nPrintX, 
    nY, 
    subitem, strlen(subitem));
    }
    nY += nCharHeight + 2;
    } if (nCurPage == 1)
    {
    pDC->MoveTo(0, 
    nY+5);
    pDC->LineTo(nHorRes * 2, 
    nY+5);
    }
    else
    {
    pDC->MoveTo(0, 
    nY + 5);
    pDC->LineTo(nHorRes * 2, 
    nY + 5);
    }
    nY += 5; LOGFONT logFont;
    memset(&logFont, 0, sizeof(LOGFONT)); logFont.lfHeight = 12; // 3/4th inch high in MM_LOENGLISH
    // (1/100th inch)
    CFont font;
    CFont* pOldFont = NULL;
    if (font.CreateFontIndirect(&logFont))
    pOldFont = pDC->SelectObject(&font);
    // Get the file name, to be displayed on title page
    CString strInfo = _T(""), tmp;

    LoadString(NULL, IDS_NOTIFY, tmp.GetBuffer(200), 200);
    tmp.ReleaseBuffer();
    strInfo += tmp + "        "; LoadString(NULL, IDS_COPYRIGHT, tmp.GetBuffer(200), 200);
    tmp.ReleaseBuffer();
    strInfo += tmp + "        "; LoadString(NULL, IDS_EMAIL, tmp.GetBuffer(200), 200);
    tmp.ReleaseBuffer();
    strInfo += tmp + "        "; LoadString(NULL, IDS_CONTANCT, tmp.GetBuffer(200), 200);
    tmp.ReleaseBuffer();
    strInfo += tmp;
    // Display the file name 1 inch below top of the page,
    // centered horizontally int nX;
    CSize size = pDC->GetTextExtent(strInfo);
    nX = nHorRes - 50 - size.cx;
    pDC->TextOut(nX, 
    nY + 10,
    strInfo); strInfo.Format("第 %d页", nCurPage);
    size = pDC->GetTextExtent(strInfo);
    nX = nHorRes + 50 - size.cx;
    nY = 740;
    pDC->TextOut(nX, 
    nY,
    strInfo);
    if (pOldFont != NULL)
    pDC->SelectObject(pOldFont);
    nY = 0;
    }void CDiskInfoView::PrintTitlePage(CDC *pDC, CPrintInfo *pInfo)
    {
    LOGFONT logFont;
    memset(&logFont, 0, sizeof(LOGFONT)); logFont.lfHeight = 40; // 3/4th inch high in MM_LOENGLISH
    logFont.lfWeight = FW_BOLD;
    // (1/100th inch)
    CFont font;
    CFont* pOldFont = NULL;
    if (font.CreateFontIndirect(&logFont))
    pOldFont = pDC->SelectObject(&font);
    // Get the file name, to be displayed on title page
    CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
    CString strPageTitle = pFrame->GetSoftCopyName();
    CSize pTitleSize = pDC->GetTextExtent(strPageTitle); // Display the file name 1 inch below top of the page,
    // centered horizontally
    pDC->TextOut(nXMargin + 50, nYMargin, strPageTitle); if (pOldFont != NULL)
    pDC->SelectObject(pOldFont); memset(&logFont, 0, sizeof(LOGFONT)); logFont.lfHeight = 12;
    // (1/100th inch)
    CFont fontInfo;
    pOldFont = NULL;
    if (fontInfo.CreateFontIndirect(&logFont))
    pOldFont = pDC->SelectObject(&fontInfo); strPageTitle = pFrame->GetPrintfInfo();
    CSize pInfoSize = pDC->GetTextExtent(strPageTitle);
    int nX = nHorRes - pInfoSize.cx;
    int nY = nYMargin + pTitleSize.cy + 2; pDC->TextOut(nX, nY, strPageTitle);
    if (pOldFont != NULL)
    pDC->SelectObject(pOldFont);
    }
      

  4.   

    int x=GetDeviceCaps (hdcPrn, HORZRES); //像素 
    int y=GetDeviceCaps (hdcPrn, VERTRES);//计算打印尺寸,如打印区域 x=x-左54 -右20 ;
    //纸张大小(464*630像素)
    //打印区域(390*503像素)
    //页边距:上76 ;下51 ;左54 ;右20 ;if (StartDoc (hdcPrn, &di) > 0){
      if (StartPage (hdcPrn) > 0){
        PrintData(hdcPrn);//打印
          if (EndPage (hdcPrn) > 0)
    EndDoc (hdcPrn) ;
       }
    }