Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 116
sum67 challenge 35 93
java ^ examples 8 62
How to convert MFC::CString to UTF8 wchar_t* 10 142
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

23 Experts available now in Live!

Get 1:1 Help Now