We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


colored edit controls

JanetG asked
Medium Priority
Last Modified: 2013-12-03
I need to color an edit control using a color in a custom 256 color palette.  In the parent window in response to WM_CTLCOLOR, I set the background color and return a solid brush containing this background color.  Then in response to WM_PAINT for the edit control, I select and realize the palette.  Unfortunately, the color gets dithered to a 16 color palette and I am not sure why.

Anyone have any idea why this might happen?  Is it possible to color an edit control with > 16 colors.

Watch Question

Yes it is, no it is not trivial.
All of the edit controls are created using 16 colors. You will have
to create owner draw controls in order to paint them nice colors.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


emmons, can you give me a little more detail on how to do this?  Are you saying I'll need to take ownership over the entire drawing of the edit control (e.g. handle WM_DRAWITEM & paint the entire control myself).  Or is there a hybrid approach that let's me use the standard drawing of the edit control but still use a 256 color palette?

Any further thoughs are appreciated.

First, are you using C or C++? When I first read your question I was thinking of the C++ classes for controls and bitmaps. If I misspoke, I apologize.

In C your code looks something like this, right?

   case WM_CTLCOLOR:
      if ( HIWORD(lParam) == CTLCOLOR_EDIT
         || HIWORD(lParam) == CTLCOLOR_MSGBOX ) {
         SelectObject((HDC)wParam, hBrush);
         SetBkMode((HDC)wParam, TRANSPARENT);
         SetTextColor((HDC)wParam, RGB(0, 0, 0));
      } else {
         return GetStockObject(LTGREY_BRUSH);

WIth appropriately creating and destroying the brushes elsewhere.
I am running this bit of code on my machine and it looks OK. I would guess that the problem is going to be one of the palette.
Is there any way for you to run your code initially on a machine that is truecolor (or at least more than 256 color)? It makes life a lot easier to be able to pick one problem at a time. In this case, let's paint the box first, then we can figure out how to realize the palette.

The code I just ran looks like this
#include <windows.h>
#include <windowsx.h>
#include "aligned.h"

HANDLE      hgInst;

//These are for one method;
HANDLE      hEditLeft;
HANDLE      hEditCenter;
HANDLE      hEditRight;
HANDLE      hEditLast1;

//These are for another method.
HANDLE      hEdit;
HANDLE      hEditLast2;

 * WinMain
 * Purpose:
 *  Main entry point of application.   Should register the app class
 *  if a previous instance has not done so and do any other one-time
 *  initializations.
 * Parameters:
 *  See Windows SDK Guide to Programming, page 2-3
 * Return Value:
 *  Value to return to Windows--termination code.

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
                    LPSTR lpszCmdLine, int nCmdShow)
    WNDCLASS    wndClass;
    HWND        hWnd;
    MSG         msg;
    short       i;
    HANDLE      hEditMem;
    HANDLE      hMem;


    if (!hPrevInstance)
        wndClass.style          = CS_HREDRAW | CS_VREDRAW;
        wndClass.lpfnWndProc    = EditAlignWndProc;
        wndClass.cbClsExtra     = 0;
        wndClass.cbWndExtra     = 0;
        wndClass.hInstance      = hInstance;
        wndClass.hIcon          = LoadIcon(hInstance, "EditAlignIcon");
        wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wndClass.hbrBackground  = GetStockObject(WHITE_BRUSH);
        wndClass.lpszMenuName   = "EditAlignMenu";
        wndClass.lpszClassName  = "EditAlign";

        if (!RegisterClass(&wndClass))
            return FALSE;

                      "Edit Alignment Test",
                      35, 35, 400, 150,

    ShowWindow(hWnd, nCmdShow);

    hEditLeft=CreateWindowEx(WS_EX_NOPARENTNOTIFY, "edit", "Control #1",
                       WS_BORDER | WS_VISIBLE | WS_CHILD | ES_LEFT | ES_MULTILINE,
                       10, 10, 380, 26, hWnd, 900, hInstance, NULL);

    //Get the edit control handle to it's memory.
    hEditMem=SendMessage(hEditLeft, EM_GETHANDLE, 0, 0L);

    //Remember which one is visible.

    hEditCenter=CreateWindowEx(WS_EX_NOPARENTNOTIFY, "edit", "",
                       WS_BORDER | WS_CHILD | ES_CENTER | ES_MULTILINE,
                       10, 10, 380, 26, hWnd, 901, hInstance, NULL);

    //Remove the memory that
    hMem=SendMessage(hEditCenter, EM_GETHANDLE, 0, 0L);
    SendMessage(hEditCenter, EM_SETHANDLE, hEditMem, 0L);

    hEditRight=CreateWindowEx(WS_EX_NOPARENTNOTIFY, "edit", "",
                       WS_BORDER | WS_CHILD | ES_RIGHT | ES_MULTILINE,
                       10, 10, 380, 26, hWnd, 902, hInstance, NULL);

    //Get the edit control handle to it's memory.
    hMem=SendMessage(hEditRight, EM_GETHANDLE, 0, 0L);
    SendMessage(hEditRight, EM_SETHANDLE, hEditMem, 0L);

    hEdit=CreateWindowEx(WS_EX_NOPARENTNOTIFY, "edit", "Control #2",
                         WS_VISIBLE | WS_BORDER | WS_CHILD | ES_LEFT | ES_MULTILINE,
                         10, 50, 380, 26, hWnd, 903, hInstance, NULL);

    while (GetMessage(&msg, NULL, 0,0 ))

    return msg.wParam;

 * EditAlignWndProc
 * Purpose:
 *  Window class procedure.  Standard callback.
 * Parameters:
 *  The standard.  See Section 2.4 Windows SDK Guide to Programming,
 *  page 2-4.
 * Return Value:
 *  See Parameters, above.

HANDLE       hBrush;
long FAR PASCAL EditAlignWndProc(HWND hWnd, UINT iMessage,
                         WPARAM wParam, LPARAM lParam)
    HANDLE      hEditTmp;
    HANDLE      hMem;
    HANDLE      hMemTmp;
    RECT        rect;
    DWORD       dwStyle;
    POINT       pt;
      HANDLE      hTempBrush;

    switch (iMessage)

      case WM_CREATE:
         hTempBrush = LoadBitmap(hgInst, "ROSE");
         hBrush = CreatePatternBrush(hTempBrush);

        case WM_DESTROY:

            SelectObject((HDC)wParam, hBrush);
            SetBkMode((HDC)wParam, TRANSPARENT);
            SetTextColor((HDC)wParam, RGB(0, 255, 0));
        case WM_COMMAND:
            switch (wParam)
                case IDM_EXIT:
                    PostMessage(hWnd, WM_CLOSE, 0, 0L);


            return (DefWindowProc(hWnd, iMessage, wParam, lParam));

    return 0L;

The resources are a menu bar that has EXIT on it and a 256 color bitmap called ROSE.


Actually I am using MFC & C++.  I'm confused though--the above example is not showing an ownerdrawn approach.  It only handles WM_CTLCOLOREDIT (which I am already doing appropriately) with setting a custom palette.

Unfortunately, I don't think this will work with anything other than a standard 16 color palette.  The Windows common controls only support 16 colors, so independent of the palette, any non standard color will be dithered to one of 16 colors.

Any further thoughts on how to do this?  I suspect you are right the only possible approach is owner drawn.

Do you have any resources for running your program on a 16 bit color machine? It would at least tell you if you are handing all of the color messages.
I have some subclasses around here somewhere from when I did 256 color buttons in MFC a while ago. Shall I look them up, or do you want to go with the owner draw  solution?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.