Changing the Color of an Edit Control

Last reviewed: June 23, 1995
Article ID: Q74043
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0

SUMMARY

The source code fragment below demonstrates how to paint the foreground and background of a single-line edit control in an application developed for the Microsoft Windows graphical environment. It creates a window that contains one single-line edit control and paints the edit control cyan on blue.

Windows sends the WM_CTLCOLOR message to the parent of an edit control before the control is painted. If desired, the parent window, usually a dialog box procedure, can process this message and change the text and background colors of the control.

NOTE: WM_CTRLCOLOR is obsolete on Windows NT and Windows 95. Instead, you would use WM_CTLCOLORBTN, WM_CTLCOLORDLG, WM_CTLCOLOREDIT, WM_CTLCOLORLISTBOX, WM_CTLCOLORMSGBOX, WM_CTLCOLORSCROLLBAR, or WM_CTLCOLORSTATIC.

MORE INFORMATION

The following MainWndProc demonstrates WM_CTLCOLOR processing:

long FAR PASCAL MainWndProc(HWND hwnd, unsigned msg,

                            WORD wParam, LONG lParam)
{
static   HWND     hwndEdit;
static   HBRUSH   hBrush;

   switch (msg)
   {
      case WM_CREATE:
         {
            HDC hdc;
            TEXTMETRIC tm;

            hdc = GetDC(hwnd);
            GetTextMetrics(hdc, &tm);
            ReleaseDC(hwnd, hdc);

            hwndEdit = CreateWindow("edit", NULL,
                            WS_CHILD | WS_VISIBLE
                            | ES_AUTOHSCROLL | ES_MULTILINE,
                            10, 20, 100, tm.tmHeight,
                            hwnd, 1, hInst, NULL);

            // Create a blue brush to be used for the edit control's
            // background color.
            hBrush = CreateSolidBrush(RGB(0, 0, 255));
         }
         break;

      case WM_CTLCOLOR:
      {
         // Set foreground and background colors only if this
         // is an edit control.
         if (HIWORD(lParam) == CTLCOLOR_EDIT)
         {
            // Set the edit control's foreground text color to
            // cyan and the text's background color to blue.
            SetTextColor(wParam, RGB(0, 255, 255));
            SetBkColor(wParam, RGB(0, 0, 255));

            // Properly originate the background brush. This is
            // of use if the brush is a pattern instead of a
            // solid color.
            UnrealizeObject(hBrush);
            SetBrushOrg(wParam, 0, 0);

            // Return a handle to the background brush for the edit
            // control.
            return (DWORD)hBrush;
         }
      }
      break;

      case WM_SETFOCUS:
         SetFocus(hwndEdit);
         return 0;

      case WM_DESTROY:
         PostQuitMessage(0);
         DeleteObject(hBrush);
         break;
   }
   return DefWindowProc(hwnd, msg, wParam, lParam);
}


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 23, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.