Solved

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

Posted on 2006-11-06
5
498 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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: 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. …
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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