将hbitmap复制到剪贴板 [英] copy hbitmap to clipboard

查看:103
本文介绍了将hbitmap复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

谁能找到解决方案????

我有一个.bmp文件的HBITMAP句柄.不,我需要将其复制到剪贴板并粘贴到其他应用程序中,例如MS Paint等...

预先感谢.

Hai,

Can anyone find a solution for this????

I have a HBITMAP handle of a .bmp file. No i need to copy it to clipboard and paste in other applications like MS Paint etc...

Thanks in advance.

推荐答案

这需要创建DIB(设备独立位图)并将其使用全局内存传递到剪贴板.

This requires creation of a DIB (device indpendant bitmap) and passing it to the clipboard using global memory.

// This is based on BitmapToDIB() from DIBUTIL.C of the Microsoft WINCAP sample.
bool BitmapToClipboard(HBITMAP hBM, HWND hWnd)
{
    if (!::OpenClipboard(hWnd))
        return false;
    ::EmptyClipboard();
    
    BITMAP bm;
    ::GetObject(hBM, sizeof(bm), &bm));
    
    BITMAPINFOHEADER bi;
    ::ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight = bm.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = bm.bmBitsPixel;
    bi.biCompression = BI_RGB;
    if (bi.biBitCount <= 1)	// make sure bits per pixel is valid
        bi.biBitCount = 1;
    else if (bi.biBitCount <= 4)
        bi.biBitCount = 4;
    else if (bi.biBitCount <= 8)
        bi.biBitCount = 8;
    else // if greater than 8-bit, force to 24-bit
        bi.biBitCount = 24;
    
    // Get size of color table.
    SIZE_T dwColTableLen = (bi.biBitCount <= 8) ? (1 << bi.biBitCount) * sizeof(RGBQUAD) : 0;
    
    // Create a device context with palette
    HDC hDC = ::GetDC(NULL);
    HPALETTE hPal = static_cast<HPALETTE>(::GetStockObject(DEFAULT_PALETTE));
    HPALETTE hOldPal = ::SelectPalette(hDC, hPal, FALSE);
    ::RealizePalette(hDC);
    
    // Use GetDIBits to calculate the image size.
    ::GetDIBits(hDC, hBM, 0, static_cast<UINT>(bi.biHeight), NULL,
        reinterpret_cast<LPBITMAPINFO>(&bi), DIB_RGB_COLORS);
    // If the driver did not fill in the biSizeImage field, then compute it.
    // Each scan line of the image is aligned on a DWORD (32bit) boundary.
    if (0 == bi.biSizeImage)
        bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) * bi.biHeight;
    
    // Allocate memory
    HGLOBAL hDIB = ::GlobalAlloc(GMEM_MOVEABLE, sizeof(BITMAPINFOHEADER) + dwColTableLen + bi.biSizeImage);
    if (hDIB)
    {
        union tagHdr_u
        {
            LPVOID             p;
            LPBYTE             pByte;
            LPBITMAPINFOHEADER pHdr;
            LPBITMAPINFO       pInfo;
        } Hdr;
        
        Hdr.p = ::GlobalLock(hDIB);
        // Copy the header
        ::CopyMemory(Hdr.p, &bi, sizeof(BITMAPINFOHEADER));
        // Convert/copy the image bits and create the color table
        int nConv = ::GetDIBits(hDC, hBM, 0, static_cast<UINT>(bi.biHeight),
            Hdr.pByte + sizeof(BITMAPINFOHEADER) + dwColTableLen, 
            Hdr.pInfo, DIB_RGB_COLORS);
        ::GlobalUnlock(hDIB);
        if (!nConv)
        {
            ::GlobalFree(hDIB);
            hDIB = NULL;
        }
    }
    if (hDIB)
        ::SetClipboardData(CF_DIB, hDIB);
    ::CloseClipboard();
    ::SelectPalette(hDC, hOldPal, FALSE);
    ::ReleaseDC(NULL, hDC);
    return NULL != hDib;
}



这篇关于将hbitmap复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆