Solved

Is a call to CBrush::DeleteObject a good/bad/neutral idea in this case

Posted on 2006-11-06
5
501 Views
Last Modified: 2013-11-20
I am using a function that draws text on the screen, and, based on mouse input, some portion of the screen needs to be highlighted by the use of a user selected(previously) background color.  This function also decides what color the text will be.

My specific question: is the cb.DeleteObject line necessary, prohibited, or otherwise?

Secondary Questions: Is there a significantly better way to accomplish what I want to accomplish?
                                Is there a dead solid way to determine when a DeleteObject is required?

      int Index;
      static COLORREF StdBK;
      COLORREF cr,cf;
      CBrush cb;
      Index=scr->Color;
      if(!StdBK)StdBK=pDC->GetBkColor();
      if(Index<0)Index=0;
      else if (Index>(SCH_COLOR_COUNT-1))Index=SCH_COLOR_COUNT-1;
      cr=RGB(m_SchCol.Red[Index],m_SchCol.Green[Index],m_SchCol.Blue[Index]);
      cf=RGB(m_SchCol.Red[SCHFOCUS],m_SchCol.Green[SCHFOCUS],m_SchCol.Blue[SCHFOCUS]);
      if(pDC){
            pDC->SetTextColor(cr);
            pDC->FillRect(scr->ScrnArea,&cb);
            if(Focus){
                  cb.CreateSolidBrush(cf);
                  pDC->SetBkColor(cf);
            }else{
                  cb.CreateSolidBrush(StdBK);
                  pDC->SetBkColor(StdBK);
            }
//            cb.DeleteObject();
      }
      return cr;

I should add that this function is called probably 100 times on a typical OnDraw();


thanks in advance, Rick
0
Comment
Question by:rickatseasoft
[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
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 17881359
CBrush cb;
...
cb.DeleteObject();

In this context this is not necessary, because when function exits, cb object is destroyed, and CBrush destructor calls DeleteObject.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 17881555
pDC->FillRect(scr->ScrnArea,&cb);  
At this point cb (coloured brush) has not been created - !!!

You can use FillSoldiRect which takes a COLORREF.

In fact I see no need for a brush at all in this code snippet.

0
 

Author Comment

by:rickatseasoft
ID: 17881581
Andy:

You are absolutely correct!  I did some cutting and pasting to remove code that was exterraneous to the problem, and screwed that one up.  In the real code, the cb has been created.

Rick
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17881606
Created - OK.
Necessary - ? - you could use FillSolidRect (unless it is a pattern brush) and so no need for the CBrush at all.
0
 

Author Comment

by:rickatseasoft
ID: 17888337
Thanks guys:

AlexFM:  I suspected that was the case, but wanted verification.

Andy:  An excellent idea that I will try today.

Thanks again! Rick
0

Featured Post

Industry Leaders: 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…
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 …
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

695 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