CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2003
    Posts
    3

    Question Change Edit Control Color Directly?

    Howdy Y'all,

    Instead of changing an Edit Control's background and text color by responding to the WM_CTLCOLOR message, I tried to do the following:

    Code:
    m_hEditBox = CreateWindow("EDIT",NULL,WS_CHILD|ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_MULTILINE|ES_WANTRETURN,
    100,100,100,100, (HMENU) 1, NULL, NULL);
    		
    HDC editBoxDC = GetDC(m_hEditBox);
    		
    SetBkMode(editBoxDC, TRANSPARENT);
    SetBkColor(editBoxDC, RGB(255,255,0));
    SetTextColor(editBoxDC, RGB(0,0,255));
    ShowWindow(m_hEditBox,SW_SHOW);		
    ReleaseDC(m_hEditBox, editBoxDC);
    ...While the edit control appears in the appropriate location, the background and text colors are unchanged. Any idea what I'm doing wrong?

    -TM
    Last edited by Tzalmaves; December 2nd, 2003 at 11:49 AM.

  2. #2
    Join Date
    Nov 2003
    Posts
    101
    Why not just use EM_SETBKGNDCOLOR?

  3. #3
    Join Date
    Dec 2003
    Posts
    3
    TSmooth,

    This is an Edit control, not a Rich Edit control - to the best of my knowledge, EM_SETBKGNDCOLOR only works for Rich Edit controls.

    -TM

  4. #4
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    That does not work.
    Such "explanation" is not good enough ?
    How I think about it, your code does not work because the Edit box "knows" what to do with WM_PAINT and other messages.
    you can subclass the edit box you created.
    But for your edit box you can use the following:

    MSDN:
    "An edit control that is not read-only or disabled sends the
    WM_CTLCOLOREDIT message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the edit control...
    Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message. "



    To TSmooth:
    EM_SETBKGNDCOLOR for rich edit control.
    Last edited by Caprice; December 2nd, 2003 at 02:52 PM.
    Good luck

  5. #5
    Join Date
    Dec 2003
    Posts
    3
    Caprice,

    I'm trying to avoid putting code in the parent window's class.

    If I subclass the Edit Control, then in OnPaint() would I just do the SetBkMode, SetTextColor, etc. and then call the Base Class's OnPaint?

    That does not work.
    Such "explanation" is not good enough ?
    You must be a Sabra.

    -TM

  6. #6
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    OnPaint() ?
    Maybe you'll find something in MFC about subclassing, I don't know.
    Without MFC it looks so:

    1. WNDPROC wpOrigProc = (WNDPROC) SetWindowLong( hWnd, GWL_WNDPROC, (LONG) NewProc);

    2. INT_PTR CALLBACK NewProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    if (uMsg == WM_LBUTTONDOWN) {
    } else if ( WhatYouWant ) {
    } else {
    return CallWindowProc(wpOrigLinkProc, hWnd, uMsg, wParam, lParam);
    }
    }

    3 . don't forget about:
    ::SetWindowLong(hWnd, GWL_WNDPROC, (LONG)wpOrigProc);

    Why you don't want to disturb the "parent" ?
    That's the shortest way to change the color.
    Good luck

  7. #7
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    A!
    About MFC..
    class CYourEditBox: public CEdit
    {
    ...
    }

    will work.
    Good luck

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