BitBlt两个图像在彼此之上

时间:2013-04-15 09:28:43

标签: c++ winapi

我正在尝试编写一个程序,当勾选一个复选框时,会在屏幕的大面积上显示一个网格。我正在尝试使用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;
}
}

1 个答案:

答案 0 :(得分:3)

我可以看到的主要问题是您要拨打BeginPaintEndPaint两次。在完成任何绘画之前,您应该只在BeginPaint执行一次,在完成所有绘画之后,您应该EndPaint

可能存在其他问题,这些东西很难完全正确。