Menu

Change colour of an edit control???

2005-09-12
2012-09-26
  • Nobody/Anonymous

    I managed to change the colour of the window by adding libgdi32.a and setting hbrBackGround to (HBRUSH) CreateSolidBrush(RGB(157,185,235));

    But now i need to change my edit controls colour to be the same and i can't find out how to do this. Iv'e done some googling and all i find is crap related to MFC and Dialogs which makes no sense to me as i don't use either(Obviously not MFC).

     
    • Nobody/Anonymous

      Well if your'e absolutely sure i cannot change the colour of the edit control itself so it is not BTNFACE how about static control's? i cant get them to do multiline but i could use 3.

      Better then a damn rich edit control with much more then i need.

       
    • Nobody/Anonymous

      you can use the WM_CTLCOLOREDIT message to change the color of the background and the textcolor of your edit control by using SetTextColor() with the given device context handle and by returning a brush at the end of WM_CTLCOLOREDIT (e.g.: return (BOOL) hBrush; ) to color the background of your edit control.

       
    • Nobody/Anonymous

      I don't get it i tried using SetBkColor() after creating the control and also in WM_CTLCOLOREDIT but it seems to have no effect.

      I must be doing something wrong.

       
    • Nobody/Anonymous

      plz post your code for the WM_CTLCOLOREDIT message, so one can see what you are doing wrong. SetBkColor won't work in either case though and also wasn't what I proposed earlier. U might consider to use SetBkMode() to make the edit control transparent.

      did you have a look for the ctlcoloredit-message on MSDN or in a WinAPI-docu?

       
    • Nobody/Anonymous

      Ahh perfect thank you, im going to add a comment in my source about this for future reference.

      I really should have said that ir was set to read only as through my google searches i came across a few articles saying there was a problem with this but they were showing MFC solutions.

      Im using the edit control in place of a label so yeah that's why it's read only and why i wanted the colour to match the window's.

       
    • Nobody/Anonymous

      You have to use a Rich Edit control to change the color. Use EM_SETBKGNDCOLOR for the background or EM_SETCHARFORMAT for the text.

       
    • Nobody/Anonymous

      Are you serious? no other language requires this they must be fairly custom edit controls........

       
    • Nobody/Anonymous

      Iv'e tried your suggestion and the suggestions of other's plus completely blind guesses of my own. I also tryed setting the control to be transparent so im a bit ahead of you :) but that had no effect either.

      Could anyone show me an example? or perhaps an article or some source that show's this working it would make thing's easier but any help is appreciated.

       
      • Br5an

        Br5an - 2005-09-13

        Start a new WindowApplication project.
        Insert this the appropriate parts of this code into the
        LRESULT CALLBACK WindowProcedure ( ... )
        {
        static HBRUSH hBrush;
        switch(message)
        {
        case WM_CREATE:
        CreateWindowEx (
        0, "EDIT", "", WS_CHILD | WS_VISIBLE,
        10, 10, 50, 16,
        hwnd, NULL, NULL, NULL);
        break;

            case WM_CTLCOLOREDIT:
                hBrush = CreateSolidBrush( RGB(255, 255, 0));
                SetBkMode((HDC)wParam, TRANSPARENT );
                SetTextColor((HDC)wParam, RGB(0, 0, 255));
                return(LRESULT)(hBrush);
        

        I took a moment to test it so it should be fine and get you started. Post back (with code) if you have a problem or question.

         
        • Br5an

          Br5an - 2005-09-13

          Leaky code. Better amend that to:

          LRESULT CALLBACK WindowProcedure ( ... )
          {
          static HBRUSH hBrush = NULL;
          switch(message)
          {
          case WM_CREATE:
          CreateWindowEx (
          0, "EDIT", "", WS_CHILD | WS_VISIBLE,
          10, 10, 50, 16,
          hwnd, NULL, NULL, NULL);
          break;

          case WM_CTLCOLOREDIT:
          if(!hBrush)
          hBrush = CreateSolidBrush( RGB(255, 255, 0));
          SetBkMode((HDC)wParam, TRANSPARENT );
          SetTextColor((HDC)wParam, RGB(0, 0, 255));
          return(LRESULT)(hBrush);

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

           
    • Nobody/Anonymous

      Umm well i tried this in my dll and it doesn't seem to have any effect. Perhaps i missed something completely.

      Here is the source to my .cpp file(exported functions are defined in the .h file):

      include "FDLL.h"

      include <windows.h>

      //Declare windows proc.
      LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

      //Make the class name into a global var.
      char szClassName[ ] = "About";
      HINSTANCE hThisInstance,hWinMainInstance = NULL;
      HWND BtnClose,EdtTxt; //Handles for controls.

      void Msg() { MessageBox(NULL,"Welcome To My First DLL :)","Caption:",MB_OK); }
      void AboutWin() { WinMain(hWinMainInstance,NULL,GetCommandLine(),SW_SHOWNORMAL); }

      //For creating buttons.
      int CreateBtn(char BtnText[ ],int X,int Y,int Width,int Height,HINSTANCE Instance,HWND& Ctrlwnd,HWND hwnd) {
      Ctrlwnd = CreateWindowEx(0,TEXT("BUTTON"),TEXT(BtnText),WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,X,Y,Width,Height,hwnd,NULL,Instance,NULL);
      }
      //For creatign single lin edit's.
      int CreateMLEdt(char DefText[ ],int X,int Y,int Width,int Height,HINSTANCE Instance,HWND& Ctrlwnd,HWND hwnd) {
      Ctrlwnd = CreateWindowEx(0,TEXT("EDIT"),TEXT(DefText),WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_READONLY,X,Y,Width,Height,hwnd,NULL,Instance,NULL);
      }

      int WINAPI WinMain (HINSTANCE hThisInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpszArgument,
      int nFunsterStil)

      {
      HWND hwnd; / This is the handle for our window /
      MSG messages; / Here messages to the application are saved /
      WNDCLASSEX wincl; / Data structure for the windowclass /

      /* The Window structure */
      wincl.hInstance = hThisInstance;
      wincl.lpszClassName = szClassName;
      wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
      wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
      wincl.cbSize = sizeof (WNDCLASSEX);
      
      /* Use default icon and mouse-pointer */
      wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
      wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
      wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
      wincl.lpszMenuName = NULL;                 /* No menu */
      wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
      wincl.cbWndExtra = 0;                      /* structure or the window instance */
      /* Use Windows's default color as the background of the window */
      wincl.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(157,185,235));//(COLOR_BTNFACE+1);
      
      /* Register the window class, and if it fails quit the program */
      if (!RegisterClassEx (&amp;wincl))
          return 0;
      
      /* The class is registered, let's create the program*/
      hwnd = CreateWindowEx (
             WS_EX_TOPMOST,                   /* Extended possibilites for variation */
             szClassName,         /* Classname */
             &quot;About&quot;,             /* Title Text */
             WS_OVERLAPPEDWINDOW, /* default window */
             CW_USEDEFAULT,       /* Windows decides the position */
             CW_USEDEFAULT,       /* where the window ends up on the screen */
             270,                 /* The programs width */
             138,                 /* and height in pixels */
             HWND_DESKTOP,        /* The window is a child-window to desktop */
             NULL,                /* No menu */
             hThisInstance,       /* Program Instance handler */
             NULL                 /* No Window Creation data */
             );
      
      /* Make the window visible on the screen */
      ShowWindow (hwnd, SW_SHOWNORMAL);
      
      /* Run the message loop. It will run until GetMessage() returns 0 */
      while (GetMessage (&amp;messages, NULL, 0, 0))
      {
          /* Translate virtual-key messages into character messages */
          TranslateMessage(&amp;messages);
          /* Send message to WindowProcedure */
          DispatchMessage(&amp;messages);
      }
      
      /* The program return-value is 0 - The value that PostQuitMessage() gave */
      return messages.wParam;
      

      }

      / This function is called by the Windows function DispatchMessage() /

      LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
      {
      static HBRUSH hBrush = NULL; // For edit colouring edit controls.
      switch (message) / handle the messages /
      {
      case WM_CREATE:
      CreateBtn("Close",182,74,75,25,hThisInstance,BtnClose,hwnd);
      CreateMLEdt("Version: 1.2.\r\nAuthor: Chesso.\r\nEmail: chesso@gmail.com",8,8,249,65,hThisInstance,EdtTxt,hwnd);
      break;
      case WM_COMMAND:
      switch (HIWORD(wParam))
      {
      case BN_CLICKED:
      if (BtnClose = reinterpret_cast<HWND>(lParam))
      {
      SendMessage(hwnd,WM_DESTROY,0,0);
      }
      }
      return 0;
      case WM_CTLCOLOREDIT:
      if (!hBrush)
      hBrush = CreateSolidBrush(RGB(157,185,235));
      SetBkMode((HDC)wParam,TRANSPARENT);
      SetTextColor((HDC)wParam, RGB(255,255,200));
      return (LRESULT)(hBrush);
      case WM_DESTROY:
      PostQuitMessage (0); / send a WM_QUIT to the message queue /
      DeleteObject(hBrush);
      DestroyWindow(BtnClose);
      DestroyWindow(EdtTxt);
      DestroyWindow(hwnd);
      UnregisterClass(szClassName,hThisInstance);
      break;
      case WM_SIZING:
      //activated when window is resized.
      break;
      default: / for messages that we don't deal with /
      return DefWindowProc (hwnd, message, wParam, lParam);
      }

      return 0;
      

      }

       
      • Br5an

        Br5an - 2005-09-13

        <Umm well i tried this in my dll and it doesn't seem to have any effect. Perhaps i missed something completely.>

        Yes, actually you did. Since your editbox is ES_READONLY you need to respond to the WM_CTLCOLORSTATIC message not WM_CTLCOLOREDIT. The same coloring code should work. Took me a minute to catch on to your little stunt ;)

        Or you could use the code for both if the colors are agreeable:
        case WM_CTLCOLOREDIT:
        case WM_CTLCOLORSTATIC:
        ...

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.