Distinguish modeless vs modal dialog - mfc

I want use the same class CTestDialog for a modal dialog
CTestDialog dlg;
dlg.DoModal();
and for a modeless Dialog
m_pDlg = new CTestDialog;
m_pDlg->Create(CTestDialog::IDD,this);
m_pDlg->ShowWindow(SW_SHOW);
The problem I have is at PostNcDestroy(..) it crashes if it is constructed as modal Dialog:
void CTestDialog::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this; // <= need for modeless, but Crash! if constructed as modal Dialog
}
How can I determine, in a straightforward way, if the class was constructed as modeless or modal dialog?

Check the dialog's m_nModalResult. If it is -1 - the dialog was modeless; otherwise it will be one of IDOK, IDCANCEL, etc. codes.
[Edited to answer the comment]
This is different from the original question.
In the OK/Cancel handler, you can test:
if (m_nFlags & WF_MODALLOOP)

I have abandoned searching a solution if the MFC Dialog itself can distingish between modeless vs modal dialog.
This workaround works for me.
I have extended the constructor to tell if it is modeless or modal.
CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/, BOOL bModeless /*=false*/)
: CDialogEx(CTestDialog::IDD, pParent)
, m_bModeless(bModeless)
{
}
void CTestDialog::PostNcDestroy()
{
CDialogEx::PostNcDestroy();
if (m_bModeless)
delete this;
}
void CTestDialog::OnOK()
{
if (UpdateData(TRUE))
{
if (m_bModeless)
DestroyWindow();
else
CDialogEx::OnOK();
}
void CTestDialog::OnCancel()
{
if (m_bModeless)
DestroyWindow();
else
CDialogEx::OnOK();
}

Related

Apply button in CDialog

I have a dialog in which after pressing button OK, the program uses the data in the dialog and draws a plot. I need to draw the plot without having to close the dialog as with IDOK, hence the apply button.
The code with drawing the dialog is,
INT_PTR val = dlg->DoModal();
if ( val == IDOK) {
//draw plot
}
The code of onOK and onApply
void DLg::OnOK() {
GetDataGrid();
CDialog::OnOK();
}
void DLg::OnBnClickedApply()
{
GetDataGrid();
}
How do I get DoModal() to return a value on onApply() without closing the dialog?
Any help would be appreciated.
A modal dialog can't return a value and leave the dialog open. You could either make your dialog non-modal, or post your main window a message from the OnBnClickedApply function that makes it draw the plot.
I tend to put drawing into a separate thread and would call it wherever needed. So you can either
(1) call the OnDrawPlot again in your Apply button
if ( val == IDOK) {
AfxBeginThread(...);//draw plot
}
void DLg::OnBnClickedApply()
{
AfxBeginThread(...);//draw plot
}
(2) send the return value back to the DoModal using EndDialog method
What parameters are there in EndDialog ?
An example can be found here.
Declare a variable in CDialog derived class preferably public. Then just at OnOK assign this variable to appropriate value. The caller would use it directly.
class Dlg : public CDialog
{
public:
int TheVariable;
...
};
Call site:
if(dlg.DoModal()==IDOK)
{
dlg.TheVariable; // Use the variable
}
However, if you need to draw on the dialog itself (and not to other window, which has launched the dialog), then don't call CDialog::OnOK or EndDialog in your OnOK override. In this case, you need to do painting in dialog itself.

Qt Run Single Application

I have a few dialogs and buttons that call these dialogs. However, every click on the button calls a new dialog window. I want the existing window to close first before the user can click on the button to open another.
Below is an example of a button calling a slot. Whenever I click on the button, it will call a copy of the dialog window. Is there any way to only call one copy of the dialog window only?
Thanks.
Bookmark.cpp:
Bookmark::Bookmark()
{
createButtons();
connect(bookmarkButton, SIGNAL(clicked()), this, SLOT(openBookmarkDlg()));
}
void Bookmark::createButtons()
{
...
bookmarkButton = new QToolButton;
bookmarkButton->setText("Bookmark");
addWidget(bookmarkButton);
...
}
void Bookmark::openBookmarkDlg()
{
BookmarkDlg *bkDlg = new BookmarkDlg;
bkDlg->show();
}
Bookmark.h:
class Bookmark : public QToolBar
{
Q_OBJECT
public:
Bookmark(void);
~Bookmark(void);
public slots:
void openBookmarkDlg();
private:
createButtons();
QToolButton *bookmarkButton;
};
If you want the dialog window to be modal, i.e. the application doesn't accept user input outside the dialog, use the window modality of the dialog.
Beware however that modal windows can be really annoying to users.
If BookmarkDlg inherits QDialog you can do the following:
void Bookmark::openBookmarkDlg()
{
BookmarkDlg *bkDlg = new BookmarkDlg;
prepareYourDialog(bkDlg);
/*If you expect to do something when the dialog is accepted*/
if (bkDlg->exec() == QDialog::Accepted)
{
/*Do something after dialog was accepted, and nothing when canceled*/
}
delete bkDlg;
}
Convert BookmarkDlg *bkDlg into a variable member of the class, instead of a local variable of the method:
private:
createButtons();
QToolButton *bookmarkButton;
BookmarkDlg *bkDlg;
Then on the implementation of the class, you could do:
void Bookmark::openBookmarkDlg()
{
if (!bkDlg)
bkDlg = new BookmarkDlg;
bkDlg->show();
}

WM_KILLFOCUS for a modal dialog does not work

In my application I have a standard MFC modal dialog. I'd like close that dialog when the user clicks outside the dialog window. For that purpose I put ON_MESSAGE(WM_KILLFOCUS, OnKillFocus) in the dialog's message map hy hand (the class wizard does not offer that option):
BEGIN_MESSAGE_MAP(CTestTreeCtrlDlg, CDialog)
//{{AFX_MSG_MAP(CTestTreeCtrlDlg)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_KILLFOCUS, OnKillFocus)
END_MESSAGE_MAP()
...
void CTestTreeCtrlDlg::OnKillFocus()
{
...
}
Now if I click outside the dialog, the latter of course looses focus, but the OnKillFocus method won't get called for some reason.
Thank you patriiice !
WM_ACTIVATE does the job:
BEGIN_MESSAGE_MAP(CTestTreeCtrlDlg, CDialog)
//{{AFX_MSG_MAP(CTestTreeCtrlDlg)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree)
//}}AFX_MSG_MAP
ON_WM_ACTIVATE()
END_MESSAGE_MAP()
...
void CTestTreeCtrlDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CDialog::OnActivate(nState, pWndOther, bMinimized);
if (nState == WA_INACTIVE)
OnOK() ;
}
Quick search -> http://www.itlisting.org/5-windows/964b01901673b4b0.aspx
I am pretty sure it is a better approach to do this.

mfc access to formview item from dialog box

In my SDI application i need to get this behawiour. After I click on a button on the FormView, a CDialog opens. When I press the OK button on the CDialog, I call a function of the FormView. I don't want to close the CDialog. I try to do it with modeless dialog, but when i call formview function from dialog, i can't access to formview's control, like it's lost hwnd; the error is can't read memory of m_hwnd, the hwnd is ???.
This is my code:
Open modeless dialog:
CCampiDlg *m_pDialog = NULL;
HWND hCampi = NULL;
// Invoking the Dialog
m_pDialog = new CCampiDlg;
if (m_pDialog != NULL)
{
BOOL ret = m_pDialog->Create(m_pDialog->IDD, this);
if (!ret) //Create failed.
{
AfxMessageBox(_T("Error creating Dialog"));
}
m_pDialog->ShowWindow(SW_SHOW);
}
when i press the ok button in the dialog i do:
CEditorTxView pView;
box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2));
box2->GetWindowTextW(campo);
pView.inserisciCampo(1, campo);
In inserisciCampo function in CEditorTxView (CFormView) i have to do operation with my control txtCtrl, but it's lost hwnd. The declaration of txtCtrl is in the CEditorTxView.h
CTx1 txtCtrl;
And initialize it in DoDataExchange function:
void CEditorTxView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TX1, txtCtrl);
}
Someone can help me plz?
I can give you two answers here:
How to do what you are asking (get access to a control of the CFormView from a modeless dialog)
How to solve your underlying problem (communicate changes in a modeless dialog to the owner view)
For the first one, you have to declare a pointer to the view in the dialog class and initialize it in the constructor of the view:
class CCampiDlg : public CDialog
{
public:
CCampiDlg(CEditorTxView* pView, CWnd*pParent = NULL) // Change declaration to add pointer to view
: m_pView(pView)
{
}
// ... Whatever
private:
CEditorTxView* m_pView;
}
Now in your button handler:
CEdit* box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2)); // Why not use a control variable?
box2->GetWindowTextW(campo);
m_pView->inserisciCampo(1, campo);
This should do what you are asking for. However, it is the wrong way to do it.
The problem with this approach is that the dialog knows way too much about its parent. It knows it is of type CEditorTxView and that it has a member called inserisciCampo, that takes a number and some text.
It shouldn't know that much. In fact, knowing anything about it, other than it is of type CView or even CWnd, is too much.
If the dialog knows about the view, you can't reuse the dialog with other views, and anytime the view changes its representation (what now is a textbox may be a combobox in the future, for example) the dialog must change accordingly.
The solution would be to send a message to the parent, explaining what's happened. Then the parent (the view) should know haw to handle that event. For example:
class CCampiDlg : public CDialog
{
public:
CCampiDlg(CWnd*pParent = NULL) {}
protected:
OnOk()
{
CString campo;
c_CampiBox2.GetWindowText(campo);
GetParent()->SendMessage(UWM_CAMPO2_SET, 0, (LPARAM)&campo);
}
}
In the view:
// It can be ON_REGISTERED_MESSAGE:
ON_MESSAGE(UWM_CAMPO2_SET, OnCampo2Set)
//...
LRESULT CEditorTxView::OnCampo2Set(WPARAM, LPARAM lParam)
{
CString* s = (CString*) lParam;
inserisciCampo(1, *campo);
return 0;
}
Now, you have decoupled the view and the dialog. The dialog knows nothing about the view. You can change its type, change the representation, even make it a dialog, and you don't have to change anything in the dialog. And if you need that same modeless dialog somewhere else, you just drop it there, create a message handler in the parent, and voilĂ !
For further explanations and better examples, check these articles:
Dialog and control design (Your case is explained in the section "Notifications to the environment")
Message management
On Ok button click the below code is running:
CEditorTxView pView;
box2 = (CEdit*)(GetDlgItem(IDC_CAMPI_BOX2));
box2->GetWindowTextW(campo);
pView.inserisciCampo(1, campo);
Note that, you are creating the new pView in stack and it does't attach with any window. You are not actually referring the view that already created and launched your dialog acting a parent. Revisit the above code and try the get the view:
Try the below code, if it is not working (Google it)
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
CView * pView = pFrame->GetActiveView();

MFC: Creating modeless dialog box without displaying

I'm trying to create a simple modeless dialog box which I'm creating from my CWinApp derived InitInstance() function.
BOOL CMyApp::InitInstance()
{
...
m_pMyDialog = new CMyDialog();
m_pMyDialog->Create(CMyDialog::IDD);
...
retrun TRUE;
}
I've created the dialog template in the resource editor and the WS_VISIBLE bit is unset. My intention is to avoid showing the dialog until I explicitly call ShowWindow(SW_SHOW) but for some reason the call to Create displays the dialog.
I've tried to change the return value of OnInitDialog() to FALSE but that doesn't work.
I've even tried to call ModifyStyle() in case something else is setting the WS_VISIBLE bit.
int CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
ModifyStyle(WS_VISIBLE, 0);
return 0;
}
That doesn't work either. In all cases, after I call Create the dialog is displayed which isn't how I've read it should work.
The problem was with AnimateWindow() which was causing the dialog to display prematurely.