Search code examples
mfcdialogkeyboardeditbox

How would you open a Dialog when an edit box is selected? MFC 2005


I would like to be able to create an onscreen keyboard to popup whenever an edit field is selected. I am using MFC Visual studio 2005 (C++ based).

Below is my code so far:

void CTestHarnessDlg::OnEnChangeEdit3()
{
    CKeyboard Dlg;
    Dlg.DoModal();
}

When I run the Dialog box and click on the selected field, it does not open an onscreen keyboard until I press a key on the keyboard. Is there a way to open the keyboard without putting anything into the text field?

I've been looking at ON_EN_SETFOCUS, but I'm very new to MFC. I'm not sure how to even use the CEDIT command classes within the code... Any help is appreciated, thanks!


Solution

  • How to add commands using Visual Studio Class Wizard

    in Visual Studio, open your project, then in the upper menu go to:

    • Project>Class Wizard
    • select your project and your class name(in your case CTestHarnessDlg)
    • on the Commands tab in the search field type your Edit ID
    • Select it and the ListBox called Messages will be filled with all the messages from that control
    • Select EN_SETFOCUS and press Add Handler and type a name that you want or leave the default one
    • then press OK or Edit Code and you should be right there on the method implementation
    • everything should be set and created automatically by the class wizard: Method declaration, Method Implementation, Message Map

    How to add commands manually

    • Go to your class declaration(usually in the .h file) and add the method declaration, you will have to know the type of the function you need to add

      afx_msg void OnSetfocusEdit();

    • go to the message map (usually in the cpp file) and add the mapping, you will have to know the macro that you have to use, in this case ON_EN_SETFOCUS

      ON_EN_SETFOCUS(IDC_YOUR_EDIT_ID, &CTestHarnessDlg::OnSetfocusEdit)

    • go to your cpp (usually in the cpp file) and add the method implementation

    void CTestHarnessDlg::OnSetfocusEdit()
    {
        TCHAR sysDir[MAX_PATH];
        if( !GetSystemDirectory( sysDir,  MAX_PATH) )
        {
            ASSERT(FALSE);
            return;
        }
        ShellExecute(NULL, NULL, L"osk.exe", _T("") , sysDir, SW_SHOW);
    }
    

    using osk.exe

    the command ShellExecute(NULL, NULL, L"osk.exe", _T("") , sysDir, SW_SHOW); will open window's on screen virtual keyboard, you don't ahve to create your own keyboard dialog there is already one by default on windows

    not using osk.exe

    you will have to create your own dialog (CKeyboard) but IMO you shouldn't use CDialog::DoModal method, you should make the dialog modeless using CDialog::Create then use CWnd::ShowWindow and then use CWnd::SetWindowPos to move your dialog where you want.