我正在使用WinAPI和GDI创建自定义gui。我有一些问题,重新绘制它。如果我将拖动窗口,它重绘很好,但如果我拖动窗口超出屏幕它不重绘。看下面的截图:
ToxicInjector.cpp:
#include "ToxicInjector.h"
...
// Method register native window object
bool ToxicInject::Application::RegisterNativeWindow()
{
WNDCLASSEX wc{ sizeof(WNDCLASSEX) };
...
wc.style = CS_VREDRAW | CS_HREDRAW;
...
}
// Window message procedure
LRESULT ToxicInject::Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
// Executes when window creates
case WM_CREATE:
{
this->CreateNativeControls();
...
}
break;
...
// Need for draw custom controls
case WM_DRAWITEM:
{
static LPDRAWITEMSTRUCT pdis = reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
switch (pdis->CtlID)
{
case UPDATE_BUTTON:
{
if (pdis->itemState & ODS_SELECTED)
this->isMouseOnUpdateBtn = true;
else
this->isMouseOnUpdateBtn = false;
this->DrawUpdateButton(pdis->hDC);
}
break;
case INJECT_BUTTON:
{
if (pdis->itemState & ODS_SELECTED)
this->isMouseOnInjectBtn = true;
else
this->isMouseOnInjectBtn = false;
this->DrawInjectButton(pdis->hDC);
}
break;
case BROWSE_BUTTON:
{
if (pdis->itemState & ODS_SELECTED)
this->isMouseOnBrowseBtn = true;
else
this->isMouseOnBrowseBtn = false;
this->DrawBrowseButton(pdis->hDC);
}
break;
}
}
break;
...
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
...
// Method creates all native-window controls
void ToxicInject::Application::CreateNativeControls()
{
...
this->hUpdate_Button = CreateWindow(L"BUTTON", L"", WS_VISIBLE| WS_CHILD | BS_OWNERDRAW, 224, 50, 212, 26, this->hwnd, reinterpret_cast<HMENU>(this->UPDATE_BUTTON), 0, 0);
this->hInject_Button = CreateWindow(L"BUTTON", L"", WS_VISIBLE| WS_CHILD | BS_OWNERDRAW, 224, 76 + 15, 212, 26, this->hwnd, reinterpret_cast<HMENU>(this->INJECT_BUTTON), 0, 0);
this->hBrowse_Button = CreateWindow(L"BUTTON", L"", WS_VISIBLE| WS_CHILD | BS_OWNERDRAW, 224, 91 + 26 + 15, 212, 26, this->hwnd, reinterpret_cast<HMENU>(this->BROWSE_BUTTON), 0, 0);
}
...
// Method draw update button
void ToxicInject::Application::DrawUpdateButton(HDC hDC)
{
...
if (this->isMouseOnUpdateBtn)
{
bg = CreateSolidBrush(RGB(44, 97, 46));
/*DRAW BACKGROUND*/
FillRect(hDC, &rc, bg);
/*DRAW BOTTOM LINE*/
SelectObject(hDC, hPen);
MoveToEx(hDC, 0, 26, 0);
LineTo(hDC, 212, 26);
/*DRAW TEXT*/
SetBkColor(hDC, RGB(44, 97, 46));
SetTextColor(hDC, RGB(255, 255, 255));
SelectObject(hDC, hTahoma_Regular);
TextOut(hDC, 83, 3, L"Update", 7);
/*FREE GDI MEMORY*/
DeleteObject(bg);
DeleteObject(hTahoma_Regular);
DeleteObject(hPen);
}
else
{
bg = CreateSolidBrush(RGB(67, 156, 70));
/*DRAW BACKGROUND*/
FillRect(hDC, &rc, bg);
/*DRAW BOTTOM LINE*/
SelectObject(hDC, hPen);
MoveToEx(hDC, 0, 26, 0);
LineTo(hDC, 212, 26);
/*DRAW TEXT*/
SetBkColor(hDC, RGB(67, 156, 70));
SetTextColor(hDC, RGB(255, 255, 255));
SelectObject(hDC, hTahoma_Regular);
TextOut(hDC, 83, 2, L"Update", 7);
/*FREE GDI MEMORY*/
DeleteObject(bg);
DeleteObject(hTahoma_Regular);
DeleteObject(hPen);
}
}
...
抱歉我的英文=)。如何解决这个问题?
所以我修好了!
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pdis = reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
switch (pdis->CtlID)
...
}
指向DRAWITEMSTRUCT
的指针应该是非静态的!这是逻辑错误,我的错误。完美的工作=)。
您可以强制重绘以回答WM_MOVE
case WM_MOVE:
this->OnWindow_Paint();
break;