简体   繁体   English

如何在MFC自定义控件中设置控件焦点

[英]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 我正在使用一个继承CWnd的类,以便使用水平滚动条使内容可见

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. 但是,上述问题是在Dialog中而不是在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 . 所谓的“默认按钮处理”是通过名为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 . 控制它的最简单方法是使父控件成为一个从CDialog的窗口,或者如果它是一个从CFormView派生的视图。 The MFC will handle all this for you in the appropriate PreTranslateMessage handler. MFC将在适当的PreTranslateMessage处理程序中为您处理所有这一切。

If you want to do this by your own you might insert your own PreTranslateMessage handler and use IsDialogMessage . 如果要自己执行此操作,则可以插入自己的PreTranslateMessage处理程序并使用IsDialogMessage The CWnd class also has a predefined implementation named CWnd::PreTranslateInput . CWnd类还具有一个名为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. 从我的角度来看,使用CFormView / CDialog是更好的方法,因为还解决了其他“有关对话框的问题”。 Including loosing and getting focus and activation... 包括放松,获得关注和激活...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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