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

    Setting Background color of a view

    I want to change the background color of my CEditView from white to gray before it is shown. In PreCreateWindow(CREATESTRUCT& cs) I have a call to SetClassLong. Well, here's the code:


    MyCEditViewClass::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs

    BOOL bPreCreated = CEditView::PreCreateWindow(cs);
    cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
    SetClassLong(AfxGetMainWnd()->GetSafeHwnd(), GCL_HBRBACKGROUND, COLOR_BTNFACE);

    return bPreCreated;
    }




    It compiles and runs, but the color of the CEditView remains white. Perhaps I'm getting the HWND the wrong way? If so, how do I do it? Perhaps I need to move the code? OnDraw? Please help

    Thanks,

    Jeff


  2. #2
    Join Date
    Oct 1999
    Location
    New York, US
    Posts
    9

    Re: Setting Background color of a view

    Likewise, changing structure cs to change the size of the window too does not work.


  3. #3
    Join Date
    Aug 1999
    Location
    Florida
    Posts
    21

    Re: Setting Background color of a view

    After looking a bit further, it winds up that AfxGetMainWnd() is returning null in the following code:


    PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs

    cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping

    SetClassLong(AfxGetMainWnd()->GetSafeHwnd(), GCL_HBRBACKGROUND, COLOR_BTNFACE);

    return CEditView::PreCreateWindow(cs);
    }




    Since AfxGetMainWnd() returns NULL, GetSafeHwnd doesn't work, and I Cant use that HWND value in SetClassLong(HWND, int, long). Any ideas as to why I get NULL, or how to get the HWND a different way? Thanks,

    Jeff


  4. #4
    Guest

    Re: Setting Background color of a view


    Well, I don't know if this is what you guys are looking
    for (I'm not real knowledgeable about the distinction
    between forground and background), but this does set
    the color of the CView.

    void CChangeColorView::OnDraw(CDC* pDC)
    {
    CChangeColorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    CRect lRect;
    GetClientRect(lRect);
    lRect.NormalizeRect();

    // The next three lines would work also, but
    // according to the documentation, FillSolidRect is faster.

    // CBrush lSolidBrush;
    // lSolidBrush.CreateSolidBrush(RGB(0,50,0));
    // dc.FillRect(lRect, &lSolidBrush);

    pDC->FillSolidRect( lRect, RGB(0,75,0) );
    }



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