Changing the color of a static control (Win32)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Changing the color of a static control (Win32)

  1. #1
    Join Date
    Jul 2010
    Posts
    75

    Changing the color of a static control (Win32)

    I'm trying to change the color of a static control from blue to purple.
    I managed to change it to blue in the dialog initialization but I can't change it to purple after.

    Here's the working blue color code:
    Code:
    switch(uMsg)
    	{
    	case WM_CTLCOLORSTATIC://wParam: Handle to the device context for the static control window.
    						   //lParam: Handle to the static control.
    		{
    			if ((HWND)lParam == GetDlgItem(hwndDlg,IDC_STATIC3))
    			{
    				SetBkMode((HDC)wParam,TRANSPARENT);
    				SetTextColor((HDC)wParam, RGB(0,0,255));
    				return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU));
    			}
    		}
    	}
    And here's the not working purple color code:
    Code:
    switch(uMsg)
    	{
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDC_STATIC3:
    		{
    			SetTextColor((HDC)GetDC(GetDlgItem(hwndDlg, IDC_STATIC3)), RGB(145,25,139));
    			return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU));//is this needed?
    		}
    	}
    thanks

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Changing the color of a static control (Win32)

    You have to handle this as part of the WM_CTLCOLORSTATIC message.
    In the WM_COMMAND handler set a flag and invalidate the control.
    In the WM_CTLCOLORSTATIC handler check the flag and return the required brush.
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,070

    Re: Changing the color of a static control (Win32)

    Code:
    				return (BOOL)CreateSolidBrush (GetSysColor(COLOR_MENU));
    Really bad idea. You create a new brush before each return and never destroy the one created last time. This is called 'GDI resource leak'. Sooner or later you run off the GDI resources, and your app stops paint.

    You have to create a brush once and return that one whenever it's needed.
    Best regards,
    Igor

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Changing the color of a static control (Win32)

    You're right.
    You got to figure out some magic to delete unused brushes.
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center