2016-11-30 65 views
0

所以我的任務是創建5個子窗口,並按下其他順序關閉窗口。但是我有一個問題,當我關閉前兩個窗口時,其他窗口變爲非活動狀態,所以我必須點擊它才能關注窗口,但是如何防止其他窗口變爲非活動狀態?如何在關閉一個窗口後阻止Win32應用程序,其他窗口變爲不活動

#include "stdafx.h" 

#define MAX_LOADSTRING 100 

// Global Variables: 
HINSTANCE hInst;        // current instance 
WCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
WCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 
int windowsCount = 1; 
WCHAR buffer[5]; 
HWND windows[5]; 

// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

int APIENTRY wWinMain(_In_ HINSTANCE hInstance, 
    _In_opt_ HINSTANCE hPrevInstance, 
    _In_ LPWSTR lpCmdLine, 
    _In_ int  nCmdShow) 
{ 
    srand(unsigned(time(NULL))); 

    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 

    // Initialize global strings 
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadStringW(hInstance, IDC_OOP10, szWindowClass, MAX_LOADSTRING); 
    MyRegisterClass(hInstance); 

    // Perform application initialization: 
    if (!InitInstance(hInstance, nCmdShow)) 
     return FALSE; 

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OOP10)); 

    MSG msg; 

    // Main message loop: 
    while (GetMessage(&msg, nullptr, 0, 0)) { 
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 

    return (int)msg.wParam; 
} 

// 
// FUNCTION: MyRegisterClass() 
// 
// PURPOSE: Registers the window class. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) { 
    WNDCLASSEXW wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra = 0; 
    wcex.cbWndExtra = 0; 
    wcex.hInstance = hInstance; 
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OOP10)); 
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_OOP10); 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 

    return RegisterClassExW(&wcex); 
} 

// 
// FUNCTION: InitInstance(HINSTANCE, int) 
// 
// PURPOSE: Saves instance handle and creates main window 
// 
// COMMENTS: 
// 
//  In this function, we save the instance handle in a global variable and 
//  create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { 
    hInst = hInstance; // Store instance handle in our global variable 

    HWND hWnd = CreateWindowW(szWindowClass, L"Main Window", WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); 

    SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256))); 

    if (!hWnd) 
     return FALSE; 

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

    windows[0] = hWnd; 

    return TRUE; 
} 

BOOL CreateChildWindow(HWND hWnd) { 
    _itoa(windowsCount + 1, (char*)buffer, 10); 

    SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256))); 
    HWND childWnd = CreateWindow(szWindowClass, buffer, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, windows[windowsCount - 1], nullptr, hInst, nullptr); 

    if (!childWnd) 
     return FALSE; 

    ShowWindow(childWnd, SW_SHOWDEFAULT); 
    UpdateWindow(childWnd); 

    windows[windowsCount++] = childWnd; 

    return TRUE; 
} 

// 
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 
// 
// PURPOSE: Processes messages for the main window. 
// 
// WM_COMMAND - process the application menu 
// WM_PAINT - Paint the main window 
// WM_DESTROY - post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { 
    switch (message) { 
    case WM_LBUTTONUP: 
    { 
     if (windowsCount < 5) { 
      if (!CreateChildWindow(hWnd)) 
       return FALSE; 
     } 
    } 
    break; 
    case WM_CLOSE: 
    { 
     if (IDOK == MessageBox(hWnd, L"Are you sure you want to quit?", L"You are leaving the program", MB_OKCANCEL | MB_ICONINFORMATION | MB_DEFBUTTON2)) { 
      if (windowsCount == 1) { 
       DestroyWindow(windows[--windowsCount]); 
       PostQuitMessage(NULL); 
       break; 
      } 
      else 
       DestroyWindow(windows[--windowsCount]); 
     } 
    } 
    break; 
    case WM_RBUTTONUP: 
     SetWindowText(hWnd, L"Changed title!"); 
     break; 
    case WM_KEYDOWN: { 
     if (windowsCount == 1) { 
      DestroyWindow(windows[--windowsCount]); 
      PostQuitMessage(NULL); 
      break; 
     } 
     else 
      DestroyWindow(windows[--windowsCount]); 
    } 
    break; 
    case WM_COMMAND: 
    { 
     int wmId = LOWORD(wParam); 
     // Parse the menu selections: 
     switch (wmId) 
     { 
     case IDM_ABOUT: 
      DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
      break; 
     case IDM_EXIT: 
      DestroyWindow(hWnd); 
      break; 
     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
    } 
    break; 
    case WM_PAINT: 
    { 
     PAINTSTRUCT ps; 
     HDC hdc = BeginPaint(hWnd, &ps); 
     // TODO: Add any drawing code that uses hdc here... 
     EndPaint(hWnd, &ps); 
    } 
    break; 
    /*case WM_DESTROY: 
     PostQuitMessage(0); 
     break;*/ 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { 
    UNREFERENCED_PARAMETER(lParam); 
    switch (message) { 
    case WM_INITDIALOG: 
     return (INT_PTR)TRUE; 

    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { 
      EndDialog(hDlg, LOWORD(wParam)); 
      return (INT_PTR)TRUE; 
     } 
     break; 
    } 
    return (INT_PTR)FALSE; 
} 
+0

那些看起來不像孩子的窗戶。 –

+0

您正在創建頂級窗口,它們之間沒有任何關係。如果要在銷燬另一個窗口時激活應用程序中的另一個頂級窗口,請先調用[SetActiveWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646311.aspx),然後然後摧毀窗戶。有關更多信息,請參見[窗口功能:活動窗口](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599.aspx#active)。 – IInspectable

回答

1

這應該是您的應用程序邏輯的一部分。將WM_CLOSE消息的事件處理程序添加到所有窗口,並將焦點設置到尚未關閉的相應窗口。

Windows無法爲您決定。

+0

嗯..我只是注意到,幾個窗口後,它不只是變得不活躍,它只是切換到其他打開的應用程序不只是這個Win32應用程序。 – stroibot

+0

Windows需要將焦點移到某處。這是最好的猜測。有時它是完美的,有時不是真的。 –

+0

所以我無能爲力? – stroibot

相關問題