谢谢CDC

解决方案 »

  1.   

    先把设备上面的内容输出位图,然后使用下面的函数就行了,主要是DDB-DIB的转换
    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);
    }