2016-03-03 73 views
0

我試圖創建一個窗口類的兩個實例。 當第一個關閉時,它應該關閉應用程序,但當第二個關閉時,它應該關閉該窗口。C++關閉窗口不應用

但是,當任一窗口關閉時,應用程序退出,我不知道爲什麼。我試着比較hWnd來檢查哪個窗口正在關閉。

// include the basic windows header file 
#include <windows.h> 
#include <windowsx.h> 

//Forgive me now 
#define MAX_WINDOWS 1024 

HWND hWindows[MAX_WINDOWS]; 

// the WindowProc function prototype 
LRESULT CALLBACK WindowProc(HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam); 

// the entry point for any Windows program 
int WINAPI WinMain(HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, 
    int nCmdShow) 
{ 
    WNDCLASSEX wc; 

    ZeroMemory(&wc, sizeof(WNDCLASSEX)); 

    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = WindowProc; 
    wc.hInstance = hInstance; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW; 
    wc.lpszClassName = L"WindowClass1"; 

    RegisterClassEx(&wc); 

    hWindows[0] = CreateWindowEx(NULL, 
     L"WindowClass1", // name of the window class 
     L"Our First Windowed Program", // title of the window 
     WS_OVERLAPPEDWINDOW, // window style 
     300, // x-position of the window 
     300, // y-position of the window 
     500, // width of the window 
     400, // height of the window 
     NULL, // we have no parent window, NULL 
     NULL, // we aren't using menus, NULL 
     hInstance, // application handle 
     NULL); // used with multiple windows, NULL 

    hWindows[1] = CreateWindowEx(NULL, 
     L"WindowClass1", // name of the window class 
     L"Our First Windowed Program", // title of the window 
     WS_OVERLAPPEDWINDOW, // window style 
     300, // x-position of the window 
     300, // y-position of the window 
     500, // width of the window 
     400, // height of the window 
     hWindows[0], // primary window 
     NULL, // we aren't using menus, NULL 
     hInstance, // application handle 
     NULL); // used with multiple windows, NULL 

    ShowWindow(hWindows[0], nCmdShow); 
    ShowWindow(hWindows[1], nCmdShow); 

    MSG msg; 

    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     // translate keystroke messages into the right format 
     TranslateMessage(&msg); 

     // send the message to the WindowProc function 
     DispatchMessage(&msg); 
    } 

    // return this part of the WM_QUIT message to Windows 
    return msg.wParam; 
} 

// this is the main message handler for the program 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    // sort through and find what code to run for the message given 
    switch (message) 
    { 

     case WM_CLOSE: 
     { 
      if (hWnd = hWindows[0]) { 
       // close the application entirely 
       PostQuitMessage(0); 
      } 
      else { 
       DestroyWindow(hWnd); 
      } 
      return 0; 
     } break; 

    } 

    // Handle any messages the switch statement didn't 
    return DefWindowProc(hWnd, message, wParam, lParam); 
} 

回答

3
if (hWnd = hWindows[0]) 

這是分配。由於hWindows[0]不爲零,因此該表達式始終評估爲真。

你的意思是:

if (hWnd == hWindows[0]) 

你應該響應呼籲PostQuitMessageWM_DESTROY。由於默認窗口過程調用DestroyWindow以響應WM_CLOSE,因此您可以這樣寫:

switch (message) 
{ 
case WM_DESTROY: 
    { 
     if (hWnd == hWindows[0]) { 
      // close the application entirely 
      PostQuitMessage(0); 
     } 
     return 0; 
    } 
    break; 
} 

// Handle any messages the switch statement didn't 
return DefWindowProc(hWnd, message, wParam, lParam); 
+0

Derp。感謝您指出了這一點! – Fletch