NO

Author Topic: Trap EN_SETFOCUS message and change edit box  (Read 7738 times)

David L Morris

  • Guest
Trap EN_SETFOCUS message and change edit box
« on: May 16, 2011, 01:30:18 PM »
I thought this would change the edit box display but I must have to do something else to achieve it.  Could someone please guide me?

Under the dlgproc case WM_COMMAND message processing:-

   case WM_CTLCOLOREDIT:
      if ((HIWORD(wParam)) == EN_SETFOCUS)
                {   hdc = (HDC)wParam;
                   SetTextColor(hdc, RGB(255,255,255));   
                   SetBkColor(hdc, RGB(0,0,0));         
                   return (LONG) GetStockObject (WHITE_BRUSH);
            }
      else
                {   hdc = (HDC)wParam;
                   SetTextColor(hdc, RGB(255,0,0));            // red
                   SetBkColor(hdc, RGB(255,255,0));            // yellow
                   return (LONG) GetStockObject (WHITE_BRUSH);
            }
      break;

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Trap EN_SETFOCUS message and change edit box
« Reply #1 on: May 16, 2011, 06:30:19 PM »
You have to trap focus and store the handle of the edit box with focus.
You can then switch off of this handle when handling WM_CTLCOLOREDIT.

attached is a demo of this technique.
No one cares how much you know,
until they know how much you care.

David L Morris

  • Guest
Re: Trap EN_SETFOCUS message and change edit box
« Reply #2 on: May 17, 2011, 03:23:10 AM »
Thanks very much DMac.  I will use your sample to study and include the example in my code.

David L Morris

  • Guest
Re: Trap EN_SETFOCUS message and change edit box
« Reply #3 on: May 18, 2011, 08:14:21 AM »
Hello again.  Could you advise how you generated the code in your example please?  I am converting from Basic and got my original code working by using the GetFocus function as follows:-

   case WM_CTLCOLOREDIT:
      if (lParam == (long)GetFocus())
                {   hdc = (HDC)wParam;
                   SetTextColor(hdc, RGB(255,255,255));   
                   SetBkColor(hdc, RGB(0,0,0));         
                   return (LONG) GetStockObject (WHITE_BRUSH);
            }
      else
                {   hdc = (HDC)wParam;
                   SetTextColor(hdc, RGB(255,0,0));            // red
                   SetBkColor(hdc, RGB(255,255,0));            // yellow
                   return (LONG) GetStockObject (WHITE_BRUSH);
            }
      break;

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Trap EN_SETFOCUS message and change edit box
« Reply #4 on: May 19, 2011, 05:23:43 PM »
Hello again.  Could you advise how you generated the code in your example please

I assume that you are refering the the message crackers (The lines begining with HANDLE_MSG or HANDLE_DLGMSG) and the associated methods.

I use the message cracker wizard see here: http://forum.pellesc.de/index.php?topic=2352.0

If I am writing a dialog based application I replace the generated HANDLE_MSG macro with the HANDLE_DLGMSG macro.

Our own timovjl has also written a tool: http://forum.pellesc.de/index.php?topic=2065.0

Timo's tool doesn't make use of the message cracker macroes but instead breaks the messages out to the methods in code.

Both of these tools yield clean and managable code.

One more tool that I use quite a bit of is the Cwizard.  The stand alone version is here: http://www.smorgasbordet.com/pellesc/contrib_cwiz.htm and the add-in can be found here: http://forum.pellesc.de/index.php?topic=104.0

I use the stand alone version and have not used the add-in yet.
No one cares how much you know,
until they know how much you care.

David L Morris

  • Guest
Re: Trap EN_SETFOCUS message and change edit box
« Reply #5 on: May 20, 2011, 03:48:15 AM »
Thanks again and I have downloaded your references to study.  I am going on a few weeks break and will report back on my return.