VC++ modify control text size and color

Hits: 0

The code to modify the text size of the control is relatively simple as follows:

CFont *stFont4;
    stFont4 = new CFont;
    stFont4->CreateFont(
        0,                         // nWidth
        0,                         // nEscapement
        0,                         // nOrientation
        FW_BOLD,                   // nWeight
        FALSE,                      // bItalic
        FALSE,                     // bUnderline
        0,                         // cStrikeOut
        ANSI_CHARSET,              // nCharSet
        OUT_DEFAULT_PRECIS,        // nOutPrecision
        CLIP_DEFAULT_PRECIS,       // nClipPrecision
        DEFAULT_QUALITY,            // nQuality 
        DEFAULT_PITCH | FF_SWISS,   // nPitchAndFamily 
        _T( "Bold" )                 // lpszFac font type
    );



    CWnd *pWnd = GetDlgItem(IDC_STATIC1); //Get the CWnd pointer of the control whose identification number is IDC-USER

    pWnd->SetFont(stFont4, TRUE);

Modifying the color of the control is a bit more complicated.

The first step is to define the message function in the .h file

BEGIN_MESSAGE_MAP(CAuctionShadowDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_CTLCOLOR() //font color
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_WM_KEYDOWN()

END_MESSAGE_MAP()

The second step is to declare the message function in the header of the .cpp file

HBRUSH CAuctionShadowDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 if (pWnd->GetDlgCtrlID() == IDC_STATIC6||pWnd->GetDlgCtrlID() == IDC_STATIC5) //Multi-line edit control
 {
  pDC->SetTextColor(RGB( 220 , 20 , 60 )); //Set the font color 
  return hbr;
 }
 else
 {

  return hbr;
 }
}

The last is the response message function

HBRUSH CAuctionShadowDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 if (pWnd->GetDlgCtrlID() == IDC_STATIC6||pWnd->GetDlgCtrlID() == IDC_STATIC5) //Multi-line edit control
 {
  pDC->SetTextColor(RGB( 220 , 20 , 60 )); //Set the font color 
  return hbr;
 }
 else
 {

  return hbr;
 }
}

You may also like...

Leave a Reply

Your email address will not be published.