Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 946
  • Last Modified:

Changing style of font displayed in static text control box

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
pcssecure
Asked:
pcssecure
2 Solutions
 
kklai72Commented:
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
 
vijay_visanaCommented:
     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
 
vijay_visanaCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexFMCommented:
0
 
Roshan DavisCommented:
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
 
pcssecureAuthor Commented:
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
 
vijay_visanaCommented:
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
 
waysideCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now