简体   繁体   中英

Determining type of MFC object from HWND

Further to this question I have decided to overwrite the PreTranslateMessage function of my main window CMainFrame to check if a WM_MOUSEWHEEL message has been sent, and if it has and the target of the message is a combo box then prevent the message from being dispatched.

However, I am having an issue determining if the target of the message is a combo box, here is what I am currently trying:

BOOL CMainFrame::PreTranslateMessage( MSG* pMsg )
{
      CWnd* pWnd = CWnd::FromHandle( pMsg->hwnd );
      if( pWnd )
      {
            if( pMsg->message == WM_MOUSEWHEEL )
            {
                    CRuntimeClass* pRuntimeClass = pWnd->GetRuntimeClass();
                    bool bIsCombo = pRuntimeClass->IsDerivedFrom( RUNTIME_CLASS(CComboBox) ) || pWnd->IsKindOf( RUNTIME_CLASS(CComboBox) );

                    if( bIsCombo && !reinterpret_cast<CComboBox*>(pWnd)->GetDroppedState() )
                         return TRUE;
            }
       }

       return CFrameWndEx::PreTranslateMessage( pMsg );

}

However, this doesn't work because the runtime class always seems to be CWnd , so I'm curious to know if there's a way to get this to work? Using a dynamic_cast from CWnd* to CComboBox* also didn't appear to work.

Thanks in advance!

GetClassName , which is what Captain Obvlious shows above is workable solution and will do what you want.

Of course, the sane solution (which I show in the question you link to) is to simply create a class derived from CComboBox and handle the WM_MOUSEWHEEL there in whichever way you feel is appropriate for your application. Then you can use the derived class instead of CComboBox simply by changing CComboBox to CNoScrollComboBox or whatever you name your class.

It's really quite simple and saves you a whole lot of trouble .

If the parent window does not subclass the combo box with CComboBox (or other suitable CWnd derived class) MFC will return a pointer to a temporary CWnd object. You have two options here. Subclass the combobox window when the parent is created (in OnCreate on OnInitDialog ) -OR- call GetClassName() and check the type of the target window when you handle the mouse wheel message.

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