[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


colored edit controls

Posted on 1997-06-04
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.

Question by:JanetG
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

emmons earned 200 total points
ID: 1397851
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.

Author Comment

ID: 1397852
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.


Expert Comment

ID: 1397853
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.

Author Comment

ID: 1397854
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.


Expert Comment

ID: 1397855
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?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question