当屏幕解决方案更改时,为什么 CreateDIBSection 返回 NULL 而 GetLastError 返回 0?

发布于 2024-10-12 13:29:12 字数 1441 浏览 2 评论 0原文

以下是我的代码:

void Fun(int nScreenWidth, int nScreenHeight)
{
...
int nMemSize = nScreenWidth*nScreenHeight*3*7

HDC hdc = ::GetDC(hWnd);
int hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);

BITMAPINFO bmpInfo = {0};
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = 7*nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = 0;
bmpInfo.bmiHeader.biSizeImage = nMemSize;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiColors[0].rgbBlue = 204;
bmpInfo.bmiColors[0].rgbGreen = 204;
bmpInfo.bmiColors[0].rgbRed = 204;
bmpInfo.bmiColors[0].rgbReserved = 0;
PVOID pvBits = NULL;
HBITMAP hBmpWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, hBmpMapFile, 0);
DWORD dwErr = ::GetLastErr();
...
}

我的操作系统是windows xp,屏幕分辨率是1024*768,调用函数:

Fun(1024, 768);

我发现CreateDIBSection返回NULL,而GetLastErr()返回0。但是当屏幕分辨率是2048*1536时,调用函数:

Fun(2048, 1536);

我发现 CreateDIBSection 返回一个有效的句柄。

为什么 ?

似乎屏幕分辨率导致 CreateDIBSection 返回无效值,我不知道为什么。

但在某些 WINXP 中,无论屏幕分辨率如何,CreateDIBSection 总是成功。 从测试来看,我认为原因与HDC有关。 HDC与屏幕分辨率有关吗?

Following is my code:

void Fun(int nScreenWidth, int nScreenHeight)
{
...
int nMemSize = nScreenWidth*nScreenHeight*3*7

HDC hdc = ::GetDC(hWnd);
int hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);

BITMAPINFO bmpInfo = {0};
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = 7*nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = 0;
bmpInfo.bmiHeader.biSizeImage = nMemSize;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiColors[0].rgbBlue = 204;
bmpInfo.bmiColors[0].rgbGreen = 204;
bmpInfo.bmiColors[0].rgbRed = 204;
bmpInfo.bmiColors[0].rgbReserved = 0;
PVOID pvBits = NULL;
HBITMAP hBmpWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, hBmpMapFile, 0);
DWORD dwErr = ::GetLastErr();
...
}

My OS is windows xp, the screen resolution is 1024*768, call the function:

Fun(1024, 768);

I found that CreateDIBSection returns NULL while GetLastErr() reuturn 0. But when the screen resolution is 2048*1536, call the function:

Fun(2048, 1536);

I found that CreateDIBSection returns a valid handle.

Why ?

It seems that screen resolution causes that CreateDIBSection reuturn invalid value, I do not know why.

But in some WINXP, CreateDIBSection always successes whatever the screen resoultion is.
From the test, I think that the cause is about HDC. Is the HDC related with the screen resolution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文