如何防止按钮在重复单击时创建对话框。 [英] How to prevent button from creating dialog box on repeated clicks.

查看:171
本文介绍了如何防止按钮在重复单击时创建对话框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在点击按钮时创建了一个对话框。但是当我再次点击时,对话框正在创建。我想在第一次点击时创建它。当点击按钮时,应该禁用更多点击。你能帮我这样做吗?



我尝试过的事情:



i在点击按钮的同时创建了一个对话框。但是它会一次又一次地创建对话框进一步点击





<前lang =c ++> void CMyViewerDlg :: OnBnClickedShow()
{
m_DCM.Create(IDD_Tags, this );
m_DCM.ShowWindow(SW_SHOW);
BOOL isOn = true ;
if (isOn == false
{
GetDlgItem( IDC_SHOW) - > EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_SHOW) - > EnableWindow(FALSE);
}
}



这是代码。我是编程的初学者。告诉我如何更改代码。

解决方案

您正在创建一个无模式对话框。在这种情况下,您必须跟踪该对话框的状态。您现有的代码可以作为起点:

 void CMy3D_Med_ViewerDlg :: OnBnClickedShowDicomTag()
{
m_DCMTagDlg.Create(IDD_DICOM_Tags,this);
m_DCMTagDlg.ShowWindow(SW_SHOW);
GetDlgItem(IDC_SHOW_DICOM_TAG) - > EnableWindow(FALSE);
}



通过禁用按钮,它会变灰,再次点击时不再调用处理程序。



但是当对话框关闭时,您现在必须再次启用该按钮。通过处理 WM_CLOSE 消息来执行此操作:

  void  DCMTagDlg :: OnClose()
{
CMy3D_Med_ViewerDlg * p =(CMy3D_Med_ViewerDlg *)GetParent();
p-> GetDlgItem(IDC_SHOW_DICOM_TAG) - > EnableWindow(TRUE);
CDialog :: OnClose();
}

请注意,仅当从 CMy3D_Med_ViewerDlg 调用对话框时才能使用上述内容。





此问题还有另一种方法:子对话框永远不会关闭但只隐藏:

  void  DCMTagDlg :: OnClose()
{
ShowWindow(SW_HIDE);
}



这需要使用指向对话框而不是实例的指针(只需替换 DCMTagDlg m_DCMTagDlg 通过 DCMTagDlg * m_pDCMTagDlg 并在构造函数中使用 NULL 初始化指针:

 void CMy3D_Med_ViewerDlg :: OnBnClickedShowDicomTag()
{
if(m_pDCMTagDlg == NULL)
{
m_pDCMTagDlg = new DCMTagDlg();
m_pDCMTagDlg->创建(IDD_DICOM_Tags,this);
}
m_pDCMTagDlg-> ShowWindow(SW_SHOW);
}



请注意,当父窗口关闭时,您现在必须销毁对话框:

  void  CMy3D_Med_ViewerDlg :: OnClose()
{
if (m_pDCMTagDlg)
{
m_pDCMTagDlg-> Destroy();
delete m_pDCMTagDlg;
m_pDCMTagDlg = NULL;
}
CDialog :: OnClose();
}



使用此功能时,您仍可以禁用该按钮。如果不这样做,单击按钮将只显示现有对话框。


首次单击后禁用该按钮,请参阅CWnd :::EnableWindow [ ^ ]。


i created a dialog box while clicking on the button.but when i again clicked again dialog box is creating. i want to create it on the first click. when the button is clicked further clicks should be disabled.can u help me to do this?

What I have tried:

i created a dialog box while clicking on the button.but it is again and again creating dialog box on further click

[EDIT: Inserted code posted as comment]

void CMyViewerDlg::OnBnClickedShow()
{
    m_DCM.Create(IDD_Tags, this);
    m_DCM.ShowWindow(SW_SHOW);
    BOOL isOn = true;
    if (isOn == false)
    {
        GetDlgItem(IDC_SHOW)->EnableWindow(TRUE);
    }
    else
    {
        GetDlgItem(IDC_SHOW)->EnableWindow(FALSE);
    }
}


this is the code. Iam a beginer in programming. tell me how to change the code.

解决方案

You are creating a modeless dialog. In this case you must track the state of that dialog. Your existing code can be used as starting point:

void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
    m_DCMTagDlg.Create(IDD_DICOM_Tags, this);
    m_DCMTagDlg.ShowWindow(SW_SHOW);
    GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(FALSE);
}


By disabling the button it is grayed out and the handler is not called anymore when clicked again.

But when the dialog is closed you must now enable the button again. Do this by handling the WM_CLOSE message:

void DCMTagDlg::OnClose()
{
    CMy3D_Med_ViewerDlg *p = (CMy3D_Med_ViewerDlg *)GetParent();
    p->GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(TRUE);
    CDialog::OnClose();
}

Note that the above can be only used when the dialog is called only from CMy3D_Med_ViewerDlg.


There is also a different approach for this problem: The child dialog is never closed but only hidden:

void DCMTagDlg::OnClose()
{
    ShowWindow(SW_HIDE);
}


This requires using a pointer to the dialog instead of an instance (just replace DCMTagDlg m_DCMTagDlg by DCMTagDlg *m_pDCMTagDlg and initialise the pointer with NULL in the constructor):

void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
    if (m_pDCMTagDlg == NULL)
    {
        m_pDCMTagDlg = new DCMTagDlg();
        m_pDCMTagDlg->Create(IDD_DICOM_Tags, this);
    }
    m_pDCMTagDlg->ShowWindow(SW_SHOW);
}


Note that you now have to destroy the dialog when the parent window is closed:

void CMy3D_Med_ViewerDlg::OnClose()
{
    if (m_pDCMTagDlg)
    {
        m_pDCMTagDlg->Destroy();
        delete m_pDCMTagDlg;
        m_pDCMTagDlg = NULL;
    }
    CDialog::OnClose();
}


When using this you may still disable the button. When not doing so, clicking on the button will just show the existing dialog.


Just disable the button after first click, see CWnd::EnableWindow[^].


这篇关于如何防止按钮在重复单击时创建对话框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆