setting the color of a child window?

This is a discussion on setting the color of a child window? within the Windows Programming forums, part of the Platform Specific Boards category; Well how to do it? I have tryed to search for something like WM_SETCOLOR, but there is no such thing... ...

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    setting the color of a child window?

    Well how to do it?
    I have tryed to search for something like WM_SETCOLOR, but there is no such thing...
    Help!
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Have you tried processing the WM_CTLCOLORBTN message? This is the message that the child window sends to the parent window procedure when the child window is about to paint its client area. The parent window can use this to alter the colors that the child window procedure will use for painting.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Quote Originally Posted by ElastoManiac
    Well how to do it?
    I have tryed to search for something like WM_SETCOLOR, but there is no such thing...
    Help!
    Good question, that I am also curious about.

    I can change the color of an edit box, but I don't know how to change the color of a regular child window. (A few weeks ago I posted code to change the color of an edit box to red/blue by left/right mouse clicks)

    A simple code example (non MFC) would be nice.

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by BobS0327
    Have you tried processing the WM_CTLCOLORBTN message?
    I have, and it works!
    Thanks!

    Heres How:
    Code:
    case WM_CTLCOLORLISTBOX:
    				SetBkColor((HDC)wParam, RGB(10,10,10));
    				SetTextColor((HDC)wParam, RGB(100,200,10));
    				return (LRESULT)CreateSolidBrush(RGB(10,50,10));
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    Yes, but that would only work for list boxes. It's terrifyingly difficult to work out what you mean by a "child window". There are two types of child window: ones that use predefined classes supplied by the system and ones that use your own classes.

    I take it that you meant ones that use predefined classes?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Quote Originally Posted by SMurf
    Yes, but that would only work for list boxes. It's terrifyingly difficult to work out what you mean by a "child window". There are two types of child window: ones that use predefined classes supplied by the system and ones that use your own classes.

    I take it that you meant ones that use predefined classes?
    For a child window, I answered my own question:
    (left/right mouse clicks change the color of main or child windows)
    Code:
    // basic window red, child window white; change background color on left/right mouse clicks
    //gcc prog.c -mwindows -o prog.exe
    
    #include <windows.h>
              
    HBRUSH   hBrushR;
    HBRUSH   hBrushG;
    HBRUSH   hBrushB;
    HBRUSH   hBrushW;
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
      switch(msg) {
        case WM_LBUTTONDOWN:
          SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG) hBrushG);
          InvalidateRect(hwnd,NULL,TRUE);
          return 0; 
        case WM_RBUTTONDOWN:
          SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG) hBrushB);
          InvalidateRect(hwnd,NULL,TRUE);
          return 0; 
        case WM_DESTROY:
          PostQuitMessage (0); 
          return 0;
        default:
          return DefWindowProc(hwnd,msg,wParam,lParam);
      }
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR args,int nShow)
    {
       WNDCLASS wc = {0};
       HWND M_hwnd;
       MSG  msg;
       
       hBrushR = CreateSolidBrush(RGB(255,0,0));   // red
       hBrushG = CreateSolidBrush(RGB(0,255,0));   // green
       hBrushB = CreateSolidBrush(RGB(0,0,255));   // blue
       hBrushW = CreateSolidBrush(RGB(255,255,255));   // white
      
       wc.lpszClassName = "mainClass";
       wc.style         = CS_VREDRAW | CS_HREDRAW;
       wc.hInstance     = hInst;
       wc.hbrBackground = hBrushR;
       wc.lpfnWndProc   = WndProc ;
       wc.hCursor       = LoadCursor(0,IDC_ARROW);
       RegisterClass(&wc);
    
       M_hwnd = CreateWindow("mainClass","test",
                   WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                   50,50,500,400,
                   0,0,hInst,0);
    
       wc.lpszClassName = "childClass";
       wc.style         = 0;
       wc.hInstance     = hInst;
       wc.hbrBackground = hBrushW;
       wc.lpfnWndProc   = WndProc ;
       wc.hCursor       = LoadCursor(0,IDC_ARROW);
       RegisterClass(&wc);
    
       CreateWindow("childClass","",
                   WS_CHILD|WS_BORDER|WS_VISIBLE,
                   50,50,50,50,
                   M_hwnd,0,hInst,0);
    
       while( GetMessage(&msg,0,0,0) > 0) {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
       }
       return (int)msg.wParam;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21