gpt4 book ai didi

c++ - Win32 C++ : Child window is not visible

转载 作者:行者123 更新时间:2023-11-30 05:16:14 33 4
gpt4 key购买 nike

我正在创建一个 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;

感谢您的宝贵时间和帮助。

更新有两个窗口的新代码,但它们似乎属于同一类,而实际上它们不是。

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);

最佳答案

您应该避免对子窗口使用 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 时它在我的 PC 上的外观截图 enter image description here

关于c++ - Win32 C++ : Child window is not visible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42734429/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com