Solved

Need some info and help with ReleaseDC(pDC)

Posted on 2007-11-21
4
417 Views
Last Modified: 2013-11-20
Please take a look at my code below.
It works just fine to change the font of a column in a ListView.
I allways learned that you have to release the DC when your are done.

             HDC hdc = pLVCD->nmcd.hdc;
             CDC* pDC=NULL;

When I add the ReleaseDC(pDC) in OnCustomdrawList, the programm will not show the new font.
Should I be concerned about the Release??? Or where can I add the Release???
Please let me know.

Thank You.

Best regards,
Thomas... a beginner with MFC

             

//----------------------------------------------------------------------------
 void CThyssenSchulteDialog::OnCustomdrawList(NMHDR* pNMHDR, LRESULT* pResult)
 {
       NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

       // Take the default processing unless we set this to something else below.
    *pResult = 0;
      
    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.

    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
        {
          *pResult = CDRF_NOTIFYITEMDRAW;
            }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        // This is the notification message for an item.  We'll request
        // notifications before each subitem's prepaint stage.

        *pResult = CDRF_NOTIFYSUBITEMDRAW;
        }
    else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
        {
        // This is the prepaint stage for a subitem. Here's where we set the
        // item's text and background colors. Our return value will tell
        // Windows to draw the subitem itself, but it will use the new colors
        // we set here.
           
            COLORREF crText, crBkgnd;
            int iCol = pLVCD->iSubItem;
            int iRow = pLVCD->nmcd.dwItemSpec;

            CString str=m_List.GetItemText(iRow,4);
                  
            
            //------------ Sichere vorherige Schrift -----------
              CFont *oldfont = GetFont();
            //--------------- Erstelle neue Schrift ------------
             CString my_new_font="Bradley Hand ITC";

              CFont *fontPtr = new CFont();
              fontPtr->CreateFont(
              12,
               0,
               0,
               0,
               FW_SEMIBOLD,
               0,
               0,
               0,
               DEFAULT_CHARSET,
               OUT_CHARACTER_PRECIS,
               CLIP_CHARACTER_PRECIS,
               DEFAULT_QUALITY,
               DEFAULT_PITCH|FF_DONTCARE,my_new_font);
             //------------------------------------------------
      
                 //========================
                   HDC hdc = pLVCD->nmcd.hdc;
                   CDC* pDC=NULL;
                  //CDC* pDC =CDC::FromHandle(pLVCD->nmcd.hdc);
                 //========================

               if (3== pLVCD->iSubItem )
           {
                   crText = RGB(255,165,0);   //orange
                   crBkgnd = RGB(255,255,224); //beige
            //------------ Neue Schrift -----------
                   if(!pDC) pDC = CDC::FromHandle(hdc);
                   pDC->SelectObject(fontPtr);
            //-------------------------------------

               }
           else
               {
                    
                      crText = RGB(0,0,255);      //blau
                      crBkgnd = RGB(255,255,224); //beige
                     //---------- Alte Schrift -------------
                      if(!pDC) pDC = CDC::FromHandle(hdc);
                      pDC->SelectObject(oldfont);
                     //-------------------------------------
                         
                  }
               
             
               
            
       

        // Store the colors back in the NMLVCUSTOMDRAW struct.
        pLVCD->clrText = crText;
        pLVCD->clrTextBk = crBkgnd;

        // Tell Windows to paint the control itself.
           *pResult = CDRF_DODEFAULT;
               
             
        }
}
0
Comment
Question by:tsp2002
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 20333970
In the code you posted there is no call to ReleaseDC(); where do you call it?
0
 

Author Comment

by:tsp2002
ID: 20334029
I tried it here....

 // Store the colors back in the NMLVCUSTOMDRAW struct.
        pLVCD->clrText = crText;
        pLVCD->clrTextBk = crBkgnd;

        // Tell Windows to paint the control itself.
           *pResult = CDRF_DODEFAULT;
            ReleaseDC();
        }
}
0
 
LVL 19

Accepted Solution

by:
alb66 earned 500 total points
ID: 20334223
An application must call ReleaseDC() only if it has called GetWindowDC() or GetDC() in order to retrieve the DC.
See http://msdn2.microsoft.com/en-us/library/ms533251.aspx
0
 

Author Comment

by:tsp2002
ID: 20334349
....An application must call ReleaseDC() only if it has called GetWindowDC() or GetDC() in order to retrieve the DC.

Thanks...that was the information I need. I thought I have to use ReleaseDC for any DC
Thanks for your help.
Best regards,
Thomas

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now