简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM