我正在尝试在我创建的窗口中显示BMP图像,并且在某种程度上是成功的。创建窗口并正确显示图像,但是当我离开窗口并再次打开时,图像已消失。例如,如果我单击窗口上的最小化按钮然后再次打开它,图像就会消失。如果我只是留在窗口,图像会停留在屏幕上。
我觉得我在使用BeginPaint函数在窗口中创建设备上下文时遇到了问题,但是如果不使用像CClientDC这样的东西我无法找到另一种方法做不想使用。所以这里有2个相关的功能,第一个是我的功能1)创建窗口2)在其中绘制BMP。这是函数:
bool Func()
{
// Register class
char ClassName[] = "ClassName";
WNDCLASSEX wc;
HWND hWnd;
wc.lpszClassName = ClassName;
wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_DBLCLKS;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = NULL;
wc.hIconSm = LoadIcon(NULL, IDI_SHIELD);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.lpszMenuName = NULL;
RegisterClassEx(&wc);
// Get screen size
const HWND Desktop = GetDesktopWindow();
RECT dDimensions;
GetWindowRect(Desktop, &dDimensions);
// Create window and begin message loop
hWnd = CreateWindowEx(WS_EX_TOPMOST, ClassName, "Hi", WS_OVERLAPPEDWINDOW, 0, 0, dDimensions.right, dDimensions.bottom, HWND_DESKTOP, 0, NULL, 0);
ShowWindow(hWnd, SW_SHOW);
// Set up device context
PAINTSTRUCT Paint;
HDC dContext = BeginPaint(hWnd, &Paint);
if (dContext == NULL) {
MessageBox(NULL, "Failed to call BeginPaint", "", 0);
return false;
}
// Load image
HBITMAP hBM = (HBITMAP)LoadImage(NULL, "image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBM == NULL) {
char Buffer[MAX_PATH];
sprintf(Buffer, "Failed to call LoadImage. Error %ld", GetLastError());
MessageBox(NULL, Buffer, "Failed", 0);
return false;
}
// Create new, compatible device context
HDC LocalDC = CreateCompatibleDC(dContext);
if (LocalDC == NULL) {
MessageBox(NULL, "Failed to call CreateCompatibleDC", "", 0);
return false;
}
// Get BMP params
BITMAP BM;
if (!GetObject(hBM, sizeof(BITMAP), &BM)) {
MessageBox(NULL, "Failed to call GetObject", "", 0);
return false;
}
// Select bitmap
HBITMAP OldBM = (HBITMAP)SelectObject(LocalDC, hBM);
if (OldBM == NULL) {
MessageBox(NULL, "Failed to call SelectObject", "", 0);
return false;
}
// Calculate values of where to place image. We want it to be centered.
int XOffset;
int YOffset;
// If the screen is bigger than the image...
if (dDimensions.right > BM.bmWidth && dDimensions.bottom > BM.bmHeight) {
XOffset = (dDimensions.right - BM.bmWidth) / 2;
YOffset = (dDimensions.bottom - BM.bmHeight) / 2;
}
// If image is bigger than screen, do best to center
else {
XOffset = -1 * ((BM.bmWidth - dDimensions.right) / 2);
YOffset = -1 * ((BM.bmHeight - dDimensions.left) / 2);
}
// Copy the image
if (!BitBlt(dContext, XOffset, YOffset, BM.bmWidth, BM.bmHeight, LocalDC, 0, 0, SRCCOPY)) {
printf("Failed to call BlitBlt. Error %ld", GetLastError());
getchar();
return false;
}
MSG messages;
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
DeleteObject(hBM);
return true;
}
第二个相关函数是用于处理窗口消息的窗口过程。引用的窗口过程并没有特别做任何事情,因为我真的只对显示图像感兴趣。也许我的错误在这里?无论如何,这里是窗口过程函数:
long __stdcall fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
break;
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
所以我的问题是当我最小化窗口并重新打开它时,图像消失了。窗口本身仍然存在,但图像消失了。有人可以指出我的函数逻辑中的错误在哪里?谢谢你。
答案 0 :(得分:1)
我不得不移动我的代码以将BMP绘制到消息切换的WM_PAINT情况中。每次窗口最小化并重新最大化时,Windows可能会调用RedrawWindow或类似的API(导致窗口再次变为空白)。谢谢你的回答,现在工作正常。