在尝试创建自己的 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
传递给 Register
的 hBrush
参数。这就是当您调整大小时背景不会重新绘制的原因。
我怀疑您打算使用可选参数并像这样调用它:
mainwin->Register(TEXT("test"), TEXT("test"), WndProc, TEXT("small.ico"),
NULL, IDC_ARROW);
我是一名优秀的程序员,十分优秀!