0

我在打电话时发现了一个窗口显示,SetWindowPos或者MoveWindow,我的意思是WM_CREATE在我有机会打电话之前在消息期间显示窗口ShowWindow。稍微查了一下,发现跟SetWindowRedraw.

这是一个例子:

#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) ;
}

运行此程序,将显示一个窗口,而无需调用ShowWindow. 但如果评论

SetWindowRedraw(hWnd, FALSE);
SetWindowRedraw(hWnd, TRUE);

那么窗口将保持隐藏状态。

有人可以解释一下吗?

4

1 回答 1

2

SetWindowRedraw()是一个将WM_SETREDRAW消息(使用SendMessage())发送到窗口的宏。

当一个窗口收到这样的消息时,它的样式集就会WS_VISIBLE添加样式。

来自 MSDN ( WM_SETREDRAW ) 的逐字记录:

[...] 如果应用程序将 WM_SETREDRAW 消息发送到隐藏窗口,则窗口变为可见(即操作系统将 WS_VISIBLE 样式添加到窗口)。[...]

于 2012-11-23T14:13:02.123 回答