解析 MFC 中的 FromHandle

40 篇文章 0 订阅
6 篇文章 0 订阅

MFC 对 Windows API 进行了封装,在很多方面都会提供便利。用 FromHandle 返回临时对象的指针,就可以调用各种类的方法。临时对象会在 OnIdle 中销毁。这里对 FromHandle 的实现原理从源码上进行解析。

//
// 1
//
CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
{
    CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist
    ASSERT(pMap != NULL);
    CWnd* pWnd = (CWnd*)pMap->FromHandle(hWnd);
#ifndef _AFX_NO_OCC_SUPPORT
    pWnd->AttachControlSite(pMap);
#endif
    ASSERT(pWnd == NULL || pWnd->m_hWnd == hWnd);
    return pWnd;
}

这是 CWnd 的 FromHandle 方法,大致的意思为从 CHandleMap 中获取临时 CWnd 对象的指针。

//
// 2
//
CHandleMap* PASCAL afxMapHWND(BOOL bCreate)
{
    AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
    if (pState->m_pmapHWND == NULL && bCreate)
    {
        BOOL bEnable = AfxEnableMemoryTracking(FALSE);
#ifndef _AFX_PORTABLE
        _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
#endif


        pState->m_pmapHWND = new CHandleMap(RUNTIME_CLASS(CTempWnd),
            offsetof(CWnd, m_hWnd));
#ifndef _AFX_PORTABLE
        AfxSetNewHandler(pnhOldHandler);
#endif
        AfxEnableMemoryTracking(bEnable);
    }
    return pState->m_pmapHWND;
}

再看

#define offsetof(s,m)   (size_t)&(((s *)0)->m)

继续

//
// 3
//
CObject* CHandleMap::FromHandle(HANDLE h)
{
    ASSERT(m_pClass != NULL);
    ASSERT(m_nHandles == 1 || m_nHandles == 2);

    if (h == NULL)
        return NULL;

    CObject* pObject = LookupPermanent(h);
    if (pObject != NULL)
        return pObject;   // return permanent one
    else if ((pObject = LookupTemporary(h)) != NULL)
    {
        HANDLE* ph = (HANDLE*)((BYTE*)pObject + m_nOffset);
        ASSERT(ph[0] == h || ph[0] == NULL);
        ph[0] = h;
        if (m_nHandles == 2)
        {
            ASSERT(ph[1] == h || ph[1] == NULL);
            ph[1] = h;
        }
        return pObject;   // return current temporary one
    }

    // This handle wasn't created by us, so we must create a temporary
    // C++ object to wrap it.  We don't want the user to see this memory
    // allocation, so we turn tracing off.

    BOOL bEnable = AfxEnableMemoryTracking(FALSE);
#ifndef _AFX_PORTABLE
    _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
#endif

    CObject* pTemp = NULL;
    TRY
    {
        pTemp = m_pClass->CreateObject();
        if (pTemp == NULL)
            AfxThrowMemoryException();

        m_temporaryMap.SetAt((LPVOID)h, pTemp);
    }
    CATCH_ALL(e)
    {
#ifndef _AFX_PORTABLE
        AfxSetNewHandler(pnhOldHandler);
#endif
        AfxEnableMemoryTracking(bEnable);
        THROW_LAST();
    }
    END_CATCH_ALL

#ifndef _AFX_PORTABLE
    AfxSetNewHandler(pnhOldHandler);
#endif
    AfxEnableMemoryTracking(bEnable);

    // now set the handle in the object
    HANDLE* ph = (HANDLE*)((BYTE*)pTemp + m_nOffset);  // after CObject
    ph[0] = h;
    if (m_nHandles == 2)
        ph[1] = h;

    return pTemp;
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MFC ,可以通过 CButton 类的派生类来实现按钮的自绘。具体步骤如下: 1. 创建一个自定义的按钮类,继承自 CButton。 2. 在该类重载 OnDrawItem 函数,在该函数实现按钮的自绘逻辑。 3. 在对话框或窗口类使用该自定义按钮类。 以下是一个简单的自定义按钮类的示例代码: ```cpp class CMyButton : public CButton { public: CMyButton(); virtual ~CMyButton(); protected: DECLARE_MESSAGE_MAP() afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct); private: COLORREF m_clrBackground; }; CMyButton::CMyButton() { m_clrBackground = RGB(255, 255, 255); } CMyButton::~CMyButton() { } BEGIN_MESSAGE_MAP(CMyButton, CButton) ON_WM_DRAWITEM() END_MESSAGE_MAP() void CMyButton::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rect = lpDrawItemStruct->rcItem; // 绘制背景 pDC->FillSolidRect(rect, m_clrBackground); // 绘制文本 CString strText; GetWindowText(strText); pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT)); pDC->SetBkMode(TRANSPARENT); pDC->DrawText(strText, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); } ``` 在对话框或窗口类,使用该自定义按钮类的示例代码如下: ```cpp CMyButton btn; btn.Create(_T("My Button"), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, CRect(10, 10, 100, 30), this, IDC_MYBUTTON); ``` 其,IDC_MYBUTTON 是自定义按钮的 ID。通过调用 Create 函数创建自定义按钮,并指定按钮的样式为 BS_OWNERDRAW,表示按钮使用自绘。然后在 OnDrawItem 函数,实现自定义按钮的绘制逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值