[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

colored edit controls

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.

0
JanetG
Asked:
JanetG
  • 3
  • 2
1 Solution
 
emmonsCommented:
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.
0
 
JanetGAuthor Commented:
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.

0
 
emmonsCommented:
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 ) {
            UnrealizeObject(hBrush);
         SelectObject((HDC)wParam, hBrush);
         SetBkMode((HDC)wParam, TRANSPARENT);
         SetTextColor((HDC)wParam, RGB(0, 0, 0));
         return(hBrush);
      } 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;


    hgInst=hInstance;

    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;
        }



    hWnd=CreateWindow("EditAlign",
                      "Edit Alignment Test",
                      WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
                      35, 35, 400, 150,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    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.
    hEditLast1=hEditLeft;

    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);
    LocalFree(hMem);
    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);
    LocalFree(hMem);
    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);
    hEditLast2=hEdit;


    while (GetMessage(&msg, NULL, 0,0 ))
        {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }


    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);
         DeleteObject(hTempBrush);
             break;

        case WM_DESTROY:
            PostQuitMessage(0);
            DeleteObject(hBrush);
            break;

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

                }

        default:
            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.
0
 
JanetGAuthor Commented:
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.



0
 
emmonsCommented:
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?
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now