Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-06
5
Medium Priority
?
508 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 1000 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 1000 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 45

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

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 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