binarydream
asked on
Brush
Experts:
I have a problem:
I create a brush in OnCtlColor() and return it later.This will make my control turn different color.
For example:
HBRUSH CMydialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush mybrush;
mybrush.CreateSolidBrush (RGB( 255, 0, 0 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
pDC->SetBkColor(RGB(255,0, 0));
pDC->SetTextColor(RGB(0,0, 0));
return mybrush;
}
My confusion is since I create mybrush in the function above and mybrush should get destructed after return.I think the mybrush should die.How the system can use it to draw control color?
But in face it works!
And how about declare the brush in the if statement? Does it make difference?
Many thx.
I have a problem:
I create a brush in OnCtlColor() and return it later.This will make my control turn different color.
For example:
HBRUSH CMydialog::OnCtlColor(CDC*
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush mybrush;
mybrush.CreateSolidBrush (RGB( 255, 0, 0 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
pDC->SetBkColor(RGB(255,0,
pDC->SetTextColor(RGB(0,0,
return mybrush;
}
My confusion is since I create mybrush in the function above and mybrush should get destructed after return.I think the mybrush should die.How the system can use it to draw control color?
But in face it works!
And how about declare the brush in the if statement? Does it make difference?
Many thx.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Seems I'm an idiot on MFC.
nietod,looks like you misunderstand the question.
Regards
nietod,looks like you misunderstand the question.
Regards
I think binarydream means the code works but he/she doesnt believe why it works.
I doubt the code works safely. The brush will be deleted before the procedure returns and the procedure will return the handle to the deleted brush. That is undeniably a mistake. If the program manages to use that deleted brush successfully--and I don't know for sure that that is the case--then it is probably because the delete brush's data still remains in the GDI heap because it has not yet been used to store data for a new object.
Yes,doubt alot.
If it works,at least,binarydream,you creat brush very time the get called and gradually the system-resource get starved,then die.Hur?
If it works,at least,binarydream,you creat brush very time the get called and gradually the system-resource get starved,then die.Hur?
ASKER
Adjusted points from 50 to 125
ASKER
Sounds a hard question.
However it works!!!!
Maybe wyn reminds me.
Thanks.
However it works!!!!
Maybe wyn reminds me.
Thanks.
God!
I'm definitly wrong here,binarydream.
The brush object will be automatically destructed when function end.
You'd declare it as a class data member and initialize it in dialog initial...
I dont know why your code works but ,at least,I'm wrong here.
:(
I'm definitly wrong here,binarydream.
The brush object will be automatically destructed when function end.
You'd declare it as a class data member and initialize it in dialog initial...
I dont know why your code works but ,at least,I'm wrong here.
:(
What is going on? Why did you reject my answer? Wyn's answer is definitely not right.
The solutions is to store the handle to the GDI brush in a global variable--or to use a global/static CBrush object. This global handle/object will hold the handle between "changes" to the brush. each time the brush needs to change (color etc), you will delete the old handle and create a new one. At the end of the program you must delete the last brush. (If you use a global/static brush object, instead of a global brush handle, some of thise details are taken care of automatically.)