Reputation: 81
I'm trying to make a second dialogbox prompt the user after they click OK on the first dialogbox while destroying the first dialogbox in the process.
This is my first dialogbox that pops up after the user click a button on the main window itself.
void displayDialogW(HWND hWnd)
{
HWND hDlg = CreateWindowW(L"myDialogClass", L"Enter Desired Width", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 0, 150, 300, 150, hWnd, NULL, NULL, NULL);
CreateWindowW(L"static", L"Width: ", WS_VISIBLE | WS_CHILD | WS_BORDER, 30, 20, 100, 20, hDlg, NULL, NULL, NULL);
CreateWindowW(L"edit", L"...", WS_VISIBLE | WS_CHILD | WS_BORDER, 80, 20, 180, 20, hDlg, NULL, NULL, NULL);
CreateWindowW(L"button", L"OK", WS_VISIBLE | WS_CHILD, 120, 60, 30, 30, hDlg, (HMENU)5, NULL, NULL);
TCHAR buff[1024];
GetWindowText(hDlg, buff, 1024);
desiredWidth = _wtoi(buff);
EnableWindow(hWnd, false);
}
The second dialogbox is more or less the same as the first but I'm not sure how to manipulate the button on the first dialogbox to make sure it opens the second dialogbox and destroys the first window at the same time.
I found a function called DestroyWindow but it needs a hDlg input so I can't exactly put it in my dialogprodecure command function. So, I'm not too sure how I would go about this.
Upvotes: 1
Views: 101
Reputation: 6289
If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.
You can hide the parent window after opening the child window, like this:
case ID_BUTTON1:
{
displayDialogW(hWnd);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_HIDEWINDOW);
}
Updated:
From A window can have a parent or an owner but not both,
Note that changing a window’s parent or owner is not a normal operation.
But if you must destroy the parent window, then you can implement it according to Reinstate Monica's comment, using SetParent.
Upvotes: 1
Reputation: 17638
The second dialog cannot have the first dialog as a parent if that's going to be destroyed right away. You could, instead, open the second dialog with the parent set to the same parent as the first one, then the first dialog can be safely destroyed.
case IDOK: // assuming hWnd is first dialog
{
createSecondDialog(GetWindow(hWnd, GW_OWNER)); // open second dialog
DestroyWindow(hWnd); // close first dialog
}
This is using GetWindow(hWnd, GW_OWNER)
rather than GetParent(hWnd)
since displayDialogW
creates a window with WS_OVERLAPPED
style, and in that case what is being passed into the CreateWindowW
call must be the owner of the new window (per the docs for owned windows).
Upvotes: 1