Solved

Need some info and help with ReleaseDC(pDC)

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

628 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