[英]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. 我需要绘制图而不必像使用IDOK那样关闭对话框,因此需要应用按钮。 The code with drawing the dialog is, 绘制对话框的代码是,
INT_PTR val = dlg->DoModal();
if ( val == IDOK) {
//draw plot
}
The code of onOK and onApply onOK和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? 如何获取DoModal()以在onApply()上返回值而不关闭对话框?
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. 您可以使对话框为非模态对话框,也可以通过OnBnClickedApply
函数在主窗口中发布一条消息来绘制该图。
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 (1)在“应用”按钮中再次调用OnDrawPlot
if ( val == IDOK) {
AfxBeginThread(...);//draw plot
}
void DLg::OnBnClickedApply()
{
AfxBeginThread(...);//draw plot
}
(2) send the return value back to the DoModal using EndDialog method (2)使用EndDialog方法将返回值发送回DoModal
What parameters are there in EndDialog ? EndDialog中有哪些参数? An example can be found here. 可以在此处找到示例。
Declare a variable in CDialog
derived class preferably public
. 在CDialog
派生类中声明一个变量,最好是public
。 Then just at OnOK
assign this variable to appropriate value. 然后在OnOK
将此变量分配给适当的值。 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. 但是,如果您需要在对话框本身上绘制(而不是在已启动该对话框的其他窗口上绘制),则不要在OnOK
覆盖中调用CDialog::OnOK
或EndDialog
。 In this case, you need to do painting in dialog itself. 在这种情况下,您需要在对话框本身中进行绘制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.