Setting back/foregroundcolor in a static control

This is a discussion on Setting back/foregroundcolor in a static control within the Windows Programming forums, part of the Platform Specific Boards category; I tried to change this when i altered the font: Code: int SetFont(HWND hWnd, int iSize, char *achFace){ int iRet, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Setting back/foregroundcolor in a static control

    I tried to change this when i altered the font:
    Code:
    int SetFont(HWND hWnd, int iSize, char *achFace){
    	int iRet, iHeight;
    	HFONT hf;
    	HDC hDC;
    
    	hDC=GetDC(hWnd);
    	iHeight = -MulDiv(iSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    	hf=CreateFont(iHeight ,0 ,0 ,0 ,FW_THIN ,0 ,0 ,0 ,1 ,OUT_DEFAULT_PRECIS ,0 ,PROOF_QUALITY ,0 ,achFace);
    	iRet=SendMessage(hWnd,WM_SETFONT,(WPARAM)hf, MAKELPARAM(TRUE, 0));
    	SetBkColor(hDC, 0xFF0000);
    	ReleaseDC(hWnd, hDC);
    	return(iRet);
    }
    This doesn't work. In addition i'd like to set the foregroudcolor as well.
    Could anyone hint me in the right direction?

    Edit:
    I forgot to mention that I'm using MSVC++ 6.0, resource script, and not MFC
    Last edited by knutso; 07-23-2003 at 06:06 AM.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    SetTextColor(hDC, RGB(255, 255, 255));
    SetBkColor(hDC, RGB(100, 100, 100));
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    It doesn't change the color:
    Code:
    int SetFont(HWND hWnd, int iSize, char *achFace){
    	int iRet, iHeight;
    	HFONT hf;
    	HDC hDC;
    
    	hDC=GetDC(hWnd);
    	iHeight = -MulDiv(iSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    	hf=CreateFont(iHeight ,0 ,0 ,0 ,FW_THIN ,0 ,0 ,0 ,1 ,OUT_DEFAULT_PRECIS ,0 ,PROOF_QUALITY ,0 ,achFace);
    	iRet=SendMessage(hWnd,WM_SETFONT,(WPARAM)hf, MAKELPARAM(TRUE, 0));
    	SetTextColor(hDC, RGB(255, 0, 0));
    	SetBkColor(hDC, RGB(0, 0, 0));
    	ReleaseDC(hWnd, hDC);
    	return(iRet);
    }

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Sorry, I saw your call to SetBkColor() and assumed you just wanted to change text attributes.

    Have a look at the WM_CTLCOLORSTATIC message.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Thanks Adrianxv.
    I am familiar with the CTL_COLORSTATIC message;I was just hoping for a simple function to do this...Anyway: I'll give it a try.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    This is what i've managed:
    Code:
    if((HWND)lParam == GetDlgItem(hwThis, LBL_HEADER)){
    	SetTextColor ((HDC)wParam, 0xFFFFFF) ;
    	SetBkColor ((HDC)wParam, RGB(150,150,150));
    	return((int)CreateSolidBrush(RGB(150,150,150)));
    }
    From MSDN:
    The system does not automatically destroy the returned brush. It is the application's responsibility to destroy the brush when it is no longer needed.
    How can i destroy the brush when the function has this as return value????

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Create the brush in the windows WM_CREATE handler, store the handle in a static HBRUSH, then in WM_DESTROY use DeleteObject() to destroy the brush.

    You shouldn't use GDI creates in the way you are doing because you then do not have the handle to delete them and you'll leak.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Thanks again, it's working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 06:24 PM
  2. Replies: 23
    Last Post: 07-09-2007, 05:49 AM
  3. WM_SETFONT, Static Control
    By Mecnels in forum Windows Programming
    Replies: 3
    Last Post: 04-30-2003, 01:47 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM
  5. Setting static text color -- I know I'm doing something wrong
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 01-26-2002, 01:59 PM

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