CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2004
    Posts
    28

    WinAPI : problem changing color of a CheckBox

    Hello,

    I'm trying to change the color of the CheckBox but not having much luck so far. Here's how I create a CheckBox:

    Code:
    			hButton = CreateWindow("button", "CheckBox Caption",
    					WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
    					0,0,150,20,hWnd,(HMENU)ID_CHECKBOX1,
    					hInstance, NULL);
    ^^^ this is executed on WM_CREATE message.

    and it shows up on a grey background and font color is set to black. I can't change neither the color of a font nor the background.

    Are colors set when the object is created or it's done separately? I've tried lots of things, even change the SysColors but still no luck. The solution is probably very simple, but I can't figure it so far.

  2. #2
    Join Date
    May 2004
    Posts
    28
    Ok ok, at least tell me where to look for the answer, please

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    The parent of the check box will get a WM_CTLCOLOR message when it needs to be painted.

    You need to handle the WM_CTLCOLOR in the dialog.

    Check for the nCtlColor value and the window handle passed to see if it is the check box window.

    If so,

    1. you can call SetTextColor( hdc, your own color ). This will change the font color
    2. To change the bkgnd color, return a brush handle with the proper bkgnd color.

  4. #4
    Join Date
    May 2004
    Posts
    28
    first off, thank you for the reply.

    I did try the SetTextColor function but it's a no go (but then again, maybe I do it improperly, I'm kinda new at WinAPI).

    And as for WM_CTLCOLOR it says undeclared identifier, so the message does not even exist? Any suggestions? I appreciate the help though

    here's the whole text

    Code:
    #include <windows.h>
    
    #define ID_CHECKBOX1 3000
    
    HINSTANCE hInstance;
    
    LRESULT CALLBACK DzWndProc (HWND, UINT, UINT, LONG);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR IpszCmdParam, int nCmdShow)
    
    {
    	HWND hWnd;
    	WNDCLASS WndClass;
    	MSG Msg;
    	char szClassName[] = "DZ";
    
    	WndClass.style = CS_HREDRAW | CS_VREDRAW;
    	WndClass.lpfnWndProc = DzWndProc;
    	WndClass.cbClsExtra = 0;
    	WndClass.cbWndExtra = 0;
    	WndClass.hInstance = hInstance;
    	WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	WndClass.hCursor = LoadCursor(NULL, IDC_CROSS);
    	WndClass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
    	WndClass.lpszMenuName = NULL;
    	WndClass.lpszClassName = szClassName;
    
    	if (!RegisterClass(&WndClass))
    	{
    		MessageBox (NULL, "Cannot register class", "Error", MB_OK);
    		return 0;
    	}
    
    	hWnd = CreateWindow (szClassName,
    								"WinAPI DZ",
    								WS_OVERLAPPEDWINDOW,
    			CW_USEDEFAULT,
    			CW_USEDEFAULT,
    			CW_USEDEFAULT,
    			CW_USEDEFAULT,
    			NULL,
    			NULL,
    			hInstance,
    			NULL);
    	if (!hWnd)
    	{
    		MessageBox (NULL, "Cannot create window", " Error", MB_OK );
    		return 0;
    	}
    
    	ShowWindow (hWnd, SW_SHOWMAXIMIZED);
    	UpdateWindow(hWnd);
    
    	while (GetMessage(&Msg,NULL,0,0))
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    
    }
    
    LRESULT CALLBACK DzWndProc (HWND hWnd, UINT Message,
    									UINT wParam, LONG IParam)
    {
    	HDC hDC;
    	PAINTSTRUCT PaintStruct;
    	RECT Rect;
    	HPEN hPen;	
    	static HWND hButton;
    
    	switch (Message)
    	{
    		
    		case WM_CREATE :
    		{
    			hDC = GetDC(hWnd);
    			SetTextColor(hDC,RGB(255,255,0)); // this is one of the tries (it failed)
    			hButton = CreateWindow("button", "CheckBox Caption",
    					WS_CHILD | WS_VISIBLE | BS_CHECKBOX ,
    					0,0,150,20,hWnd,(HMENU)ID_CHECKBOX1,
    					hInstance, NULL);
    					return 0;
    		}
    		
    		case WM_PAINT :
    		{
    			hDC = BeginPaint (hWnd, &PaintStruct);
    			GetClientRect(hWnd,&Rect);
    			DrawText(hDC,
    				"How to change the background color of that CHECKBOX in the upper left corner? And its font color?",
    					-1,
    					&Rect,
    					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    			EndPaint(hWnd,&PaintStruct);
    			return 0; 
    		}
    
    
    		case WM_DESTROY : 
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    
    
    		case WM_COMMAND :
    		{
    			switch(LOWORD(wParam))
    			{
    				case ID_CHECKBOX1:
    				if (SendMessage(hButton,BM_GETCHECK,NULL,NULL)==0)
    				SendMessage(hButton,BM_SETCHECK, 1, NULL);
    				  else
    				SendMessage(hButton,BM_SETCHECK, 0, NULL);
    			}
    		}
    
    
    	}
    	return DefWindowProc (hWnd, Message, wParam, IParam);
    }
    P.S. I've been told to define a class for the button and change those parameters there, but I can't find the info on how to define a class for the Button! Something like WNDCLASS WndClass but for a button...

  5. #5
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97
    Thats strange.... WM_CTLCOLOR is declared in <windows.h> What compiler are you using?

    You should set the wParam and lParam parameters as so:

    WPARAM wParam // Handle to a display context (DC).
    LPARAM lParam; // Handle to a child window (control).

  6. #6
    Join Date
    May 2004
    Posts
    28
    Microsoft's compiler... I also thought it was strange it didn't recognize it

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    1. In the wndproc add
    static HBRUSH hBrush;
    2. After createWindow before returning call this
    hBrush = CreateSolidBrush(RGB(255,0,0));
    3. Add this:
    Code:
    case WM_CTLCOLORSTATIC :
          {
            HDC hdc = (HDC)wParam;
            HWND hWnd = (HWND)IParam;
            if(hWnd == hButton)
            {
              SetBkMode(hdc,TRANSPARENT);
              SetTextColor(hdc,RGB(0,255,0));
              return (LRESULT)hBrush;
            }
          }
          break;
    The problem is the checkbox sends a CTLCOLORSTATIC and not CTLCOLORBTN !! Strange !!!!!!!!!

  8. #8
    Join Date
    May 2004
    Posts
    28
    Thank you so much!!! Everything's working great!

  9. #9
    Join Date
    Apr 2003
    Location
    Regensburg, Bavaria, Germany
    Posts
    147
    Not to be mean or anything, but I compiled the code that you presented and there are a 8 warnings.

    These 2 lines of code are unnecessary, the first one makes an error...
    Code:
    HPEN hPen;
    ...
    SetTextColor(hDC,RGB(255,255,0));
    Also, in the COMMAND section, there are still warnings with each SendMessage method. I wouldn't call myself an expert in this C WinAPI, but I think those are pretty useful tips
    Favorite music:
    Rammstein
    E nomine
    Prodigy

    "Beer, the solution and the cause of all of our problems" -- Homer Simpson

  10. #10
    Join Date
    May 2004
    Location
    RUSSIA
    Posts
    4
    All that manipulations do not depend at systemcolor
    If it becomes the same as you controls they will become invisible.

    So i recomend to replace them for each COLORREF`s with
    GetSysColor(COLOR_xxx)^YOUR_COLOR;
    where xxx is certain windows element
    COLOR_WINDOW
    COLOR_TEXT
    COLOR_MENU
    etc.etc.etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured