We help IT Professionals succeed at work.

Default button IDOK etc. ?

hasmet
hasmet asked
on
I do not wish my dialog to close when enter key is pressed. I have an OK button. I make it non default button still dialog closes. My current solution is
void CDontcloseonreturnDlg::OnOK()
{
     // TODO: Add extra validation here
     
     if (GetFocus()->m_hWnd == GetDlgItem(IDOK)->m_hWnd)
          CDialog::OnOK();
}

But I like to know why if I select nondefault button style for
OK button, why it still behaves like a default button.

If I add a dummy button and make it default and invisible,
then I also get the behaivior I want. A gain why not simply select a style and do it ?


Comment
Watch Question

don's call CDialog::OnOK() and the dialog will not close...

Author

Commented:
Yes but I want it to close if OK button is clicked with mouse. I want to reject the enter key behaviour.
OK - I got you...

trap the key press in an override of PreTranslateMessage():

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        // get the ENTER key here...
        if (pMsg->wParam == VK_RETURN)
        {
            // do something...
        } // end if
    } // end if

    return CDialog::PreTranslateMessage(pMsg);
} // end PreTranslateMessage()

Author

Commented:
Are you telling me that there is no quick fix ?
I can tell you few more short ways of doing it, if I have to worry about messages, subclassing, hooking etc.
But I was hoping to do it by just simply turning on/of some styles such as default button !!!
Please tell me if you are %100 sure this is not the case.
Thanks.
It's always been my experience that the PreTranslateMessage() override is the thing to do... it's really a quick and easy thing... just go into ClassWizard, select the Messages tab and select the PreTranslateMessage entry in the right list box...

Author

Commented:
Frankly, I do not think that it is easier that what I already have

void CDontcloseonreturnDlg::OnOK()
{
    // TODO: Add extra validation here
   
    if (GetFocus()->m_hWnd == GetDlgItem(IDOK)->m_hWnd)
         CDialog::OnOK();
}

Do you really thing that this is a better way of doing it than PreTranslateMessage ?

By the way I am not avoiding to give you points. I am going to do so, but I really want to make sure that I am not missing something with styles. Lets wait a day in case some one comes up with a better suggestion, else I will post your points. THANKS.
I had another thought - just change the ID of the OK button to something other than IDOK and it should work...

try it out...

Commented:
hi hasmet,

  yes there is a simple way just go through the following code


     MSG msg;
     ::GetMessage(&msg,this->m_hWnd,0,0);
     if(GetFocus()->m_hWnd==GetDlgItem(IDOK)->m_hWnd && msg.message!=WM_KEYUP && msg.message!=WM_KEYDOWN)
          CDialog::OnOK();

 or u may simply check the messages WM_KEYDOWN or KEYUP..

regards,
himmya.
Vinayak KumbarSr Program Manager

Commented:
Yes, U need to go for PreTranslateMessage(). That according to me is the simple way to do. That function will look some thing like

BOOL CCryst3Dlg::PreTranslateMessage(MSG* pMsg)
{
     if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
     {
          return 1;    
     }    
     return CDialog::PreTranslateMessage(pMsg);
}

When U detect ENTER key, do not call the base class function!!! otherwise, it will close doen the dialog.

VinExpert
Commented:
Hi,
 Look at SetDefId function of the CDialog class. Using this will change the default behaviour of the OK button.
- PK

Author

Commented:
Thank you all, but SetDefId is what I was looking for.
BOOL CDontcloseonreturnDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     // Set the icon for this dialog.  The framework does this automatically
     //  when the application's main window is not a dialog
     SetIcon(m_hIcon, TRUE);               // Set big icon
     SetIcon(m_hIcon, FALSE);          // Set small icon
     
     // TODO: Add extra initialization here
     SetDefID(-1);
     return TRUE;  // return TRUE  unless you set the focus to a control
}


will do the job. It is the simplest solution.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.