为什么不能将HBIMAP从CImage直接发送到剪贴板? [英] Why can't I directly send an HBIMAP from a CImage to clipboard?

查看:123
本文介绍了为什么不能将HBIMAP从CImage直接发送到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个小部件,在Windows上进行屏幕捕获.

I'm working on a small widget, screen capture stuff on Windows.

该程序的核心类似于以下内容(我在Internet上找到了它).首先从CImage派生,然后添加一个成员函数来做到这一点:

The core of this program is like the following (I found it on the Internet). First derive from CImage, then add a member function to do this:

BOOL CScreenImage::CaptureRect(const CRect& rect)
{
    // detach and destroy the old bitmap if any attached
    CImage::Destroy();

    // create a screen and a compatible memory device context
    HDC hDCScreen = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
    HDC hDCMem = ::CreateCompatibleDC(hDCScreen);
    //
    // create a bitmap compatible with the screen and select it into the memory DC
    // so you can do whatever you want on the bitmap through the mem DC.
    //
    HBITMAP hBitmap =
    ::CreateCompatibleBitmap(hDCScreen, rect.Width(), rect.Height());
    HBITMAP hBmpOld = (HBITMAP)::SelectObject(hDCMem, hBitmap);
    //
    // bit-blit from screen to memory device context
    // note: CAPTUREBLT flag is required to capture layered windows
    //
    DWORD dwRop = SRCCOPY | CAPTUREBLT;
    BOOL bRet = ::BitBlt(hDCMem, 0, 0, rect.Width(), rect.Height(),
    hDCScreen,
    rect.left, rect.top, dwRop);
    // attach bitmap handle to this object
    Attach(hBitmap);

    // restore the memory DC and perform cleanup
    ::SelectObject(hDCMem, hBmpOld);
    ::DeleteDC(hDCMem);
    ::DeleteDC(hDCScreen);

    return bRet;
}

效果很好.如果要将位图发送到剪贴板,则只需执行以下操作:

It worked fine. And if I want to send the bitmap to clipboard, I just need to do the following:

if (::OpenClipboard(hWnd)) {
    HBITMAP hImage = Detach();
    ::EmptyClipboard();
    ::SetClipboardData(CF_BITMAP, hImage);
    ::CloseClipboard();
}

我对其进行了测试,可以使用它通过剪贴板将位图从程序复制到Paint,Word,PowerPoint.

I tested it and I can use it to copy a bitmap from the program to Paint, Word, PowerPoint through the clipboard.

但是,我不明白的是,这行不通:

However, what I don't understand is that this dosen't work:

if (::OpenClipboard(this->GetSafeHwnd())) {
    CImage img;
    img.Load(_T("D:\\scc.bmp"));

    ::EmptyClipboard();
    ::SetClipboardData(CF_BITMAP, img.Detach());
    ::CloseClipboard();
}

似乎确实有一些数据发送到剪贴板,但是接收方(例如Paint)会抱怨剪贴板上的数据无法插入".

It seems that some data was indeed sent to the clipboard, but the receiver side (like Paint) would complain "the data on the clipboard cannot be inserted".

任何人都可以帮忙吗?非常感谢.

Can anyone help? Many thanks.

推荐答案

CImage* pImage = GetImage();
CDC memDC;
memDC.CreateCompatibleDC(NULL);

CBitmap bitmap;
bitmap.CreateCompatibleBitmap(GetDC(), pImage->GetWidth(), pImage->GetHeight());
memDC.SelectObject(&bitmap);
pImage->BitBlt(memDC.GetSafeHdc(), 0, 0, pImage->GetWidth(), pImage->GetHeight(), 0, 0, SRCCOPY);

EmptyClipboard();
//put the data on the clipboard
SetClipboardData(CF_BITMAP, bitmap.GetSafeHandle());
CloseClipboard();
memDC.DeleteDC();
bitmap.Detach();

这篇关于为什么不能将HBIMAP从CImage直接发送到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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