我是WINAPI的新手,我正在尝试制作一个小游戏,我需要将图像作为一个整体显示在屏幕上,而不是依次显示每个块,我只能发现我首先需要将所有内容写入memoryDC,然后输出它,但我不明白如何完全实现它。
另外,由于某种原因,只有在调整窗口大小时才会重绘窗口,我该如何解决这个问题?
我尝试做某事:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
HBITMAP hBmp_red = (HBITMAP)LoadImage(NULL, L"C:\\Red.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC bmpdc = NULL;
HDC memdc = NULL;
HBITMAP membmp = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_EX_APPWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SelectObject(bmpdc, hBmp_red);
StretchBlt(memdc, 0, 0, 100, 100, bmpdc, 0, 0, 10, 10, SRCCOPY);
membmp = CreateCompatibleBitmap(hdc, 100, 100);
SelectObject(memdc, membmp);
BitBlt(hdc, 0, 0, 100, 100, memdc, 100, 100, SRCCOPY);
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}