[英]Strange Show Window behavior
I found a window shows when I call SetWindowPos
or MoveWindow
, I mean the window shows during WM_CREATE
messsage before I have a chance to call ShowWindow
. 当我调用SetWindowPos
或MoveWindow
,我发现一个窗口显示,我的意思是在我有机会调用ShowWindow
之前,窗口显示WM_CREATE
消息。 After a little check, I found it has something to do with SetWindowRedraw
. 经过一番检查后,我发现它与SetWindowRedraw
。
Here's an example: 这是一个例子:
#include <Windows.h>
#include <windowsx.h>
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL MainWindow_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct);
void MainWindow_OnDestroy(HWND hWnd);
void MainWindow_OnSize(HWND hWnd, UINT state, int cx, int cy);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex = { sizeof(wcex) };
HWND hWnd;
BOOL ret;
MSG msg;
wcex.lpfnWndProc = MainWindowProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wcex.lpszClassName = TEXT("MainWindow");
wcex.hIconSm = wcex.hIcon;
RegisterClassEx(&wcex);
hWnd = CreateWindow(wcex.lpszClassName, TEXT("Test"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
// ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (ret == -1)
{
return 1;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, MainWindow_OnCreate);
HANDLE_MSG(hWnd, WM_DESTROY, MainWindow_OnDestroy);
HANDLE_MSG(hWnd, WM_SIZE, MainWindow_OnSize);
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
BOOL MainWindow_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
SetWindowPos(hWnd, NULL, 100, 100, 300, 300, SWP_NOREDRAW | SWP_NOZORDER);
return TRUE;
}
void MainWindow_OnDestroy(HWND hWnd)
{
PostQuitMessage(0);
}
void MainWindow_OnSize(HWND hWnd, UINT state, int cx, int cy)
{
SetWindowRedraw(hWnd, FALSE);
SetWindowRedraw(hWnd, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN) ;
}
Run this, a window will show without calling ShowWindow
. 运行此命令,将显示一个窗口而不调用ShowWindow
。 But if comment 但如果评论
SetWindowRedraw(hWnd, FALSE);
SetWindowRedraw(hWnd, TRUE);
then the window will remain hidden. 然后窗口将保持隐藏状态。
Can somebody explain this? 有人可以解释一下吗?
SetWindowRedraw()
is a macro which sends the WM_SETREDRAW
message (using SendMessage()
) to a window. SetWindowRedraw()
是一个宏,它将WM_SETREDRAW
消息(使用SendMessage()
)发送到窗口。
When a window receives such message its set of styles gets the WS_VISIBLE
style added. 当窗口收到此类消息时,其样式集将添加WS_VISIBLE
样式。
Verbatim from MSDN ( WM_SETREDRAW ): 从MSDN( WM_SETREDRAW )逐字:
[...] If the application sends the WM_SETREDRAW message to a hidden window, the window becomes visible (that is, the operating system adds the WS_VISIBLE style to the window). [...]如果应用程序将WM_SETREDRAW消息发送到隐藏窗口,则该窗口变为可见(即,操作系统将WS_VISIBLE样式添加到窗口)。 [...] [...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.