求教一下CreateWindow出来的子控件在SW_HIDE之后再SW_SHOW时, 背景图刷新的问题
我写了一个win32的dll, 先CreateWindow一个主窗体, 然后再在上面CreateWindow一个子控件, 也写了一个子控件的消息处理函数, 并且在WM_PAINT的时候给子控件画背景图, 这个时候背景图可以画出来的, 一切正常. 但当我SW_HIDE主窗体后, 再SW_SHOW出主窗体的时候, 那个子控件上的背景图不能画出来了, 请大家指教一下要怎么解决, 这个是win32的,不能用mfc
代码如下:
建立主窗体
m_hWndDLG = CreateWindow(L"DLL", NULL, WS_BORDER,
0, 0, nWidth, nHeight, NULL, NULL, m_hVCInst, (DLGPROC)WindowProc);
主窗体消息处理
LRESULT WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
PAINTSTRUCT ps;
HDC hdc;
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
return FALSE;
break;
case WM_DESTROY:
PostQuitMessage(0);
return FALSE;
case WM_COMMAND:
{
switch (wmId)
{
case IDC_BUTTON_LINK:
{
OnButtonCall();
}
break;
case IDC_BUTTON_VIDEO:
{
OnButtonVedio();
}
break;
case IDC_BUTTON_BACK:
{
OnButtonBack();
}
break;
case IDC_BUTTON_SPEECH:
{
OnButtonSpeech();
}
break;
case IDC_BUTTON_MINIMIZE:
{
OnButtonMinimize();
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//return TRUE;
}
break;
case WM_NOTIFY:
{
if(NM_CLICK == ((LPNMHDR)lParam)->code)
{
return OnClickTree(hWnd, ((LPNMHDR)lParam)->hwndFrom);
}
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
};
return DefWindowProc(hWnd, uMsg, wParam, lParam);
// return FALSE; // 返回FALSE给缺省对话框函数DefDlgProc(),表示没有处理本消息
}
// 新建最小化按钮
void NewBtnMinimize(int nID, RECT rect)
{
if (NULL == m_hBmpBtnMinimize)
{
m_hBmpBtnMinimize = GetBitmap(PNG_BTN_MINIMIZE);
}
if (NULL == m_hWBtnMinimize)
{
m_hWBtnMinimize = CreateWindow(_T("Button"), _T(""), WS_VISIBLE | WS_CHILD | BS_OWNERDRAW,
rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, m_hWndDLG, (HMENU)nID, m_hVCInst, NULL);
if (!m_hWBtnMinimize)
{
return;
}
}
g_BtnMinimizeWndProc = (WNDPROC)SetWindowLong(m_hWBtnMinimize, GWL_WNDPROC, (LONG)BtnMinimizeWndProc);
ShowWindow(m_hWBtnMinimize, SW_SHOW);
}
子窗体消息处理
LRESULT CALLBACK BtnMinimizeWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
OnPaint(hWnd, m_hBmpBtnMinimize);
return TRUE;
}
break;
}
return CallWindowProc(g_BtnMinimizeWndProc, hWnd, uMsg, wParam, lParam);
}
void OnPaint(HWND hWnd, HBITMAP hBitMap)
{
RECT rect;
GetClientRect(hWnd, &rect);
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HDC hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC, hBitMap);
//先将背景画上
BitBlt(hdc, 0, 0, nWidth, nWidth, hMemDC, rect.left, rect.top, SRCCOPY);
EndPaint(hWnd, &ps);
}
[解决办法]
好像是要在WM_ERASEBACKGROUD消息中做处理吧
[解决办法]
WM_ERASEBACKGROUD你这个消息没有处理。在这里把整个窗体刷一下。
[解决办法]
HDC hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC, hBitMap);
======
这个hBitMap很可疑,如果你不用memDC,直接画没问题的话,应该就是这里了
[解决办法]