[Programming Visual C++]Chapter Five - The Lifetime of a GDI Selection
2005-07-26 15:24
369 查看
pDC->SelectObject返回的指针所指向的GDI对象有可能会被delete掉,所以不能只保存返回的指针。 正确的做法:convert the return pointer to Windows Handle with the GetSafeHdc[/i] 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
相关文章推荐
- Stability of the C++ ABI: Evolution of a Programming Language
- [Programming Visual C++]Chapter Five - Fonts
- Exception Handling(Chapter 14 of The C++ Programming Language)
- Early Stages of the (Visual) C++ .Net 17
- How to detect the presence of the Visual C++ 2010 redistributable package
- C&C++学习笔记1--The seven steps of programming
- [Programming Visual C++]Chapter Six-Setting the Color for the Dialog Background and for Controls
- Try out the latest C++ compiler toolset without waiting for the next update of Visual Studio
- Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
- A Tour of the Standard Library(Chapter 3 of The C++ Programming Language)
- Derived Classes(Chapter 12 of The C++ Programming Language)
- Templates(Chapter 13 of The C++ Programming Language)
- Library Organization and Containers(Chapter 16 of The C++ Programming Language)
- Expressions and Statements(Chapter 6 of The C++ Programming Language)
- The mixed programming in terms of matlab and C++
- Elements of Programming Style -- The C++ Style Guide
- How to change the text color of the selected property sheet tab in Visual C++
- A Tour of C++(Chapter 2 of The C++ Programming Language)
- Types and Declarations(Chapter 4 of The C++ Programming Language)
- Source Files and Programs(Chapter 9 of The C++ Programming Language)