求一份源代码,就像acdsee中那样的,好的再加100分,决不失言!思路也可,谢谢了!

解决方案 »

  1.   

    void Draw(HDC hDC,HBITMAP hBmp,double dScaleX,double dScaleY,int iX,int iY,int iWidth=0,int iLength=0)
    {
    HPALETTE hPal;
    BITMAP bm;
    BITMAPINFOHEADER bi;
    LPBITMAPINFOHEADER  lpbi;
    DWORD dwLen;
    HANDLE hDIB;
    HANDLE handle;
    HDC  hDC1;
    if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE )
    {
    UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
    LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
    pLP->palVersion = 0x300;
    pLP->palNumEntries =GetSystemPaletteEntries( hDC, 0, 255, pLP->palPalEntry );
    hPal=CreatePalette(pLP );
    delete[] pLP;
    }
    if (hPal==NULL) hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
    ::GetObject(hBmp,sizeof(bm),(LPSTR)&bm);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight  = bm.bmHeight;
    bi.biPlanes  = 1;
    bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    int nColors = (1 << bi.biBitCount);
    if( nColors > 256 )
    nColors = 0;
    dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
    hDC1 = ::GetDC(NULL);
    hPal = SelectPalette(hDC1,hPal,FALSE);
    RealizePalette(hDC1);
    hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
    if (!hDIB)
    {
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    *lpbi = bi;
    ::GetDIBits(hDC1, hBmp, 0L, (DWORD)bi.biHeight,
    (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
    bi = *lpbi;
    if (bi.biSizeImage == 0)
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)* bi.biHeight;
    dwLen += bi.biSizeImage;
    if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    hDIB = handle;
    else
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    BOOL bGotBits = GetDIBits( hDC1, hBmp,0L,(DWORD)bi.biHeight,(LPBYTE)lpbi+ (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);
    if( !bGotBits )
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return;
    }
    if(iWidth==0||iLength==0)
    {
    iWidth=lpbi->biWidth;
    iLength=lpbi->biHeight;
    iWidth=(int)(dScaleX*iWidth);
    iLength=(int)(iLength*dScaleY);
    }
    StretchDIBits(hDC,iX,iY,iWidth,iLength,0,0,lpbi->biWidth,lpbi->biHeight,(LPBYTE)lpbi  // address for bitmap bits
    + (bi.biSize + nColors * sizeof(RGBQUAD)),(LPBITMAPINFO)lpbi,DIB_RGB_COLORS,SRCCOPY);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hDIB);
    DeleteObject(hPal);
    }
      

  2.   

    void CSGVView::OnPrintBitmap()
    {   
    CDC memDC;
    CClientDC dc(this);

    int bmpWidth = 1600;
    int bmpHeight = 500;
    memDC.CreateCompatibleDC( &dc );
    CBitmap * bitmap = new CBitmap();
    bitmap->CreateCompatibleBitmap(&dc,bmpWidth, bmpHeight);
    CBitmap * pOldBitmap = (CBitmap *) memDC.SelectObject( bitmap );
    if (pOldBitmap == NULL) // if bitmap is very big, better check this !
    {
    memDC.DeleteDC();
    delete bitmap;
    AfxMessageBox("Not enough resource for the bitmap. Either reduce the bitmap dimension or 
     \nswitch to lower screen setting (e.g. 256-color mode), and try again.");
    return;
    }
    //draw bitmap here, or it can be done in another function
        memDC.Ellipse(0, 0, bmpWidth - 1, bmpHeight - 1);

    CDC prtDC;
    CPrintInfo printInfo;
    CSize size;
    DOCINFO di;
    CString szPortName, szAppName, szPrintError;
    szAppName.LoadString(AFX_IDS_APP_TITLE);
    szPrintError = ""; CSize paper_size;  //printer paper size in mm
    int xLogPPI = 0;
    int yLogPPI = 0; if( AfxGetApp()->GetPrinterDeviceDefaults(&printInfo.m_pPD->m_pd) )
    {
    HDC hDC = printInfo.m_pPD->m_pd.hDC;
    if (hDC == NULL)
    hDC = printInfo.m_pPD->CreatePrinterDC();
    if(hDC !=NULL)
    {
    prtDC.Attach(hDC);
    paper_size.cx = prtDC.GetDeviceCaps(HORZSIZE);
    paper_size.cy = prtDC.GetDeviceCaps(VERTSIZE);
    xLogPPI = prtDC.GetDeviceCaps(LOGPIXELSX);
    yLogPPI = prtDC.GetDeviceCaps(LOGPIXELSY);
    }
    else 
    {
    AfxMessageBox("Can not find printer. Please check installed/default printers.");
    return;
    }
    }
    int scr_xLogPPI = dc.GetDeviceCaps(LOGPIXELSX);
    int scr_yLogPPI = dc.GetDeviceCaps(LOGPIXELSY);
    int paper_width = (int) ((double) paper_size.cx * (double) xLogPPI / 25.4);   //width of a printed page in pixels
    int paper_height = (int) ((double) paper_size.cy * (double) yLogPPI / 25.4);
    double ratio_x = (double) xLogPPI / (double) scr_xLogPPI;
    double ratio_y = (double) yLogPPI / (double) scr_yLogPPI; CString strPageNumber = "";

    int page_info_left = (int) ( (double) paper_width * 0.9 );
    int page_info_right = paper_width;
    int page_info_top = (int) ( (double) paper_height * 0.99);
    int page_info_bottom = paper_height;
    CRect page_info_rect = CRect(page_info_left, page_info_top, 
                                   page_info_right,page_info_bottom );
    int printed_pages = 0;
    int total_print_pages = 0;
    BOOL bAbort_print = FALSE; // calculate pages
        int total_pages = (bmpWidth * ratio_x + paper_width - 1 ) / paper_width;
    //pop up printer dialog
    CPrintDialog prtDlg(FALSE, PD_PAGENUMS);

    prtDlg.m_pd.nMinPage = 1;
    prtDlg.m_pd.nMaxPage = total_pages;
    prtDlg.m_pd.nFromPage = 1;
    prtDlg.m_pd.nToPage = total_pages;

    if(prtDlg.DoModal() == IDOK )
    {
    memset(&di, 0, sizeof(DOCINFO));
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = szAppName;
    szPortName = prtDlg.GetPortName();
    di.lpszOutput = szPortName;
    prtDC.m_bPrinting = TRUE;
    }
    else
    return;  //Cancel button pressed, don't forget this! if(prtDC.StartDoc(&di) == -1)
    {
    AfxMessageBox("Printing error occured. Unable to find printer.");
    prtDC.Detach();
    prtDC.DeleteDC();
    return;
    }

    prtDC.SetMapMode(MM_TEXT);

    int i = 0;
    for(i = 0; i < total_pages; i++)
    {
    prtDC.StartPage();
    strPageNumber.Format("Page:%d of %d", ++printed_pages, total_print_pages );

    if ( i == (total_pages - 1) && total_pages > 1 ) //last page
    {
    int last_bmpWidth = bmpWidth - paper_width / ratio_x * i;
    prtDC.StretchBlt(0, 0, last_bmpWidth * ratio_x, bmpHeight* ratio_y, &memDC,
        paper_width * i / ratio_x, 0, last_bmpWidth, bmpHeight, SRCCOPY);
    }
    else
    prtDC.StretchBlt(0, 0, paper_width, bmpHeight* ratio_y, &memDC,
        paper_width * i / ratio_x, 0, paper_width / ratio_x , bmpHeight, SRCCOPY);
    prtDC.TextOut(page_info_rect.left, page_info_rect.top, strPageNumber );

    prtDC.EndPage();
    }
    memDC.SelectObject(pOldBitmap);
    delete bitmap;
    memDC.DeleteDC();

    prtDC.EndDoc();
    prtDC.Detach();
    prtDC.DeleteDC();
    return;
    }
      

  3.   

    void Draw(HDC hDC,HBITMAP hBmp,double dScaleX,double dScaleY,int iX,int iY,int iWidth=0,int iLength=0)
    {
    HPALETTE hPal;
    BITMAP bm;
    BITMAPINFOHEADER bi;
    LPBITMAPINFOHEADER  lpbi;
    DWORD dwLen;
    HANDLE hDIB;
    HANDLE handle;
    HDC  hDC1;
    if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE )
    {
    UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
    LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
    pLP->palVersion = 0x300;
    pLP->palNumEntries =GetSystemPaletteEntries( hDC, 0, 255, pLP->palPalEntry );
    hPal=CreatePalette(pLP );
    delete[] pLP;
    }
    if (hPal==NULL) hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
    ::GetObject(hBmp,sizeof(bm),(LPSTR)&bm);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight  = bm.bmHeight;
    bi.biPlanes  = 1;
    bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    int nColors = (1 << bi.biBitCount);
    if( nColors > 256 )
    nColors = 0;
    dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
    hDC1 = ::GetDC(NULL);
    hPal = SelectPalette(hDC1,hPal,FALSE);
    RealizePalette(hDC1);
    hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
    if (!hDIB)
    {
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    *lpbi = bi;
    ::GetDIBits(hDC1, hBmp, 0L, (DWORD)bi.biHeight,
    (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
    bi = *lpbi;
    if (bi.biSizeImage == 0)
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)* bi.biHeight;
    dwLen += bi.biSizeImage;
    if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    hDIB = handle;
    else
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    BOOL bGotBits = GetDIBits( hDC1, hBmp,0L,(DWORD)bi.biHeight,(LPBYTE)lpbi+ (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);
    if( !bGotBits )
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return;
    }
    if(iWidth==0||iLength==0)
    {
    iWidth=lpbi->biWidth;
    iLength=lpbi->biHeight;
    iWidth=(int)(dScaleX*iWidth);
    iLength=(int)(iLength*dScaleY);
    }
    StretchDIBits(hDC,iX,iY,iWidth,iLength,0,0,lpbi->biWidth,lpbi->biHeight,(LPBYTE)lpbi  // address for bitmap bits
    + (bi.biSize + nColors * sizeof(RGBQUAD)),(LPBITMAPINFO)lpbi,DIB_RGB_COLORS,SRCCOPY);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hDIB);
    DeleteObject(hPal);
    }