Reputation: 23
I'm creating a win32 application in which child window should be a ball inside the parent window. It compiles without errors, but only main window is displayed, what can be the problem here? Here is my code.
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;
}
Thanks for your time and help.
UPDATE New code with two windows, but both seem to be of the same class while they are not.
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);
Upvotes: 1
Views: 1856
Reputation: 1917
You should avoid using WS_OVERLAPPED_WINDOW for child windows. Your ChildProc should look like this in order to paint the child window in red:
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;
}
I've also attached a screenshot of how it looks on my PC when I add the ChildProc
Upvotes: 1