Solved

colored edit controls

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

0
Comment
Question by:JanetG
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
emmons earned 100 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.
0
 

Author Comment

by:JanetG
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.

0
 
LVL 4

Expert Comment

by:emmons
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 ) {
            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
 

Author Comment

by:JanetG
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.



0
 
LVL 4

Expert Comment

by:emmons
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?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now