开发者

WM_PAINT doesn't show up after painting

So i'm painting a bitmap, heres my code:

hdcMem = CreateCompatibleDC(hdc);

SelectObject(hdcMem, g_hBitmap);
GetObject(g_hBitmap, sizeof(bm), &bm);

BitBlt(hdc, 196 - (bm.bmWidth/2), 90, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

DeleteDC(hdcMem);

Sometimes, when I paint it with this code, the bit开发者_开发技巧map is not displayed. Although if i minimize/unminimize the window, the bitmap is displayed. I'm pretty sure there's no problems with my code so is there something else I should be doing?

EDIT:

Turns out it's not just bitmaps, if I draw text with TextOut sometimes it's not displayed until its minimized/unminimized. I don't think minimizing/unminimizing sends another WM_PAINT message, so I don't think that when i do that it's causing it to be repainted correctly.

Oh and the rest of the controls get painted normally, just the stuff inside WM_PAINT isn't painted.

UPDATE

Here's the code thats causing the problems, it works 98% of the time too.


// This is a global variable
bool GlobalVar = false;

// This is a different thread started with _beginthread
void ThreadExample()
{
    GlobalVar = true;
    InvalidateRect(hMainWnd, NULL, TRUE);
    _endthread();
}

case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    if (GlobalVar == true)
    {
        SetBkMode(hdc, TRANSPARENT);
        SetTextColor(hdc, 0x0000ff);

        OrigFont = SelectObject(hdc, g_hLargeFont);

        GetTextExtentPoint32(hdc, ErrorMsg, lstrlen(ErrorMsg), &sz);
        TextOut(hdc, 196 - (sz.cx/2), 100, ErrorMsg, lstrlen(ErrorMsg));

        SelectObject(hdc, OrigFont);
    }

    EndPaint(hWnd, &ps);
    break;

EDIT2:

Another important detail could be, in my actual application, this code is inside a if statement that checks a global variable, and paints if its true. And this variable is set from a different thread, and after the variable is set I call InvalidateRect(hMainWnd, NULL, TRUE);

Updated my example code to represent this.


What is immediately not good with this code snippet (you actually should rather have posted more details) is that you delete temporary DC with your global bitmap handle still selected into it. You need to do SelectObject once again to unselect your bitmap.

You normally do it like this:

HGDIOBJ hPreviousBitmap = SelectObject(hdcMem, g_hBitmap);
// ...
SelectObject(hdcMem, hPreviousBitmap);

Also, error checking never hurts. Possibly one of the API calls fail and it's important which one exactly as it sheds more light on the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜