Pergunta

I have a list control and a number of CDialog-derived forms without border. When user clicks on a particular list item - a particular form is displayed. I used CDialog class as a base class for these forms. Everything works well but when I press Escape key in the main window where list and these controls are situated - current form hides. How to block escape key? Should I define OnCancel method in forms' classes and prevent a dialog from closing or there are some flags that could be set to solve my problem? I've chosen mainly CDialog class as a base class in order to have DoDataExchange within form classes.

Foi útil?

Solução

I am not sure what you are referring to as form. I just assume that you are working with dialogs.

When you press Esc dialog does not hide; it is dismissed with IDCANCEL exit code. The same happens when RETURN is pressed. The difference is that exit code is set to IDOK.

Do not change Cancel handler behavior. You need it to know that user actually terminated dialog with Cancel button.

Instead, create accelerator table in the resource editor and add Esc (VK_ESCAPE) key to it. Add HACCEL type member variable to your app. In InitInstance call:

m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));

Add virtual PreTranslateMessage to yur application. Place following code in the override:

BOOL CYourApp::PreTranslateMessage(MSG* pMsg)
{
    if(TranslateAccelerator(pMsg->hwnd, m_hAccel, pMsg))
    {
        return TRUE;
    }

    return CWinAppEx::PreTranslateMessage(pMsg);
}

That is it. You even do not have to insert a handler for this accelerator, unless you want to do something else beside preventing dialog from closing when Esc is pressed.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top