Thread: Common Font Dialog Box

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    96

    Common Font Dialog Box

    I have a function that displays the change font dialog box. It changes the font but not the font color interestingly MSDN tells you exactly how to do it but it doesn't work.

    Here is the MSDN link.

    http://msdn.microsoft.com/library/de...boxlibrary.asp

    Here is my code.

    font.h
    Code:
    #ifndef FONT_H
    #define FONT_H
    
    class CFont
    {
    	public:
    	    
    	    CFont();
    	    ~CFont();
    	    
    	    bool Change(HWND hParentWindow, HDC hDC, int iEditControlID);
    	    
    	    HFONT g_hFont;
    	    	    
    	private:
    	    
    	    CHOOSEFONT m_cf;
    	    
    	    LOGFONT m_lf;
    	    
    	    HFONT m_hFontPrevious;
    	    
    	    COLORREF m_rgbCurrent, m_rgbPrevious;    
    };
    
    #endif // FONT_H
    font.cpp
    Code:
    #include <windows.h>
    #include "font.h"
    
    CFont::CFont()
    {
    	
    }
    
    CFont::~CFont()
    {
    	
    }
    
    bool CFont::Change(HWND hParentWindow, HDC hDC, int iEditControlID)
    {
    	ZeroMemory(&m_cf, sizeof(m_cf));
    	
    	hDC = GetDC(hParentWindow);
    		
    	m_cf.lStructSize = sizeof(m_cf);
    	m_cf.hwndOwner = hParentWindow;
    	m_cf.lpLogFont = &m_lf;
    	m_cf.rgbColors = m_rgbCurrent;
    	m_cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
    	
    	if(!ChooseFont(&m_cf))
    	{       
    		return false;        
    	}
    	
    	g_hFont = CreateFontIndirect(m_cf.lpLogFont);		
    		
    	m_hFontPrevious = (HFONT)SelectObject(hDC, g_hFont);	
    	
    	m_rgbCurrent = m_cf.rgbColors;
    	   
    	m_rgbPrevious = SetTextColor(hDC, m_rgbCurrent);	 	 
    	
    	SendDlgItemMessage(hParentWindow, iEditControlID, 
                           WM_SETFONT, (WPARAM)g_hFont, MAKELPARAM(TRUE, 0));	
    			
    	return true && g_hFont;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The font and colour are separate properties. WM_SETFONT will set the control font but you'll need to use SetTextColor to change the colour. Assuming iEditControlID is for an edit control then handle the WM_CTLCOLOREDIT (WM_CTLCOLORSTATIC for read-only edits) message and set it there. If the id is not for an edit control then you'll need to use the relevant WM_CTLCOLOR* message.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    SetTextColor() only changes the color when your are using a DC draw function like TextOut(). If you just want to change the color of static text, then handle WM_CTLCOLORSTATIC as Ken suggested.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    I'm sorta lost this is what I did and nothing happened.

    Code:
    SendMessage(hParentWindow, WM_CTLCOLOREDIT, (WPARAM)hDC, iEditControlID);
    The edit control is created as follows.

    Code:
    bool CEditControl::Create(DWORD dwStyleEx, DWORD dwStyle, int iX, int iY, 
                              int iWidth, int iHeight, HWND hParentWindow, 
                              HINSTANCE hInstance)
    {	    
    	g_hEdit = CreateWindowEx(
    	          dwStyleEx,
    	          "EDIT",
    	          NULL,
    	          dwStyle,
    	          iX,
    	          iY,
    	          iWidth,
    	          iHeight,
    	          hParentWindow,
    	          (HMENU)g_iEditControlID,
    	          hInstance,
    	          NULL
    	          );
    
         if(!g_hEdit)
         {
         	MessageBox(hParentWindow, "Can't create edit control!", "Error!",
         	           MB_ICONEXCLAMATION | MB_OK);
         	return false;
         }    
    	          
    	SendMessage(g_hEdit, WM_SETFONT, 
    	            (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
    	            MAKELPARAM(TRUE, 0));
    	            
    	return true && g_hEdit && g_iEditControlID;                    
    }
    Here is the call to Font.Change
    Code:
    Font.Change(hwnd, g_hDC, EditControl.g_iEditControlID);

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The WM_CTLCOLOR* messages for controls are notifications to the parent window so you need to handle those messages, not send them. To change the text colour to, for example, red use SetTextColor as I have previously described:
    Code:
    case WM_CTLCOLOREDIT:
      {
      HDC hdc=(HDC)wParam;
      
      SetTextColor(hdc,RGB(200,0,0));
      return (LRESULT)GetStockObject(WHITE_BRUSH);
      }
    Last edited by Ken Fitlike; 09-14-2005 at 12:09 PM. Reason: misspelt colour
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Well I got it to work now thanks. The only problem is when I handle the WM_CTLCOLOREDIT message my program ignores my manifest file.
    How can I get it to use the Windows XP visual styles now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Common Dialog Box in C#?
    By stevespai in forum C# Programming
    Replies: 5
    Last Post: 08-07-2006, 06:14 PM
  3. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM