Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dialog Box application 2

Status
Not open for further replies.

BobbyB

Programmer
Feb 19, 2002
44
CA
I've started a dialog box application and I'd like to prevent a window from closing when someone push the "ESC" or "RETURN" key...

thank you!
 
from the classwizard, override the preTranslateMessage. The code below will filter RERTURN and ESC key.

BOOL CTestDlgDlg::preTranslateMessage(MSG* pMsg)
{

if (pMsg->message == WM_KEYDOWN &&(pMsg->wParam == VK_RETURN
|| pMsg->wParam == VK_ESCAPE)) return true;

else return CDialog::preTranslateMessage(pMsg);
}
 
This work really fine!!! Thank you Jeffray!

But maybe a little better than what I was expecting... The "ESC" handle is great but the "RETURN" takes over some events like when a button is pressed(by the keyboard RETURN key)...

If I don't handle the "RETURN"... the application works fine when I'm on a button, but the dialog box will close if I'm on a CListBox for example...

Is there another way to handle this problem?

 
I thought you might still have the problem with the enter key.

The solution to this is:
1. add a variable in your dialog class:
bool bEnterKey;
make sure to initialize it to false.
2. change your PreTranslateMessage to :

BOOL CTestDlgDlg::preTranslateMessage(MSG* pMsg)
{

if (pMsg->message == WM_KEYDOWN){
if( pMsg->wParam == VK_ESCAPE) return true;
else if (pMsg->wParam == VK_RETURN) bEnterKey = true;
}

return CDialog::preTranslateMessage(pMsg);
}

3. overide the IDOK message handler if it is not.
add the code below to it:

void CTestDlgDlg::OnOK()
{
if (bEnterKey){
bEnterKey = false;
return;
}
...
CDialog::OnOK();
}

4. do the same to the IDCANCEL message

 
I've used a part of your suggestion...

I've overide the OnOK and OnCancel like this...

.h
virtual void OnOK();
virtual void OnCancel();

.cpp
void OnOK(){}
void OnCancel(){}

... it works great for some forms, but for others, it seems that the ESC or RETURN just don't used these override methods... I have no idea why for now... (I've tested this by putting &quot;cout << &quot;ok&quot; << endl;&quot; in the OnOK override method)

thanks for the help...
 
can you give me more details about the dialog it did not work?
 
I've put the code that I thought was useful... For this case, the ESC key calls OnCancel(that's ok)... but when I'm on a CTreeCtrl(or any other control that don't use the RETURN key) in my dialog box and I push RETURN... it's the OnClick event of the BtnOk that is call... seems kind of weird.... thanks for the help.


DlgFiles::DlgFiles(...) : CDialog(DlgFiles::IDD, pParent){
...
}

void DlgFiles::DoDataExchange(CDataExchange* pDX){
...
}

BEGIN_MESSAGE_MAP(DlgFiles, CDialog)
//{{AFX_MSG_MAP(DlgFiles)
ON_BN_CLICKED(IDC_BTN_DBSELECT, OnBtnDbselect)
ON_BN_CLICKED(IDC_BTN_REFRESH, OnBtnRefresh)
ON_NOTIFY(NM_DBLCLK, IDC_TREE_FILES, OnDblclkTreeFiles)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_FILES, OnSelchangedTreeFiles)
ON_CBN_SELCHANGE(IDC_LST_SERVER, OnSelchangeLstServer)
ON_BN_CLICKED(IDC_BTN_OK, OnBtnOk)
ON_BN_CLICKED(IDC_BTN_CANCEL, OnBtnCancel)
ON_WM_CLOSE()
ON_CBN_EDITCHANGE(IDC_LST_SERVER, OnEditchangeLstServer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void DlgFiles::OnBtnOk(){
cout << &quot;btnOK&quot; << endl;
}

void DlgFiles::OnOK(){cout << &quot;ok&quot; << endl; }
void DlgFiles::OnCancel(){cout << &quot;cancel&quot; << endl;}


 
I think this is the funtamental windows behavior.

there is a default button setting in your dialog. usually this is your ok button unless you change it to other buttons. when you move the focus to the controls other than buttons, the border of your default button changes to black. Thus when you press the enter key, the message will be mapped to the click envent of your default button.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top