Click to See Complete Forum and Search --> : How to change color of brush?


Yoh-hei
April 24th, 1999, 08:46 PM
hello:
I use VC++6.0 MFC.I want change my brush but
it's no work.My code:
int ControlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
CClientDC dc(this);
CBitmap bit;
CDC memdc;
m_nMaxX=GetSystemMetrics(SM_CXSCREEN);
m_nMaxY=GetSystemMetrics(SM_CYSCREEN);
memdc.CreateCompatibleDC(&dc);
bit.CreateCompatibleBitmap(&dc,m_nMaxX,m_nMaxY);
CBitmap* pOldBitmap=memdc.SelectObject(&bit);
m_hBrush=(HBRUSH)memdc.SelectStockObject(BLACK_BRUSH);
memdc.SelectObject(m_hBrush);
memdc.PatBlt(0,0,m_nMaxX,m_nMaxY,PATCOPY);

}
when i change the brush color in other function
But the brush keep up BLACK_BRUSH.(MY CDC memdc)
Who can tell me?
Thank you !

Bob Clarke
April 25th, 1999, 10:00 AM
By using these two lines of code:

m_hBrush=(HBRUSH)memdc.SelectStockObject(BLACK_BRUSH);
memdc.SelectObject(m_hBrush);

you are selecting a black brush, then immediately replacing it with waht the black brush replaced. The SelectStockObject() call returns what was replaced by the BLACK_BRUSH.

Start by getting rid of the extra SelectObject call.

Yoh-hei
April 25th, 1999, 08:22 PM
Hello:
I test the code:
m_hBrush=(HBRUSH)m_memdc.SelectStockObject(BLACK_BRUSH);
m_memdc.SelectObject(m_hBrush);
But run there is error 2228.
use the code:
m_hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(m_memdc,m_hBrush);
No error.But the color no change.If I can use RGB color?
Excuse me I need your help again!
Thank you!

Andy St.Clair
April 26th, 1999, 01:37 AM
Hi,

The previous replier (Bob Clarke) does have a point.

In addition I think the following apply:

1. You are only painting to a memoryDc not to the screen so nothing will appear on-screen anyway.

2. This code is in the OnCreate function. At the time of this message the window has not yet appeared on the screen - i think.

3. Even if the code worked -it will only work once - at the time of window creation.


If you want the client area to be always colour black- This code should be done when during the Onpaint or OnEraseBkgnd functions.