Solved

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

Posted on 2006-11-06
5
486 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
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now