Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need some info and help with ReleaseDC(pDC)

Posted on 2007-11-21
4
Medium Priority
?
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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