如果 DibSection 独立于任何设备,为什么它需要设备上下文?

If a DibSection is independent of any device, why does it need a device context?

创建位图时有三个四个选择:

为什么 CreateCompatibleBitmap 需要一个 hdc 参数是有道理的:它必须知道要与哪个 DC 兼容。

Note: It doesn't make sense why CreateBitmap doesn't take an hdc. How does it know what DC to be compatible with?

CreateBitmap doesn't take a DC, and it doesn't know what DC to be compatible with. That's your job. And you better make sure it's compatible with the DC you eventually intended to use it with.

为什么 CreateDIBSection 获取设备上下文句柄?

CreateDIBSection function

The CreateDIBSection function creates a DIB that applications can write to directly. The function gives you a pointer to the location of the bitmap bit values. You can supply a handle to a file-mapping object that the function will use to create the bitmap, or you can let the system allocate the memory for the bitmap.

| Function               | Type | Takes hdc |
|------------------------|------|-----------|
| CreateBitmap           | DDB  | No        |
| CreateCompatibleBitmap | DDB  | Yes       |
| CreateDIBitmap         | DDB  | Yes       |
| CreateDIBSection       | DIB  | Yes       |

DIB 有什么用?

加分题

问。 CreateBitmap 是怎么回事?

一个。由您来确保它兼容。祝你好运!或者你可以只使用 CreateCompatibleBitmap

红利阅读

答案好像是:

  • 仅当 usage = DIB_PAL_COLORS
  • 时才需要 hdc
  • 否则(即usage = DIB_RGB_COLORS)则hdc 可能是可选的