我正在尝试编写一个程序,当复选框被打勾时,会在屏幕的大面积上显示网格。我正在尝试使用 BitBlt 在 WM_PAINT 中执行此操作,并且我的显示函数肯定是使用正确的布尔值调用的。但是,它不会对窗口进行任何更改。我是在做一些愚蠢的事情,还是没有切实可行的方法来实现这一目标?如果这是无法实现的,是否有任何方法可以在不指定包含网格的不同图像集的情况下获得类似的结果?
void DisplayRoom(HWND hwnd, char TileList[][100], POINT pMaxDisplay, POINT pMinDisplay, HBITMAP ahbmTileset[], bool bDisplayBars)
{
HBITMAP hbmSprite, hbmMask;
BITMAP bm;
PAINTSTRUCT ps;
HINSTANCE hinNULL = GetModuleHandle(NULL);
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld;
POINT Position;
hbmSprite = LoadBitmap(hinNULL, MAKEINTRESOURCE(IDB_CLEAR_GROUND));
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmSprite);
GetObject(hbmSprite, sizeof(bm), &bm);
for (int iii = pMinDisplay.x; iii < pMaxDisplay.x; iii++)
for (int jjj = pMinDisplay.y; jjj < pMaxDisplay.y; jjj++)
{
switch(TileList[iii][jjj])
{
case 'g':
hbmSprite = ahbmTileset[0];
break;
case 'd':
hbmSprite = ahbmTileset[1];
break;
case 'i':
hbmSprite = ahbmTileset[2];
break;
case 'l':
hbmSprite = ahbmTileset[3];
break;
}
assert(hbmSprite != NULL);
SelectObject(hdcMem, hbmSprite);
BitBlt(hdc, (iii - pMinDisplay.x) * 34 + 90, (jjj - pMinDisplay.y) * 34 + 60, 34, 34, hdcMem, 0, 0, SRCCOPY);
}
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
if (bDisplayBars)
{
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
hbmSprite = LoadBitmap(hinNULL, MAKEINTRESOURCE(IDB_GRID));
assert(hbmSprite != NULL);
hbmMask = CreateBitmapMask(hbmSprite, RGB(0,0,0));
for (int iii = pMinDisplay.x; iii < pMaxDisplay.x; iii++)
for (int jjj = pMinDisplay.y; jjj < pMaxDisplay.y; jjj++)
{
SelectObject(hdcMem, hbmMask);
BitBlt(hdc, (iii - pMinDisplay.x) * 34 + 90, (jjj - pMinDisplay.y) * 34 + 60, 34, 34, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, hbmSprite);
BitBlt(hdc, (iii - pMinDisplay.x) * 34 + 90, (jjj - pMinDisplay.y) * 34 + 60, 34, 34, hdcMem, 0, 0, SRCPAINT);
}
DeleteDC (hdcMem);
EndPaint(hwnd, &ps);
}
}
对话过程如下所示。
BOOL CALLBACK CreateRoom(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
static char achTileDetails[100][100];
static POINT pDimensions, pMonsterLocations, pMinDisplay, pMaxDisplay;
static HBITMAP ahbmTileset[4];
static bool bShowBars;
switch(Message)
{
case WM_INITDIALOG:
SetFocus(hwnd);
for (int iii = 0; iii < 100; iii++)
for(int jjj = 0; jjj < 100; jjj++)
achTileDetails[iii][jjj] = 'g';
pDimensions.x = 20;
pDimensions.y = 10;
pMinDisplay.x = 0;
pMinDisplay.y = 0;
pMaxDisplay.x = 20;
pMaxDisplay.y = 10;
for (int iii = 0; iii < 4; iii++)
ahbmTileset[iii] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CLEAR_GROUND + iii));
return TRUE;
case WM_PAINT:
DisplayRoom(hwnd, achTileDetails, pMaxDisplay, pMinDisplay, ahbmTileset, bShowBars);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
case IDC_DISPLAY_GRID:
switch(HIWORD(wParam))
{
case BN_CLICKED:
bShowBars = SendMessage(GetDlgItem(hwnd, IDC_DISPLAY_GRID), BM_GETCHECK, 0, 0);
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
break;
}
break;
}
break;
case WM_LBUTTONDOWN:
break;
default:
return FALSE;
}
}