Cannot Close a CDialog Box immediately when focus is on an inner CEdit control

I have the following dialog box -

1.  My outer dialog box is standard and is derived from CDialog
2.  It contains a CListBox control
3.  There is a CEdit control that appears over each row of the CListbox as the user is editing a series of numbers (the user has to enter a series of numbers in order down a column)

If the user clicks the Cancel button to get out he or she has to click it twice.  The first time takes the focus away from the CEdit control.  The second time the cancel is actually processed and the dialog box closes.

How do I get around this so that the first click on the cancel button closes the dialog?

-Philip T
LVL 1
PTRUSCOTTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Is the CEdit control a standard CEdit or one you have customised?
Do you have any code that responds to the cancel button?
KurtVonCommented:
You could catch the loss of focus to the edit/list control using the WM_SETFOCUS message, then, if the mouse was clicked over your dialog (or a control in the dialog) resend the mouse click with the SendInput function.

Hope this helps.
PTRUSCOTTAuthor Commented:
This is a standard CEdit control.  I have not added any code that responds to the Cancel button or any other button.  To the best of my knowledge the CEdit control is therefore responding in the normal way to mouseclicks.
PTRUSCOTTAuthor Commented:
Could you go into a little more detail about how to use the WM_SETFOCUS message?  I tried going into ClassWizard to generate a funcion that uses it but it did not come up in the list of WM_ messages.  My list goes straight from WM_SETCURSOR to WM_SHOWWINDOW.

-Philip T.
KurtVonCommented:
Well, you have a CEditControl over a CListBox.  Since the CListBox does not make a CEdit control itself, you must have added code to display that.  When you create the CEdit, is the parent window the CListBox or the CDialog?

And you are right, the WM_SETFOCUS message does not seem to be in the class wizard (I'm surprised I never noticed that).  It does exist, though, and is the opposite of the WM_KILLFOCUS message that is in the list.  There is an ON_WM_SETFOCUS macro for the message list, and the prototype for the functions it calls is

afx_msg void OnSetFocus(CWnd* pOldWin);

So you can add it by hand.

You probably want the prototype to do something like

void CMyDialog::OnSetFocus(CWnd* pOldWin)
{
    // If the old window was the edit control
    if (GetDlgCtrl(IDC_LISTBOXEDITCONTROL) == pOldWin)
    {
        CRect rcWindow;
        CPoint ptMouse;
        GetWindowRect(rcWindow);
        GetCursorPos(ptMouse);

        if (rcWindow.PtInRect(ptMouse))
        {
            INPUT mouseInput;
            ::setmem(&mouseInput, 0, sizeof(mouseInput));
            mouseInput.type = INPUT_MOUSE;
            mouseInput.mi.dx = ptMouse.x;
            mouseInput.mi.dy = ptMouse.y;
            mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
            ::SendInput(1, &mouseInput, sizeof(mouseInput));
        }
    }
}

You may need to modify this if right clicks cause a focus loss too.  Especially since this could select a context menu item if there is one.  But this is the general idea.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.