简体   繁体   中英

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:

在此处输入图片说明

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.

在此处输入图片说明

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);

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM