심상원
심상원

Reputation: 75

How to set control focus inside an MFC custom control

Experts!

I am using a class that inherits CWnd to make the content visible using a horizontal scroll bar

The control I want to create looks like this:

enter image description here

However, I have some problems and leave a question

When the button receives focus, it changes to blue. If another button is pressed, the button that received the existing focus should be unfocused.

enter image description here

The button does not release focus as shown in the second picture.

However, the above problem occurs when implemented in Dialog, not in SDI.

I need help solving this problem.

Custom Control Create Code;

m_ScrollWnd.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, 1234);

BOOL CScrollWnd::Create(DWORD dwStyle, CRect &rect, CWnd *pParent, UINT nID)
{
    dwStyle |= ((WS_HSCROLL) );

    return CWnd::Create(CScrollWnd::IID, nullptr, dwStyle, rect, pParent, nID);
}


m_Button3.Create(_T("Hello3"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 0, 0), this, 1238);

Upvotes: 0

Views: 908

Answers (2)

SevenWow
SevenWow

Reputation: 315

Official document from MSDN: Dialog Box Keyboard Interface

BTW, xMRi explains it very well.

Upvotes: 0

xMRi
xMRi

Reputation: 15375

The so called "default button handling" is done by a function named IsDialogMessage.

The easiest way to control this is to make your parent control a window derived from CDialog, or if it's a view derive from CFormView. The MFC will handle all this for you in the appropriate PreTranslateMessage handler.

If you want to do this by your own you might insert your own PreTranslateMessage handler and use IsDialogMessage. The CWnd class also has a predefined implementation named CWnd::PreTranslateInput.

So this might be sufficient:

BOOL CYourParentClass::PreTranslateMessage(MSG* pMsg)
{
    // allow standard processing
    if (__super::PreTranslateMessage(pMsg))
        return TRUE;
    return PreTranslateInput(pMsg);
}

Using CFormView / CDialog is the better way from my point of view, because also other "problematic things about dialogs" are solved in it. Including loosing and getting focus and activation...

Upvotes: 1

Related Questions