Solved

change brush color

Posted on 2002-06-07
12
402 Views
Last Modified: 2010-05-18
OK, I have CBrush myBrush, then I put:
myBrush.CreateSolidBrush(RGB(100,200,10));
 Now, the problem is, how to change myBrush to another color. It popped up an error when I try to use CreateSolidBrush again on that object.?? Any clue. Thanks.
0
Comment
Question by:quanghoc
  • 6
  • 5
12 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 7062419
You have to select it out of the current device context and call myBrush.DeleteObject before you can call myBrush.CreateSolidBrush(...) again.

A basic note: Posts like 'an error popped up' aren't really helpful if you don't tell us what error it was.
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7062805
The error was Debug assertion failed!
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7062940
That's what I guessed -- but I'm sure it said more than just some debug assertion, right? I probably said that CBitmap::m_hObject != NULL
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7063043
yes, ASSERT(someobject==NULL) return false and that made the error. I found out when I choose "debug". So, what I don't understand is what actually wrong with it? Can someone explain?
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7063247
ASSERT is there to help you. In general it follows this syntax:
ASSERT( some_condition )
if some_condition is false, this dialog comes up. It is there to give you runtime-diagnostics. It is there to check whether an assumption you made is correct. In the case of your CBrush it ensures that the object isn't attached to any GDI-object when you call any of the creation functions. This prohibits any memory leaks within your application's drawing functions.
Btw: You can put as many ASSERT's in your code as you like -- in the Release build they are not compiled into your executable. With this in mind, you should never change any variables or call functions that change them inside an ASSERT. If you need to call such functions use the VERIFY-macro instead.
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7063301
Okey, I kinda got that ASSERT is to check memory leaks, but how come doing myBrush.CreateSolidBrush(RGB(100,200,10)) twice is wrong?? And I already tried myBrush.DeleteObject, not work either.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:fl0yd
ID: 7063413
No, ASSERT isn't to check for memory leaks but to make sure some condition that is crucial for your application is met. In the CBrush class it is used to prevent memory and resource leaks not to see if there are any.
To understand why applying CreateSolidBrush twice is wrong you have to understand what CBrush actually is. An object of class CBrush never contains a GDI brush -- all it does is control the way you interact with it. So if you call a creation function for the second time, the GDI object still exists -- I'm not sure, but a CBrush::Detach() could help after you called CBrush::DeleteObject(); I thought a DeleteObject() was enough though, noone is perfect...
Basically, my opinion is that using MFC without having a strong win32-background is making things more complicated than necessary. This usually becomes apparent when dealing with drawing routines, CDC's, CGdiObject's and the likes.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7063436
I just checked it: you don't need to call CBrush::Detach() -- it is called in CBrush::DeleteObject(). This means that you can call CreateSolidBrush() after calling DeleteObject() and there should be no error. You should check the return value of CBrush::DeleteObject() -- it can return FALSE. Also, make sure that the brush is *NOT* selected into any device context when calling DeleteObject() -- otherwise it will fail and not reset CBrush::m_hObject thus the
ASSERT( m_hObject == NULL )
will be false the second time you try to create a brush and the dialog will pop up.
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7064204
----------
make sure that the brush is *NOT* selected into any device context when calling DeleteObject() --
----------
sorry, what does it mean?
0
 
LVL 8

Accepted Solution

by:
fl0yd earned 150 total points
ID: 7064257
Somewhere in your code you have something like

CBrush* pOldBrush = static_cast<CBrush*>( cdc.SelectObject( &myBrush ) );

or maybe

CBrush* pOldBrush = (CBrush*)cdc.SelectObject( &myBrush );

This line of code selects the brush into the device context to make it available for drawing. As long as the brush is selected into the device context it cannot be deleted. So you have to call

cdc.SelectObject( pOldBrush );

before going on to delete your brush using

BOOL success = myBrush.DeleteObject();

This is a bit of inverse logic here: You cannot select a CGdiObject-derived object (CPen, CBrush, ...) out of a device context. However, at any given time there can only be ONE object of any type selected into the device context, thus if you select your old brush back into the device context, the currently used brush is deselected.

Don't get me wrong here, but I think you should definately do some reading on win32-/gdi-programming before going on using mfc. I'm not trying to discourage you, but from my own experience I know that it was pure luck, that my first mfc experiments did work at all. Once you have a somewhat solid understanding of the windows gdi you will find your way around those mfc-anomalies a lot more easily. Believe me, it is surely not all that complex as it presents itself to you at the moment.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7261565
Dear quanghoc

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "fl0yd"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7263272
Sorry, actually, I got it worked. The reason this question has forgotten because there was a bug in this forum preventing me from submitting any response. Thanks all.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

17 Experts available now in Live!

Get 1:1 Help Now