Solved

Need some info and help with ReleaseDC(pDC)

Posted on 2007-11-21
4
440 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

751 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