• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 如何在VC++中使用API直接打印

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    (說明:)這一段代碼,用以演示《如何在VC++中使用API直接打
    (說明:)這一段代碼,用以演示《如何在VC++中使用API直接打印》。并且該段代碼可以直接嵌入各種工程中,有實際使用的價值。

    (用途:)在Visual C++中,應用程序通常是使用CView中提供的打印功能,在OnPrint()或OnDraw()中向打印機輸出。但是對于對話框中的數據,或基于對話框的程序,打印成了一件繁瑣的工作。

    該段代碼向用戶提供了PrintListCtrl()函數,用于打印用戶在對話框或FormView中的CListCtrl(控件必須是Report View 形式的)控件中的內容。在打印過程中,根據控件中每列標題的寬度計算打印輸出時各列的寬度,并根據數據的行數自動分頁。在本代碼的基礎上稍作修改,就可以適應各種數據的輸出。

    (用法:)該段代碼使用Visual C++ 6.0, 使用Windows API來完成所需功能,使用時將本文本作為頭文件使用。打印時直接調用PrintListCtrl(),函數的參數為所要打印的ListCtrl。?聯系方法:lff@mail.wl.xj.cn

    *///該結構用于存儲各列的信息
    typedef struct tagColAtt
    {
    int nColIndex;
    CString strColText;
    int nPrintX;
    int nSubItemIndex;
    }
    COLATT;

    BOOL PrintListCtrl(CListCtrl &list)
    {
    PRINTDLG pd;
    pd.lStructSize = sizeof(PRINTDLG);
    pd.Flags = PD_RETURNDC;
    pd.hDC = NULL;
    pd.hwndOwner = NULL;
    pd.hInstance = NULL;
    pd.nMaxPage = 1;
    pd.nMinPage = 1;
    pd.nFromPage = 1;
    pd.nToPage = 1;
    pd.nCopies = 1;
    pd.hDevMode = NULL;
    pd.hDevNames = NULL;
    //顯示打印對話框,由用戶來設定紙張大小等。
    if(!PrintDlg(&pd)) return FALSE;
    ASSERT(pd.hDC!=NULL);
    int nHorRes = GetDeviceCaps(pd.hDC, HORZRES);
    int nVerRes = GetDeviceCaps(pd.hDC, VERTRES);
    int nXMargin = 2;
    int nYMargin = 2;
    TEXTMETRIC tm;
    GetTextMetrics(pd.hDC, &tm);
    int nCharHeight = tm.tmHeight;
    int nCharWidth = tm.tmAveCharWidth;
    CHeaderCtrl* pHeader = list.GetHeaderCtrl();
    //獲得行,列的個數
    int nColCount = pHeader->GetItemCount();
    int nLineCount = list.GetItemCount();
    int ColOrderArray[100];
    COLATT ca[100];
    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_TEXT|LVCF_SUBITEM;
    lvc.pszText = text;
    lvc.clearcase/" target="_blank" >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);
    if(nColX > nHorRes)
    {
    DeleteDC(pd.hDC);
    AfxMessageBox("字段太多,無法在一行內打印,請試用較大的紙,或橫向打印。");
    return FALSE;
    }
    }
    DOCINFO di;
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "ListCtrl Data Printing";
    di.lpszOutput = (LPTSTR) NULL;
    di.lpszDatatype = (LPTSTR) NULL;
    di.fwType = 0;
    StartDoc(pd.hDC, &di);
    StartPage(pd.hDC);
    //調整各列的寬度,以使各列在后面的打印輸出時更均勻的打印在紙上。
    int space = (nHorRes-nXMargin*nCharWidth-nColX) / (nColCount -1);
    for(i =1; i<nColCount; i++)
    {
    ca[i].nPrintX += i*space;
    }
    //輸出列標題
    for(i =0; i<nColCount; i++)
    TextOut(pd.hDC, ca[i].nPrintX, nYMargin,
    ca[i].strColText, strlen(ca[i].strColText));
    int nMaxLinePerPage = nVerRes/nCharHeight -3;
    int nCurPage =1;
    //輸出各列的數據
    for(i =0; i<nLineCount; i++)
    {
    for(int j =0; j<nColCount; j++)
    {
    if(i+1-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage)
    {
    //新的一頁
    EndPage(pd.hDC);
    StartPage(pd.hDC);
    nCurPage ++;
    }
    CString subitem = list.GetItemText(i, ca[j].nSubItemIndex);
    TextOut(pd.hDC, ca[j].nPrintX,nYMargin+(i+1-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,subitem, strlen(subitem));
    }
    }
    EndPage(pd.hDC);
    EndDoc(pd.hDC);
    //打印結束
    DeleteDC(pd.hDC);
    return TRUE;

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>