CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Posts
    79

    How to change color of brush?

    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 !


  2. #2
    Join Date
    May 1999
    Posts
    42

    Re: How to change color of brush?

    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.


  3. #3
    Join Date
    Apr 1999
    Posts
    79

    Re: How to change color of brush?

    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!


  4. #4
    Join Date
    May 1999
    Posts
    25

    Re: How to change color of brush?

    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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured