• [Programming Visual C++]Chapter Five The Lifetime of a GDI Selection


    pDC->SelectObject返回的指针所指向的GDI对象有可能会被delete掉,所以不能只保存返回的指针。
    正确的做法:convert the return pointer to Windows Handle with the GetSafeHdc member function.

    For other device contexts, such as those for printers and memory buffers, your assignments can last longer. For these long-life device contexts, things get a little more complicated. The complexity results from the temporary nature of GDI C++ object pointers returned by the SelectObject function. (The temporary "object" will be destroyed by the application framework during the idle loop processing of the application, sometime after the handler function returns the call. See MFC Technical Note #3 in the online documentation.) You can't simply store the pointer in a class data member; instead, you must convert it to a Windows handle (the only permanent GDI identifier) with the GetSafeHdc member function. Here's an example:

    // m_pPrintFont points to a CFont object created in CMyView's constructor
    // m_hOldFont is a CMyView data member of type HFONT, initialized to 0

    void CMyView::SwitchToCourier(CDC* pDC)
    {
    m_pPrintFont->CreateFont(30, 10, 0, 0, 400, FALSE, FALSE,
    0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    DEFAULT_PITCH | FF_MODERN,
    "Courier New"); // TrueType
    CFont* pOldFont = pDC->SelectObject(m_pPrintFont);

    // m_hOldFont is the CGdiObject public data member that stores
    // the handle
    m_hOldFont = (HFONT) pOldFont->GetSafeHandle();
    }

    void CMyView:SwitchToOriginalFont(CDC* pDC)
    {
    // FromHandle is a static member function that returns an
    // object pointer
    if (m_hOldFont) {
    pDC->SelectObject(CFont::FromHandle(m_hOldFont));
    }
    }

    // m_pPrintFont is deleted in the CMyView destructor

  • 相关阅读:
    新的for增强循环方法,记录一下,方便以后使用
    Intellij IDEA 自动生成 serialVersionUID
    Java知识点汇总[Review]
    D16-常用十种算法[Java数据结构和算法]
    W9-请求响应[JavaWeb]
    D15-图[Java数据结构和算法]
    D14-多路查找树[Java数据结构和算法]
    D13-平衡二叉树[Java数据结构和算法]
    D12-二叉排序树[Java数据结构和算法]
    D11-堆排序和赫夫曼编码[Java数据结构和算法]
  • 原文地址:https://www.cnblogs.com/huqingyu/p/200415.html
Copyright © 2020-2023  润新知