C++ Win32 窗口背景不重绘

标签 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 但没有成功

最佳答案

您正在将 NULL 传递给 RegisterhBrush 参数。这就是当您调整大小时背景不会重新绘制的原因。

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

mainwin->Register(TEXT("test"), TEXT("test"), WndProc, TEXT("small.ico"),
    NULL, IDC_ARROW);

关于C++ Win32 窗口背景不重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10103918/

相关文章:

c++ - win32 GUI 在窗口中显示来自 char 数组变量的文本

C++ 处理自引用的派生类

c++ - 从 Qt 应用程序执行 MSI 文件

c++ - std::vector 与 array[number] 相同吗?

objective-c - 帮助 GCC 和 ObjectiveC 代码以及 Cygwin

windows - 是否存在 ReadProcessMemory 失败的情况?

c++ - 基于时间的通知使用的是 UTC 时间还是本地时间?

c++ - 清理DLL : _endthreadex() vs TerminateThread()中的线程

c++ - 如何使用 RegExp 验证字符串表达式?

c++ - 寻找调试棘手的 Windows 服务启动 gremlin 的想法