Solved

Changing style of font displayed in static text control box

Posted on 2004-05-02
11
838 Views
Last Modified: 2013-12-03
Hi experts, I've created a CDialog box and put a Static Text Control in it.

How do i change the font text to bold, the type of fonts and the font size for the text displayed in the control box?
Thanks.
0
Comment
Question by:pcssecure
11 Comments
 
LVL 3

Expert Comment

by:kklai72
ID: 10974864
try this, I got a button, when it is pressed, it changed the font of the static text (with ID IDC_TEXT) to bold type

void CTransparentDlg::OnButton1()
{
      // TODO: Add your control notification handler code here
      CWnd *pWnd;

      pWnd=this->GetDlgItem(IDC_TEXT);
      if (pWnd!=NULL) {
            LOGFONT lf;
            CFont *pOldFont;
            CFont *pNewFont;


            pOldFont=pWnd->GetFont();
            pOldFont->GetLogFont(&lf);
            lf.lfWeight=FW_BOLD;

            pNewFont=new CFont;
            pNewFont->CreateFontIndirect(&lf);
            
            pWnd->SetFont(pNewFont);
            delete pNewFont;

            pWnd->SetFont(pOldFont,FALSE);
      }
}
0
 
LVL 7

Assisted Solution

by:vijay_visana
vijay_visana earned 250 total points
ID: 10974934
     CStatic * pMySt = (CStatic *)GetDlgItem(IDC_STATIC);
      ASSERT(pMySt);
      CFont * pMyFont = pMySt->GetFont();
      LOGFONT LogFont;
      pMyFont->GetLogFont (&LogFont);
      LogFont.lfWeight = FW_BOLD;
      CFont newFont;
      newFont.CreateFontIndirect (&LogFont);
      pMySt->SetFont (&newFont);
0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10974944
see the LOGFONT stucture
I have just change the Weight field but you can manipulate other fields to get desired font for your control.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:AlexFM
ID: 10974977
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 250 total points
ID: 10975301
A small change in VV's code :)

make this object as class member
in the header of the class put
CFont m_newFont;



    CStatic * pMySt = (CStatic *)GetDlgItem(IDC_STATIC);
     ASSERT(pMySt);
     CFont * pMyFont = pMySt->GetFont();
     LOGFONT LogFont;
     pMyFont->GetLogFont (&LogFont);
     LogFont.lfWeight = FW_BOLD;

     m_newFont.CreateFontIndirect (&LogFont);
     pMySt->SetFont (&m_newFont);

Rosh :)
0
 

Author Comment

by:pcssecure
ID: 10975483
hi vijay, i've successfully changed the text to bold but i can't seems to change the font size.
I tried to change the font size using the following statement, but the size is still the same.

logFont.lfHeight = -MulDiv(20, dc->GetDeviceCaps(LOGPIXELSY), 72);  

Pls advice.
Thks!
0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10975814
In my code it is working fine.
do the following

HDC hdc = GetDC()->m_hDC ;
ASSERT(hdc);
int PointSize = 20;
int nHight = -MulDiv(PointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
//check nHight here in my pc it is -27
LogFont.lfHeight = nHight ;

0
 
LVL 14

Expert Comment

by:wayside
ID: 10976845
Are you trying to change one specific static text item, or all of the text in the dialog to the same font?

If you want to change all of them, you can change the font, size, and weight by setting a dialog property. For VC++.Net, right-mouse on the dialog title bar in the resource editor, select properties. In the properties window is a Font section that lets you change the font attributes.

I think there is someething equivalent in VC++6.0.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert MFC::CString to UTF8 wchar_t* 10 277
Annoying "thing" blocks my view 4 115
Increment column based of a FK 8 31
moto 4g phone enable finger print 4 26
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question