为什么 DestroyWindow 关闭我的应用程序?

jma*_*erx 3 c c++ winapi

我在创建主窗口后创建了一个窗口,但在其句柄上调用 DestroyWindow 会关闭整个应用程序,我该如何简单地摆脱它?

它看起来像这样:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   HWND fakehandle;


   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_EX_LAYERED,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   fakehandle = CreateWindow(szWindowClass, "FAKE WINDOW", WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd || !fakehandle)
   {
      return FALSE;
   }
//some code
   DestroyWindow(fakehandle);


   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

如何在不破坏主窗口的情况下破坏此窗口?我正在创建一个虚拟窗口来检查 OpenGL 中的多重采样。

谢谢

Chr*_*isF 5

我刚刚发现了这个评论:

如果指定的窗口是父窗口或所有者窗口,则 DestroyWindow 在销毁父窗口或所有者窗口时会自动销毁关联的子窗口或拥有的窗口。该函数首先销毁子窗口或所有者窗口,然后销毁父窗口或所有者窗口。

DestroyWindowMSDN页面

这对您的问题有影响吗?你能设置你hWnd所在位置的父级//some code吗?


Jus*_*eff 5

DestroyWindow() 向相关窗口发送 WM_DESTROY。如果 WndProc 将 WM_DESTROY 传递给 DefWindowProc(),则 DefWindowProc() 将终止您的应用程序。

因此,在您的 WndProc 中,为 WM_DESTROY 创建一个处理程序(如果您还没有),并检查窗口句柄。您应该能够区分两者并从那里采取行动。

// assuming you have the two window handles as hwnd1 and hwnd2
case WM_DESTROY:
    if( hwnd == hwnd1 ) {
        // this will kill the app
        PostQuitMessage(0);
    } else if( hwnd == hwnd2 ) {
        // chucking WM_DESTROY on the floor
        // means this window will just close,
        // and the other one will stay up.
        return;
    }
    break;
Run Code Online (Sandbox Code Playgroud)

请注意,如果您代表任一窗口执行 PostQuitMessage() ,它将关闭您的应用程序,因为 PostQuitMessage() 将终止消息循环。