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. A SetFont problem

A SetFont problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 Posts 4 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.
  • V Offline
    V Offline
    Vincent Ye
    wrote on last edited by
    #1

    Hi all, I have a question about SetFont. Now I create a class that is derived from CWnd, and use SetFont to set a font to it. Then I override the Paint function to display the content I want. The problem happens there, no matter what font I set, when I draw the text in Paint, it display with the same font, I think it is the system font. How can I make the SetFont work? Thanks in advance

    C 1 Reply Last reply
    0
    • V Vincent Ye

      Hi all, I have a question about SetFont. Now I create a class that is derived from CWnd, and use SetFont to set a font to it. Then I override the Paint function to display the content I want. The problem happens there, no matter what font I set, when I draw the text in Paint, it display with the same font, I think it is the system font. How can I make the SetFont work? Thanks in advance

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if you are using DrawText or TextOut, you have to use CreateFont/SelectObject . SetFont doesn't do what you think it does. something like this:

      CFont fnt;
      fnt.CreateFont(...);

      CFont *pOldFont = dc.SelectObject(&fnt);

      dc.TextOut(...);
      or
      dc.DrawText(...);

      dc.SelectObject(pOldFont);

      -c CheeseWeasle

      V 1 Reply Last reply
      0
      • C Chris Losinger

        if you are using DrawText or TextOut, you have to use CreateFont/SelectObject . SetFont doesn't do what you think it does. something like this:

        CFont fnt;
        fnt.CreateFont(...);

        CFont *pOldFont = dc.SelectObject(&fnt);

        dc.TextOut(...);
        or
        dc.DrawText(...);

        dc.SelectObject(pOldFont);

        -c CheeseWeasle

        V Offline
        V Offline
        Vincent Ye
        wrote on last edited by
        #3

        Thank you for your reply. After reading your comment, I have another question. I have used SetFont to set a font to it. How can I know which font I have set when I want to use it in Paint function. Because my class is used by other people, I have to use a generic way to know which font they set. Actually I have tried another way, I override the SetFont function in my class, and remember the font they said. It worked. But because the SetFont is not a virtual one, when they use a CWnd pointer to call the SetFont function, my SetFont will not be called. Thanks again.

        C 1 Reply Last reply
        0
        • V Vincent Ye

          Thank you for your reply. After reading your comment, I have another question. I have used SetFont to set a font to it. How can I know which font I have set when I want to use it in Paint function. Because my class is used by other people, I have to use a generic way to know which font they set. Actually I have tried another way, I override the SetFont function in my class, and remember the font they said. It worked. But because the SetFont is not a virtual one, when they use a CWnd pointer to call the SetFont function, my SetFont will not be called. Thanks again.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          vincentye wrote: How can I know which font I have set when I want to use it in Paint function have you tried GetFont() ? -c CheeseWeasle

          V 1 Reply Last reply
          0
          • C Chris Losinger

            vincentye wrote: How can I know which font I have set when I want to use it in Paint function have you tried GetFont() ? -c CheeseWeasle

            V Offline
            V Offline
            Vincent Ye
            wrote on last edited by
            #5

            Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again

            J R 2 Replies Last reply
            0
            • V Vincent Ye

              Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again

              J Offline
              J Offline
              Joel Lucsy
              wrote on last edited by
              #6

              The painting code is right. It sounds more like that the code you're using to test it is somehow not passing the right font. Or perhaps you're creating a font that's local (allocated on the stack) and it's going out of scope. SetFont takes a pointer. If that pointer becomes invalid, I'd suspect that the dc.SelectObject( GetFont() ); would fail. If you're unsure, post the code that calls SetFont and I'll take a look. Joel Lucsy

              V 1 Reply Last reply
              0
              • J Joel Lucsy

                The painting code is right. It sounds more like that the code you're using to test it is somehow not passing the right font. Or perhaps you're creating a font that's local (allocated on the stack) and it's going out of scope. SetFont takes a pointer. If that pointer becomes invalid, I'd suspect that the dc.SelectObject( GetFont() ); would fail. If you're unsure, post the code that calls SetFont and I'll take a look. Joel Lucsy

                V Offline
                V Offline
                Vincent Ye
                wrote on last edited by
                #7

                Thank you for your information. However it is not that case. Below is the answer from MS newsgroup: " There is no default implementation of WM_SETFONT and WM_GETFONT. You need to implement these message handlers to store and return the current HFONT (or NULL as the case may be), and Invalidate if LPARAM is TRUE. IIRC, ClassWizard won't help with these, so you'll need to add them manually using ON_MESSAGE macros in your message-map, and remember that the prototype is 'LRESULT memberFxn(WPARAM, LPARAM)'. In your WM_PAINT handler, you will need to SelectObject the currently stored HFONT (if there is one) into your CPaintDC, and SelectObject it back out again when you're done. -- Jeff Partch [VC++ MVP] " I follow his instruction. It works well. Anyway, thanks again.

                1 Reply Last reply
                0
                • V Vincent Ye

                  Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  When you call SetFont(), are you doing something like this?

                  CFont font;
                  font.CreateFont(...);
                  SetFont(&font);

                  If you do this, the font will be destroyed as soon as the CFont object goes out of scope, so the default system font will be returned by GetFont(). Make sure you either detach the font (CFont::Detach()) or use a font object that is a class member. Hope this helps,

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  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