How to check whether the pop up dialog is already open? - mfc

I am having a button in main dialog,if I click that,pop up dialog box will be opened.Now,I should stop creation of so many pop up dialog boxes when I click that button.So,I need to check whether the child window is opened already.Help me,how to get the handle of child window?

I'm not sure if this is something that you would want in your program but surely give it a try. May be once your work is done with the first pop up dialog box, add another button, name is "Close" in your popup box that would allow it close it and in the Event Handler for it, just type OnOK().
void CMyDlg::OnClose()
{
CDialog::OnOK();
}
..
Hope this helps.

Related

open a dialog behind a modal dialog or after it is closed

I have an application which has several background tasks running in non-GUI threads which may time to time require some user interaction so they send signal to the main thread and the corresponding slot creates a dialog and shows it to the user. Meanwhile the task thread is waiting in a blocking event loop. Once the user answers the dialog and closes it, the task event loop is signaled to quit and the task resumes.
There is however a problem. In the GUI thread I can still use the application which time to time shows some modal dialogs. When there is a modal dialog already shown and then the background tasks requests another dialog to be opened, this task-related dialog is displayed in front of the modal dialog. But this new dialog is not modal and the modal dialog is hidden behind it. The non-modal one therefore is not responsive, the application feels like it got stuck.
My idea was to display the new dialog always behind the modal dialog, which I believe I can get with QApplication::activeModalWidget(). But I do not know how to do this. How can I show a dialog behind another dialog but still in front of the main window (which is a parent of both dialogs)? I tried to call QApplication::activeModalWidget()->activateWindow() after I show the non-modal one but his causes blinking of windows and moreover I can still click into the new non-modal dialog hiding the modal one. So this is not a perfect solution.
Or do you see any other solution?
Maybe I could implement a queue of dialogs, and once there is any modal dialog visible, then the new background task-related dialog would not be shown, only enqueued and shown once the modal dialog is closed. However this feels more fragile solution to me.
Any ideas?
UPDATE: I redefined the question by adding "or after it is closed" becasue this works for me too.
I found a solution which seems to work well and which shows the non-modal dialog only after the modal dialog is closed.
QWidget *nonModalDialog = ... // creates the non-modal dialog
nonModalDialog->setAttribute(Qt::WA_DeleteOnClose);
QWidget *modalDialog = qApp->activeModalWidget();
if (modalDialog == nullptr)
{
// no modal dialog, we can show the non-modal one now
dialog->show();
}
else
{
// we must wait until the modal one is closed
QObject::connect(modalDialog, &QWidget::destroyed, nonModalDialog, &QWidget::show);
}
This seems simple and robust.
I think you are looking for QWidget::raise(). You should be able to use QApplication::activeModalWidget()->raise() after calling dialog->show() on your non-modal dialog.
If you're getting into situations where you have multiple modal and non-modal dialogs, all launched in different order, this might not be enough to solve the problem. You might raise one modal dialog only to have others behind other non-modal dialogs and end up stuck again. You should consider keeping a reference to the collection of modal dialogs that are currently active so that you can make sure they are always on top of the non-modal dialogs.

c++, mfc - messagebox display, main window has a cancel button. prevent cancel while messagebox is active

I have a queer sort of problem. Consider the following scenario:
Main window creates a child dialog on click of some button.
Child dialog does some task in a worker thread.
An error occurs during that task causing a message box to be displayed saying something along the lines of "Yikes! Something went wrong.".
Child dialog's 'Cancel' button is clicked on causing the child dialog to be closed.
Message box is still active! Clicking on anything in the message box = crash.
Pseudocode of how things are happening: (please ignore syntactic correctness here)
MainWindowClass mainObj;
void MainWindowClass::OnSomeButtonClick()
{
SomeDialogClass someDialogObj;
someDialogObj.DoModal();
}
int MainWindowClass::doTask()
{
// Do work
if(ERROR)
{
MessageBox("Yikes! Something went wrong.", "Error", MB_OK);
return ERROR;
}
}
///////////////////////////////////////////////////////////////////
// Meanwhile, in another file,
extern MainWindowClass mainObj;
void SomeDialogClass::OnCancel()
{
// Do all cleanup and close dialog
}
int SomeDialogClass::workerThreadFunc()
{
return mainObj.doTask();
}
int SomeDialogClass::DoModal()
{
AfxBeginThread(workerThreadFunc);
// Do all other work and then wait for the worker thread
}
My question is twofold:
On cancel, is there a way to know if any message boxes/dialogs in
the same application are active?
Is my entire design wrong and should I be doing something else altogether?
I thought one of the main reasons to use a modal dialog was its ability to prevent focus from going to parent dialogs. Yet, when that error message box is shown, I can happily click on the dialog and then click on 'Cancel' and pretend that the error message box never showed.
Rather than have your worker thread bring up a modal message box, it should signal the error condition back to the UI thread. My experience of MFC and multi-threading is that having all UI handled in the same thread makes things much simpler and removes these types of error. Having two threads potentially bringing up modal dialogs at the same time is asking for trouble.
I'm kind of new at this, but my suggestions would be if the child is going to be cancelled when there is an error then destroy it before launching the error.
As far as I can tell, the SomeDialogClass is modal, then the parent launches another modal dialog, the MessageBox. Now, if SomeDialogClass was modeless then the message box would appear on top since SomeDialogClass would then be a child of the entire desktop and the sole child of MainWindowClass would be the MessageBox.
I'm under the impression that modal only prevents focus from going to things underneath the modal dialog, not what is launched on top or after it is open. I've done a modal dialog, launched a modeless from it, and could move both around the screen just fine. Additionally, I created a WM_MESSAGE from the modeless dialog back to the modal to announce when it was done so the modeless could finish.

MFC dialog box continue after pressing ok

I have a custom dialog message box that pops-up when a edit control in my main dialog has wrong data.
CDlgError dlgError = new CDlgError(this);
dlgError.Create(CDlgError::IDD, this);
dlgError.m_staticMessage.SetWindowTextA("Error message!");
dlgError.ShowWindow(SW_SHOW);
//more code
I want the rest of the code to be executed only after i press an OK button in my CDlgError pop-up dialog. How can i do that?
Use DoModal instead of Create and ShowWindow to show your error dialog. e.g.
CDlgError dlgError = new CDlgError(this);
dlgError.m_strMessage = "Error message!";
dlgError.DoModal();
As you can see from the code you'll need to pass in the text and THEN set your message label inside CDlgError::OnInitDialog because the control won't be initialized before going modal.
You are creating a dialog using Create which shows a modalless dialog(you can click on other parts of application even dialog is open).
You requirement is for modal dialog where you can not click on any part of application until this dialog is closed.
To do this use DoModal function instead of create.

How to hide a modal dialogbox in MFC application?

I have a hard time hiding a modal dialog box. What I am doing is - I am trying to design a UI for my own application in MFC, kind of a setup assistant.
In the 1st dialog box I have NEXT button, so when I click that it has to hide the 1st dialog box and move on to the 2nd dialog box, where I have some controls in 2nd dialog box.
How can I achieve that?
I have never tried to hide a Modal dialog...not sure how it can be done.
Anyway, it seems to me you don't need to hide the dialog but destroy the first one and create the second one. You can use EndDialog to terminate a modal dialog.
But MFC has its own mechanism to create your own wizard, have a look at this class CPropertySheet. I am sure you can find thousand of examples, this is one.
Hope it helps.
You can use ShowWindow() function to hide modal
Its default patametet is SW_SHOW which is equal true value 1 and
To hide modal use SW_HIDE value when you click next button
You just use ShowWindow(SW_HIDE) If you make prev button you should use modal pointer
Or next modal should child modal because you cannot have prev modal variable.
I wish you understand me for my english

WinApi C++, How to open a window push down a button?

I have created a "check Box " window with a button, when the button is clicked, It should open a "Edit" window, How can I do?
thanks
It depends completely on the windowing system you are using, or the graphics library. ¿Are you using .NET? ¿MFC?
In any case, your button object will have a way to associate a function to its click event. Just write a function that does what you need (in this case, open the "Edit" window), associate this function to the click event of your button, and you are done.
Make your edit window hidden by default in the resource editor and show it with ShowWindow(hEditWnd,SW_SHOW) when the button is clicked.
You could instanciate a new window and show it on the click event