c++ - 如何在一个类中存储多个位图?

标签 c++ winapi bitmap

我想创建一个在运行时生成一些位图的类,然后根据请求在目标设备的上下文中绘制它们。

我试试这个:

我的位图.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.

有关详细信息,请参阅下面的链接。

Guide to Win32 Memory DC

链接中列出了很多使用CompatibleDC时需要注意的事项。请仔细阅读。

关于c++ - 如何在一个类中存储多个位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56541673/

相关文章:

c++ - memset() 的意外行为

windows - 在我的程序运行时防止 Windows 进入休眠状态?

c++ - 在 C++ 上获取本地计算机的 IP 地址

c# - 创建位图图像 WPF

javax.imageio.IIOException : Missing Huffman code table entry while Adding text to an jpg image

c++ - 错误 : no matching function for call to 'make_pair(int&, Quest*)'

C++20 "transparently replaceable"关系

后台窗口的 C++ WinAPI 屏幕截图

Android 双线性图像缩小

c++ - boost::ptr_vector 和 boost::any 的问题