Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange Font bolding

Strange Font bolding

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    super_ttd
    wrote on last edited by
    #1

    Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type CStatic on it. here is the code i put in the dialog's OnInitDialog() :

    HDC hdc = (HDC)(\*m\_myStatic.GetDC());
    TEXTMETRIC tm;
    ::GetTextMetrics(hdc, &tm);
    int n = ::GetTextFace(hdc, 0, NULL);
    TCHAR\* pszFontName = new TCHAR\[n\];
    ::GetTextFace(hdc, n, pszFontName);
    CFont nf;
    nf.CreateFont(tm.tmHeight,
                  tm.tmAveCharWidth,
                  0,
                  0,
                  tm.tmWeight,
                  FALSE,
                  tm.tmUnderlined,
                  tm.tmStruckOut,
                  tm.tmCharSet,
                  OUT\_DEFAULT\_PRECIS,
                  CLIP\_DEFAULT\_PRECIS,
                  DEFAULT\_QUALITY,
                  tm.tmPitchAndFamily,
                  pszFontName);
    delete\[\] pszFontName;
    m\_myStatic.SetFont(&nf);
    

    as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?

    N D 2 Replies Last reply
    0
    • S super_ttd

      Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type CStatic on it. here is the code i put in the dialog's OnInitDialog() :

      HDC hdc = (HDC)(\*m\_myStatic.GetDC());
      TEXTMETRIC tm;
      ::GetTextMetrics(hdc, &tm);
      int n = ::GetTextFace(hdc, 0, NULL);
      TCHAR\* pszFontName = new TCHAR\[n\];
      ::GetTextFace(hdc, n, pszFontName);
      CFont nf;
      nf.CreateFont(tm.tmHeight,
                    tm.tmAveCharWidth,
                    0,
                    0,
                    tm.tmWeight,
                    FALSE,
                    tm.tmUnderlined,
                    tm.tmStruckOut,
                    tm.tmCharSet,
                    OUT\_DEFAULT\_PRECIS,
                    CLIP\_DEFAULT\_PRECIS,
                    DEFAULT\_QUALITY,
                    tm.tmPitchAndFamily,
                    pszFontName);
      delete\[\] pszFontName;
      m\_myStatic.SetFont(&nf);
      

      as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      super_ttd wrote:

      CFont nf; nf.CreateFont(tm.tmHeight, tm.tmAveCharWidth, 0, 0, tm.tmWeight, FALSE, tm.tmUnderlined, tm.tmStruckOut, tm.tmCharSet, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, tm.tmPitchAndFamily, pszFontName);

      You are creating a local CFont object. Hence the CFont object get's destroyed. This is the reason for this wierd behavior, might be since the font object got destroyed it's using DEFAULT_GUI_FONT.:~

      From MSDN:

      Specifically, do not destroy the specified CFont object until after the CWnd control has been destroyed.
      Windows does not copy the font specified in a SetFont() call.
      If the font is destroyed before the control is destroyed, unpredictable results can occur.

      "new" the font object and see the difference... And by the way you don't have to write this much code for modifying an existing font object... Here is the code snippet that does this...

      CFont* pFont = m_cstc.GetFont();
      LOGFONT lfLogFont = { 0 };
      pFont->GetLogFont( &lfLogFont );
      lfLogFont.lfWeight = FW_BOLD;

      CFont pNewFont = new CFont;// Delete this afterwards, or make this a member variable
      pNewFont->CreateFontIndirect( &lfLogFont );
      m_cstc.SetFont( pNewFont );


      Nibu thomas A Developer Programming tips[^]  My site[^]

      1 Reply Last reply
      0
      • S super_ttd

        Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type CStatic on it. here is the code i put in the dialog's OnInitDialog() :

        HDC hdc = (HDC)(\*m\_myStatic.GetDC());
        TEXTMETRIC tm;
        ::GetTextMetrics(hdc, &tm);
        int n = ::GetTextFace(hdc, 0, NULL);
        TCHAR\* pszFontName = new TCHAR\[n\];
        ::GetTextFace(hdc, n, pszFontName);
        CFont nf;
        nf.CreateFont(tm.tmHeight,
                      tm.tmAveCharWidth,
                      0,
                      0,
                      tm.tmWeight,
                      FALSE,
                      tm.tmUnderlined,
                      tm.tmStruckOut,
                      tm.tmCharSet,
                      OUT\_DEFAULT\_PRECIS,
                      CLIP\_DEFAULT\_PRECIS,
                      DEFAULT\_QUALITY,
                      tm.tmPitchAndFamily,
                      pszFontName);
        delete\[\] pszFontName;
        m\_myStatic.SetFont(&nf);
        

        as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        super_ttd wrote:

        can anyone explain me why the font (Tahoma) is acting like this ?

        Don't make things any more complicated than necessary. See here.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        S 1 Reply Last reply
        0
        • D David Crow

          super_ttd wrote:

          can anyone explain me why the font (Tahoma) is acting like this ?

          Don't make things any more complicated than necessary. See here.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          S Offline
          S Offline
          super_ttd
          wrote on last edited by
          #4

          hi David, Thanks for answering. but you know, i refered this exact post to initiate my test, and i found that the answers to this guy were far from being complete... see, i couldn't figure out myself the right way with your 3 replies there. but thanks to Babu, I think I'm on the road again :cool:

          N 1 Reply Last reply
          0
          • S super_ttd

            hi David, Thanks for answering. but you know, i refered this exact post to initiate my test, and i found that the answers to this guy were far from being complete... see, i couldn't figure out myself the right way with your 3 replies there. but thanks to Babu, I think I'm on the road again :cool:

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            super_ttd wrote:

            thanks to Babu

            Nibu babu thomas. ;)


            Nibu thomas A Developer Programming tips[^]  My site[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups