C ++ Win32窗口背景不重绘

时间:2012-04-11 10:11:34

标签: c++ winapi

在我尝试创建自己的win32包装器时,我遇到了这个问题。 每当我调整窗口大小时,背景都不会被重绘(http://i44.tinypic.com/23k855.png白色应该是背景,但当我使窗口变小时会变黑)

我班级的一部分:

void Register(  tstring _className,
                    tstring _menuName,
                    WNDPROC w32proc,
                    tstring hIcon,
                    HICON hIconSmall,
                    LPCTSTR hCursor,
                    HBRUSH hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH),
                    UINT windowStyle = CS_HREDRAW | CS_VREDRAW,
                    int classExtraBytes=NULL,
                    int windowExtraBytes=NULL
                    )
    {
        className = _className; 
        menuName = _menuName;

        wcex.cbSize         = sizeof(WNDCLASSEX);
        wcex.style          = windowStyle;
        wcex.lpfnWndProc    = w32proc;
        wcex.cbClsExtra     = classExtraBytes;
        wcex.cbWndExtra     = windowExtraBytes;
        wcex.hInstance      = this->hInst;
        wcex.hIcon          = LoadIcon(this->hInst,hIcon.c_str());
        wcex.hCursor        = LoadCursor(NULL,hCursor);
        wcex.hbrBackground  = hBrush;
        wcex.lpszMenuName   = this->menuName.c_str();
        wcex.lpszClassName  = this->className.c_str();
        wcex.hIconSm        = hIconSmall;

        RegisterClassEx(&wcex);
    }

    bool Create(tstring _windowTitle,
                DWORD windowStyle,
                int width=CW_USEDEFAULT,
                int height=CW_USEDEFAULT,
                int xPos=CW_USEDEFAULT,
                int yPos=CW_USEDEFAULT,
                HWND parent=NULL,
                LPVOID lpParam=NULL,
                HMENU menu=NULL
                )
        {
        windowTitle = _windowTitle;

        handle = CreateWindow(this->className.c_str(), this->windowTitle.c_str(), windowStyle,
                             xPos, yPos, width, height, parent, menu, this->hInst, lpParam);
        if (!this->handle)
            return false;

        Show();
        return true;
    }

    bool Show(int cmd=SW_SHOWNORMAL)
    {
        if( !ShowWindow(this->handle, cmd) || !UpdateWindow(this->handle) )
            return false;
        return true;
    }

我的窗口过程:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

以及我如何使用所有内容:

mainwin = new Window(hInstance);
mainwin->Register(TEXT("test"),TEXT("test"),WndProc,TEXT("small.ico"),NULL,IDC_ARROW,NULL,NULL,NULL);
mainwin->Create(TEXT("test window"),WS_OVERLAPPEDWINDOW );
mainwin->Show(nCmdShow);
while(mainwin->GetMsg() > 0)
    mainwin->TranslateDispatchMsg();

非常感谢一些帮助:)

ps:我也试过处理WM_PAIN,WM_SIZE,WM_SIZING消息来调用UpdateWindow和/或ShowWindow但没有成功

1 个答案:

答案 0 :(得分:1)

您正在将NULL传递给hBrush的{​​{1}}参数。这就是为什么在调整大小时不重绘背景的原因。

我怀疑你打算使用你的可选参数并像这样调用它:

Register