C ++ Winapi,弹出窗口将打开一次但不会重新打开

时间:2015-10-14 19:16:22

标签: c++ winapi button popup

我正在使用Windows,Visual Studio 2015.我遇到的问题是我有一个按钮,会打开一个子弹出窗口,第一次就可以了,但是一旦关闭弹出窗口并单击按钮再次,它不会重新开放。

我有一个全局变量hwnd2,因为目前我不知道如何在另一个函数中访问该变量,除非它是全局的。只是在你们之前让你知道“eww,全局变量”

可能有一些完全错误但是在互联网上搜索不断让我这么做,没有人真正具体/深入。

#include "stdafx.h"
#include "Win32Project1.h"
#include <Windows.h>

#define IDB_CLICK_ME 200
HWND hwnd2;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine , int iCmdShow)
{
    wchar_t wzAppName[] = L"GameLauncher";
    wchar_t wzAppName2[] = L"GameTest";
    MSG msg;
    HWND hwnd;
    WNDCLASS wndclass, wndclass2;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hInstance = hInstance;
    wndclass.lpszClassName = wzAppName;
    wndclass.lpszMenuName = NULL;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;

    wndclass2.lpfnWndProc = WndProc2;
    wndclass2.cbClsExtra = 0;
    wndclass2.cbWndExtra = 0;
    wndclass2.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass2.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass2.hInstance = hInstance;
    wndclass2.lpszClassName = wzAppName2;
    wndclass2.lpszMenuName = NULL;
    wndclass2.style = CS_HREDRAW | CS_VREDRAW;


    RegisterClass(&wndclass);
    RegisterClass(&wndclass2);

    hwnd = CreateWindow(wzAppName, L"Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    hwnd2 = CreateWindow(wzAppName2, L"Options", WS_POPUPWINDOW | WS_CAPTION, 100, 100, 500, 300, hwnd, NULL, NULL, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message)
    {
        case WM_CREATE:
            CreateWindow(L"button", L"CLICK ME", WS_CHILD | WS_VISIBLE, 5, 5, 200, 25, hwnd, (HMENU) IDB_CLICK_ME, NULL, NULL);
            break;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            DrawText(hdc, L"Hello", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hwnd, &ps);
            break;

        case WM_COMMAND:
            switch (LOWORD(wparam));
            {
                case IDB_CLICK_ME:
                    ShowWindow(hwnd2, 1);
                    UpdateWindow(hwnd2);
                    break;

            }

        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, message, wparam, lparam);
    }

    return 0;
}

LRESULT CALLBACK WndProc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    switch (message)
    {
        case WM_CREATE:
            break;

        case WM_DESTROY:
            return 0;

        default:
            return DefWindowProc(hwnd, message, wparam, lparam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

当您关闭弹出窗口时,它不仅仅是隐藏的。它被摧毁了。如果您想要一种不同的行为(WM_CLOSE只需要调用DefWindowProc),您就可以处理DestroyWindow