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

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;
      else if (Index>(SCH_COLOR_COUNT-1))Index=SCH_COLOR_COUNT-1;
//            cb.DeleteObject();
      return cr;

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

thanks in advance, Rick
Who is Participating?
AlexFMConnect With a Mentor Commented:
CBrush cb;

In this context this is not necessary, because when function exits, cb object is destroyed, and CBrush destructor calls DeleteObject.
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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.

rickatseasoftAuthor Commented:

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.

AndyAinscowFreelance programmer / ConsultantCommented:
Created - OK.
Necessary - ? - you could use FillSolidRect (unless it is a pattern brush) and so no need for the CBrush at all.
rickatseasoftAuthor Commented:
Thanks guys:

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

Andy:  An excellent idea that I will try today.

Thanks again! Rick
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.