我想创建一个在运行时生成一些位图的类,然后根据请求在目标设备的上下文中绘制它们。
我试试这个:
我的位图.h
#include <windows.h>
class myBitmaps
{
public:
void myBitmaps(HDC hDC);
const int size = 16;
HDC firstDC;
HBITMAP firstBuff;
HDC secondDC;
HBITMAP secondBuff;
void drawBitmap(HDC hDC, int xPos, int yPos, bool first);
}
我的位图.cpp
#include "myBitmaps.h"
void myBitmaps(HDC hDC)
{
firstDC = CreateCompatibleDC(hDC);
firstBuff = CreateCompatibleBitmap(hDC, size, size);
SelectObject(firstDC, firstBuff);
...draw some lines...
secondDC = CreateCompatibleDC(hDC);
secondBuff = CreateCompatibleBitmap(hDC, size, size);
SelectObject(secondDC, secondBuff);
...draw some lines...
}
void drawBitmap(HDC hDC, int xPos, int yPos, bool first)
{
if(first) {
BitBlt(hDC, xPos, yPos, size, size, firstDC , 0, 0, SRCCOPY);
}
else {
BitBlt(hDC, xPos, yPos, size, size, secondDC , 0, 0, SRCCOPY);
}
}
但是这段代码会导致运行时错误。
如何在我的类中存储多个位图?
最佳答案
There can be only one type of each GDI object selected into any type of DC at a time. The memory DC is unique, because it is the only type of DC that is possible to use an HBITMAP with a call to ::SelectObject. Unlike other GDI object types, the HBITMAP can only be selected into one DC at a time. Therefore, if you are using the same bitmap with multiple memory DCs, be sure to save the original HGDIOBJ pushed-out from the memory DC when you select your bitmap into the DC. Otherwise, your attempt to select the bitmap into a second memory DC will fail.
有关详细信息,请参阅下面的链接。
链接中列出了很多使用CompatibleDC
时需要注意的事项。请仔细阅读。
关于c++ - 如何在一个类中存储多个位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56541673/