Win32 C ++:子窗口不可见

时间:2017-03-11 11:07:26

标签: c++ winapi

我正在创建一个win32应用程序,其中子窗口应该是父窗口内的一个球。它编译没有错误,但只显示主窗口,这可能是什么问题?这是我的代码。

LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINPR_LAB02, szWindowClass, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINPR_LAB02, szWindowClass1, MAX_LOADSTRING);

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_WINPR_LAB02));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(255, 255, 0));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINPR_LAB02);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex);

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW^WS_THICKFRAME,
    (GetSystemMetrics(SM_CXSCREEN) - 200) / 2, (GetSystemMetrics(SM_CYSCREEN) - 300) / 2, 200, 300, nullptr, nullptr, hInstance, nullptr);
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
    WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, (255 * 50) / 100, LWA_ALPHA);

WNDCLASSEXW wcex1;
memset(&wcex1, 0, sizeof(wcex1));
wcex1.style = CS_HREDRAW | CS_VREDRAW;
wcex1.lpfnWndProc = (WNDPROC)ChildProc;
wcex1.cbClsExtra = 0;
wcex1.cbWndExtra = 0;
wcex1.hInstance = hInstance;
wcex1.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINPR_LAB02));
wcex1.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex1.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
wcex1.lpszMenuName = nullptr;
wcex1.lpszClassName = (LPCWSTR)szWindowClass1;
wcex1.hIconSm = LoadIcon(wcex1.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex1);

HWND hWnd1 = CreateWindow(szWindowClass1, szTitle1, WS_CHILD | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    (GetSystemMetrics(SM_CXSCREEN) - 200) / 2, (GetSystemMetrics(SM_CYSCREEN) - 300) / 2, 200, 300, hWnd, nullptr, hInstance, nullptr);

SetWindowLong(hWnd1, GWL_STYLE, 0); //remove all window styles, check MSDN for details
HRGN region1 = CreateEllipticRgn(0, 0, 10, 10);
SetWindowRgn(hWnd1, region1, true);

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

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINPR_LAB02));
MSG msg;

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

return (int) msg.wParam;

}

感谢您的时间和帮助。

UPDATE 带有两个窗口的新代码,但两者似乎都是同一个类,而不是。

WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;// | CS_PARENTDC;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINPR_LAB02));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(255, 255, 0));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINPR_LAB02);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex);

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW^WS_THICKFRAME,
    (GetSystemMetrics(SM_CXSCREEN) - 200) / 2, (GetSystemMetrics(SM_CYSCREEN) - 300) / 2, 200, 300, nullptr, nullptr, hInstance, nullptr);
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
    WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, (255 * 60) / 100, LWA_ALPHA);

WNDCLASSEXW wcex1;
wcex1.cbSize = sizeof(WNDCLASSEX);
wcex1.style = CS_HREDRAW | CS_VREDRAW;
wcex1.lpfnWndProc = (WNDPROC)ChildProc;
wcex1.cbClsExtra = 0;
wcex1.cbWndExtra = 0;
wcex1.hInstance = hInstance;
wcex1.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINPR_LAB02));
wcex1.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex1.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
wcex1.lpszMenuName = nullptr;
wcex1.lpszClassName = szWindowClass1;
wcex1.hIconSm = LoadIcon(wcex1.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex1);

HWND hWnd1 = CreateWindow(szWindowClass1, szTitle1, WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
    0, 0, 350, 300, hWnd, nullptr, hInstance, nullptr);
HRGN region1 = CreateEllipticRgn(0, 0, 75, 75);
SetWindowRgn(hWnd1, region1, true); //creates circle

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

1 个答案:

答案 0 :(得分:1)

您应该避免对子窗口使用WS_OVERLAPPED_WINDOW。 您的ChildProc应如下所示,以便将子窗口绘制为红色:

LRESULT CALLBACK ChildProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

当我添加ChildProc时,我还附上了它在我的电脑上的外观截图 enter image description here