none
why CMFCToolBarButton::HasFocus() allways return No? RRS feed

  • Question

  • afxtoolbarbutton.h

    HasFocus()like this

    	virtual BOOL HasFocus() const
    	{
    		HWND hwndBtn = ((CMFCToolBarButton*)this)->GetHwnd();
    		return hwndBtn != NULL && (hwndBtn == ::GetFocus() || ::IsChild(hwndBtn, ::GetFocus()));
    	}

    GetHwnd() like this

    	virtual HWND GetHwnd() { return NULL; }

    GetHwnd() return NULL, allways

    so

    hwndBtn =NULL,allways。

    design fault ?

    Thursday, September 26, 2019 2:45 AM

Answers

  • I think that toolbar buttons do not represent separate windows. They are drawn and managed by parent toolbar. GetHwnd is virtual and it has sense for other derived real controls, such as CMFCToolBarComboBoxButton. Returning NULL is mentioned in documentation.

    Therefore, this implementation of HasFocus is not applicable for items that are not controls (windows). And usually buttons do not receive keyboard focus.

    • Proposed as answer by Guido Franzke Thursday, September 26, 2019 6:02 AM
    • Marked as answer by 凤凰与梧桐 Friday, October 11, 2019 1:19 AM
    Thursday, September 26, 2019 4:47 AM

All replies

  • I think that toolbar buttons do not represent separate windows. They are drawn and managed by parent toolbar. GetHwnd is virtual and it has sense for other derived real controls, such as CMFCToolBarComboBoxButton. Returning NULL is mentioned in documentation.

    Therefore, this implementation of HasFocus is not applicable for items that are not controls (windows). And usually buttons do not receive keyboard focus.

    • Proposed as answer by Guido Franzke Thursday, September 26, 2019 6:02 AM
    • Marked as answer by 凤凰与梧桐 Friday, October 11, 2019 1:19 AM
    Thursday, September 26, 2019 4:47 AM
  • Thank you for answering my question.


    Friday, October 11, 2019 1:19 AM