c++ - 关闭一个窗口后如何防止Win32应用程序其他窗口变为非事件状态

标签 c++ windows winapi

所以我的任务是创建 5 个子窗口,并在按下键时以其他顺序关闭窗口。但是我有一个问题,当我关闭前 2 个窗口时,其他窗口变为非事件状态,所以我必须单击它以便它可以成为焦点窗口,但是如何防止其他窗口变为非事件状态?

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

最佳答案

这应该是您的应用程序逻辑的一部分。向所有窗口的 WM_CLOSE 消息添加事件处理程序,并将焦点设置到尚未关闭的适当窗口。

Windows 无法为您决定。

关于c++ - 关闭一个窗口后如何防止Win32应用程序其他窗口变为非事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896589/

相关文章:

c++ - 在 C++ 中每 30 秒检查一次进程

c# - native 文件打开/保存对话框(没有 Windows 窗体或 WPF)

c++ - 未定义的函数接口(interface)引用::接口(interface)(int)

c++ - 编写 Makefile.am 来调用 googletest 单元测试

c++ - 如何访问arduino库中的枚举变量

c++ - 从 std::system 调用执行时,awk 的输出重定向失败

c++ - 设备驱动程序内存缓冲区处理器缓存问题

c++ - 更改控制台颜色并保留 C++ 中的输出

java - 如何以编程方式清除 Windows 中的缓存?

c# - 了解 AttachThreadInput - 分离失去焦点