Changing the Tab Label Font

We can change the font used by the tab control to draw the labels by using the SetFont() function in the CWnd class. The code given below is very simple and does not bother with resizing the tab control or the property sheet. This works fine for fonts that are smaller than the font being used by the tab control or about the same size. A good place to put this code in is the OnInitDialog() of the CPropertySheet derived class.
	// m_fontEdit is a member variable
	// Create a bold font
	m_fontEdit.CreateFont( -8, 0, 0, 0, 700, 0, 0, 0, 1, 
			0, 0, 0, 0, _T("MS Sans Serif") );
	GetTabControl()->SetFont( &m_fontEdit );



Comments

  • MS Property Sheet example

    Posted by Legacy on 06/28/2003 12:00am

    Originally posted by: Mick

    There is an example on the Microsoft Knowledge base on how to do things with fonts in Property sheet:

    Microsoft Knowledge Base Article - 142170

    It includes some sample code. From this example it's pretty easy to work out what to do. Personnaly I wanted to get my property sheet to resize itself at run time using the system font size.

    Reply
  • Change Font to Active Page

    Posted by Legacy on 03/24/2003 12:00am

    Originally posted by: Antonio

    Hi
    I'de like to distinguish the tab that has focus not using the HighlightItem() function but changing its caption text in bold font.
    Please help me to realize it.

    Thanks. Antonio

    • How to change propertysheet font

      Posted by emp on 09/01/2004 07:26am

      // Use this function in Sheet::OnInitDialog;
      // Inherit the sheet::BuildPropPageArray to get dialog resource template font
      void ChangeDialogFont(CWnd* pWnd, CFont* pFont, int nFlag)
      {
      	CRect windowRect;
      
      	// grab old and new text metrics
      	TEXTMETRIC tmOld, tmNew;
      	CDC * pDC = pWnd->GetDC();
      	CFont * pSavedFont = pDC->SelectObject(pWnd->GetFont());
      	pDC->GetTextMetrics(&tmOld);
      	pDC->SelectObject(pFont);
      	pDC->GetTextMetrics(&tmNew);
      	pDC->SelectObject(pSavedFont);
      	pWnd->ReleaseDC(pDC);
      
      	long oldHeight = tmOld.tmHeight+tmOld.tmExternalLeading;
      	long newHeight = tmNew.tmHeight+tmNew.tmExternalLeading;
      
      	if (nFlag != CDF_NONE)
      	{
      		// calculate new dialog window rectangle
      		CRect clientRect, newClientRect, newWindowRect;
      
      		pWnd->GetWindowRect(windowRect);
      		pWnd->GetClientRect(clientRect);
      		long xDiff = windowRect.Width() - clientRect.Width();
      		long yDiff = windowRect.Height() - clientRect.Height();
      	
      		newClientRect.left = newClientRect.top = 0;
      		newClientRect.right = clientRect.right * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth;
      		newClientRect.bottom = clientRect.bottom * newHeight / oldHeight;
      
      		if (nFlag == CDF_TOPLEFT) // resize with origin at top/left of window
      		{
      			newWindowRect.left = windowRect.left;
      			newWindowRect.top = windowRect.top;
      			newWindowRect.right = windowRect.left + newClientRect.right + xDiff;
      			newWindowRect.bottom = windowRect.top + newClientRect.bottom + yDiff;
      		}
      		else if (nFlag == CDF_CENTER) // resize with origin at center of window
      		{
      			newWindowRect.left = windowRect.left - 
      							(newClientRect.right - clientRect.right)/2;
      			newWindowRect.top = windowRect.top -
      							(newClientRect.bottom - clientRect.bottom)/2;
      			newWindowRect.right = newWindowRect.left + newClientRect.right + xDiff;
      			newWindowRect.bottom = newWindowRect.top + newClientRect.bottom + yDiff;
      		}
      		pWnd->MoveWindow(newWindowRect);
      	}
      
      	pWnd->SetFont(pFont);
      
      	// iterate through and move all child windows and change their font.
      	CWnd* pChildWnd = pWnd->GetWindow(GW_CHILD);
      
      	while (pChildWnd)
      	{
      		pChildWnd->SetFont(pFont);
      		pChildWnd->GetWindowRect(windowRect);
      
      		CString strClass;
      		::GetClassName(pChildWnd->m_hWnd, strClass.GetBufferSetLength(32), 31);
      		strClass.MakeUpper();
      		if(strClass==_T("COMBOBOX"))
      		{
      			CRect rect;
      			pChildWnd->SendMessage(CB_GETDROPPEDCONTROLRECT,0,(LPARAM) &rect);
      			windowRect.right = rect.right;
      			windowRect.bottom = rect.bottom;
      		}
      
      		pWnd->ScreenToClient(windowRect);
      		windowRect.left = windowRect.left * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth;
      		windowRect.right = windowRect.right * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth;
      		windowRect.top = windowRect.top * newHeight / oldHeight;
      		windowRect.bottom = windowRect.bottom * newHeight / oldHeight;
      		pChildWnd->MoveWindow(windowRect);
      		
      		pChildWnd = pChildWnd->GetWindow(GW_HWNDNEXT);
      	}
      }

      Reply
    • Change propersheet font

      Posted by emp on 09/01/2004 07:19am

      Use the following function for sheet and its pages; void ChangeDialogFont(CWnd* pWnd, CFont* pFont, int nFlag) { CRect windowRect; // grab old and new text metrics TEXTMETRIC tmOld, tmNew; CDC * pDC = pWnd->GetDC(); CFont * pSavedFont = pDC->SelectObject(pWnd->GetFont()); pDC->GetTextMetrics(&tmOld); pDC->SelectObject(pFont); pDC->GetTextMetrics(&tmNew); pDC->SelectObject(pSavedFont); pWnd->ReleaseDC(pDC); long oldHeight = tmOld.tmHeight+tmOld.tmExternalLeading; long newHeight = tmNew.tmHeight+tmNew.tmExternalLeading; if (nFlag != CDF_NONE) { // calculate new dialog window rectangle CRect clientRect, newClientRect, newWindowRect; pWnd->GetWindowRect(windowRect); pWnd->GetClientRect(clientRect); long xDiff = windowRect.Width() - clientRect.Width(); long yDiff = windowRect.Height() - clientRect.Height(); newClientRect.left = newClientRect.top = 0; newClientRect.right = clientRect.right * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth; newClientRect.bottom = clientRect.bottom * newHeight / oldHeight; if (nFlag == CDF_TOPLEFT) // resize with origin at top/left of window { newWindowRect.left = windowRect.left; newWindowRect.top = windowRect.top; newWindowRect.right = windowRect.left + newClientRect.right + xDiff; newWindowRect.bottom = windowRect.top + newClientRect.bottom + yDiff; } else if (nFlag == CDF_CENTER) // resize with origin at center of window { newWindowRect.left = windowRect.left - (newClientRect.right - clientRect.right)/2; newWindowRect.top = windowRect.top - (newClientRect.bottom - clientRect.bottom)/2; newWindowRect.right = newWindowRect.left + newClientRect.right + xDiff; newWindowRect.bottom = newWindowRect.top + newClientRect.bottom + yDiff; } pWnd->MoveWindow(newWindowRect); } pWnd->SetFont(pFont); // iterate through and move all child windows and change their font. CWnd* pChildWnd = pWnd->GetWindow(GW_CHILD); while (pChildWnd) { pChildWnd->SetFont(pFont); pChildWnd->GetWindowRect(windowRect); CString strClass; ::GetClassName(pChildWnd->m_hWnd, strClass.GetBufferSetLength(32), 31); strClass.MakeUpper(); if(strClass==_T("COMBOBOX")) { CRect rect; pChildWnd->SendMessage(CB_GETDROPPEDCONTROLRECT,0,(LPARAM) &rect); windowRect.right = rect.right; windowRect.bottom = rect.bottom; } pWnd->ScreenToClient(windowRect); windowRect.left = windowRect.left * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth; windowRect.right = windowRect.right * tmNew.tmAveCharWidth / tmOld.tmAveCharWidth; windowRect.top = windowRect.top * newHeight / oldHeight; windowRect.bottom = windowRect.bottom * newHeight / oldHeight; pChildWnd->MoveWindow(windowRect); pChildWnd = pChildWnd->GetWindow(GW_HWNDNEXT); } }

      Reply
    Reply
  • Font size problem in the code

    Posted by Legacy on 01/31/2003 12:00am

    Originally posted by: Jagadees

    Hi,
    I added code in the OnInitDialog of CPropertySheet for changing the font. It worked. But there is one problem, my property sheet size and font size shrinked. Please help me to solve this. We used -8 as font height in CreateFont.

    TIA,
    Jagadees.

    Reply
  • How do you change the Font of just the active page

    Posted by Legacy on 09/26/2000 12:00am

    Originally posted by: Lou

    This code is simple and works well if you want all the pages in the PropertySheet to be same Font. How do you change the font of the Tab label on JUST the active page?

    Thanks,
    Lou

    Reply
  • How can I set the focus rectangle in the tabs of an PropertySheet?

    Posted by Legacy on 07/27/2000 12:00am

    Originally posted by: Ulf Lohmann

    Hi there,

    can anybody tell me how I can set the focus rectangle in the tabs of a property sheet programatically?

    The MFC seems to show the focus recangle only when no control inside the active page can recieve the input focus.

    Reply
  • How can I change Tab Label Font in this sample ?

    Posted by Legacy on 08/31/1999 12:00am

    Originally posted by: Jeffrey

    Hi there,

    I have a problem when I try to put several property pages inside a SDI view.

    I found following sample project in VC Books online, <SAMPLE: Property Sheet as the View Window of a CFrameWnd, Article ID: Q161886 >. What I want to do is changing the font of each tab title, ie. "Name", "Order Entry". Do you have any good idea for this ?

    The sample can also be found by searching key word "Psview" in MSDN library.

    Thanks a lot

    Reply
  • CPropertySheet Fonts

    Posted by Legacy on 08/17/1999 12:00am

    Originally posted by: Doug Watts

    MFC has changed (since version 4.0) regarding its use of property sheets. Since version 4.0 MFC's CPropertySheet is designed to use the system font and all pages will use the font of the sheet. There is an "undocumented" work-around that, as any undocumented coding, is subject to change in the future.

    For details, search MSDN knowledgebase for article #Q142170.

    Reply
  • Change Font for Property Sheet

    Posted by Legacy on 03/26/1999 12:00am

    Originally posted by: David Zhu

    I changed Font size to 12 for each property page, but when
    I addpage to property sheet and pop it up, everything
    change back to font size 8. Can anybody help me?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds