我在客户区中用了个报表控件,但是他不支持打印,我想通过抓图的办法,把客户区的东西拷贝到打印DC 中,打印出来,如何代码实现???

解决方案 »

  1.   

    我也要! 
    [email protected]
      

  2.   

    BITMAPINFO * BitmapToDIB(HPALETTE hPal, // palette for color conversion
     HBITMAP  hBmp, // DDB for convert
     int nBitCount, int nCompression) // format wanted
    {
    typedef struct
    {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD        bmiColors[256+3];
    } DIBINFO; BITMAP  ddbinfo;
    DIBINFO dibinfo; // retrieve DDB information
    if ( GetObject(hBmp, sizeof(BITMAP), & ddbinfo)==0 )
    return NULL; // fill out BITMAPINFOHEADER based on size and required format
    memset(&dibinfo, 0, sizeof(dibinfo)); dibinfo.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
    dibinfo.bmiHeader.biWidth       = ddbinfo.bmWidth;
    dibinfo.bmiHeader.biHeight      = ddbinfo.bmHeight;
    dibinfo.bmiHeader.biPlanes      = 1;
    dibinfo.bmiHeader.biBitCount    = nBitCount;
    dibinfo.bmiHeader.biCompression = nCompression; HDC     hDC = GetDC(NULL); // screen DC
    HGDIOBJ hpalOld;

    if ( hPal )
    hpalOld = SelectPalette(hDC, hPal, FALSE);
    else
    hpalOld = NULL; // query GDI for image size
    GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS); int nInfoSize  = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader);
    int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader); BYTE * pDIB = new BYTE[nTotalSize]; if ( pDIB )
    {
    memcpy(pDIB, & dibinfo, nInfoSize);

    if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) )
    {
    delete [] pDIB;
    pDIB = NULL;
    }
    } if ( hpalOld )
    SelectObject(hDC, hpalOld); ReleaseDC(NULL, hDC); return (BITMAPINFO *) pDIB;
    }
    HBITMAP CaptureWindow(HWND hWnd)
    {
    RECT wnd; if ( ! GetWindowRect(hWnd, & wnd) )
    return NULL; HDC hDC = GetWindowDC(hWnd); HBITMAP hBmp = CreateCompatibleBitmap(hDC, wnd.right - wnd.left, wnd.bottom - wnd.top); if ( hBmp )
    {
    HDC hMemDC   = CreateCompatibleDC(hDC);
    HGDIOBJ hOld = SelectObject(hMemDC, hBmp); BitBlt(hMemDC, 0, 0, wnd.right - wnd.left, wnd.bottom - wnd.top, 
    hDC, 0, 0, SRCCOPY); SelectObject(hMemDC, hOld);
    DeleteObject(hMemDC);
    }

    ReleaseDC(hWnd, hDC); return hBmp;
    }void SaveWindowToFile(HWND hWnd, const char *filename, int nBitCount, int nCompression)
    {
    HBITMAP hBmp = CaptureWindow(hWnd); if(hBmp)
    {
    BITMAPINFO * pDIB = BitmapToDIB(NULL, hBmp, nBitCount, nCompression);
    if(pDIB)
    {
    SaveDIBToBmp(filename, pDIB, NULL);
    delete [](BYTE *) pDIB;
    }
    DeleteObject(hBmp);
    }
    }