Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need some info and help with ReleaseDC(pDC)

Posted on 2007-11-21
4
Medium Priority
?
465 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 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