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. Font size

Font size

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestionhelp
14 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.
  • G garfield185

    Hi everybody. I have noticed that some controls in Visual C++ allow to change their font size. Others don´t allow it, and it takes it from the size you set on the Dialog properties. To change that, I have created my own font with the CFont class like this: ************************************ CFont m_fuente; LOGFONT lf; m_fuente.CreateStockObject(DEFAULT_GUI_FONT); m_fuente.GetLogFont(&lf); strcpy(lf.lfFaceName,"MS Sans Serif"); lf.lfWeight =700; lf.lfHeigth =16; m_fuente.DeleteObject(); m_fuente.CreateFontIndirect(&lf); **************************************** My question is about the size. Using lf.lfHeigth = 20 will not make words bigger, it will just make the space around the word bigger (check it in a combo box, for example, increasing the lfheigth of a combo box would make the space between words bigger. Does anybody know how to fix this? Thank you!!!

    Time to come clean... Vive y deja vivir / Live and let live Javier

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #2

    See the nHeight explanation here [^]. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    G 1 Reply Last reply
    0
    • C CPallini

      See the nHeight explanation here [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      G Offline
      G Offline
      garfield185
      wrote on last edited by
      #3

      I guess it is all about that line that I do not understand ;P nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); Can you explain me what it means? Thanks!

      Time to come clean... Vive y deja vivir / Live and let live Javier

      H C 2 Replies Last reply
      0
      • G garfield185

        I guess it is all about that line that I do not understand ;P nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); Can you explain me what it means? Thanks!

        Time to come clean... Vive y deja vivir / Live and let live Javier

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #4

        This code is for convert of points to logical device coordinates.


        WhiteSky


        G 1 Reply Last reply
        0
        • H Hamid Taebi

          This code is for convert of points to logical device coordinates.


          WhiteSky


          G Offline
          G Offline
          garfield185
          wrote on last edited by
          #5

          O_o Aha... So how do I say that I want the font size to be 20, for example? ;P

          Time to come clean... Vive y deja vivir / Live and let live Javier

          C 1 Reply Last reply
          0
          • G garfield185

            I guess it is all about that line that I do not understand ;P nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); Can you explain me what it means? Thanks!

            Time to come clean... Vive y deja vivir / Live and let live Javier

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #6

            garfield185 wrote:

            I guess it is all about that line that I do not understand

            You also have to be aware that the system matches your request again available fonts.

            garfield185 wrote:

            nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); Can you explain me what it means?

            OK. It simply states that nHeight isn't the pixel height of the font, but it is related with it by the factor GetDeviceCaps(hDC, LOGPIXELSY)/72 that is system-dependant. On my Laptop the factor is 1.33 meaning that I have to specify nHeight=27 (let's forget for the minus sign) to obtain a 20-point sized font (and nHeight=21 to obtain the 16-point one). On your system maybe the difference between the requested font heights it's so small that the same font is choosen. Hope that helps. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            G 1 Reply Last reply
            0
            • G garfield185

              O_o Aha... So how do I say that I want the font size to be 20, for example? ;P

              Time to come clean... Vive y deja vivir / Live and let live Javier

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              garfield185 wrote:

              So how do I say that I want the font size to be 20, for example?

              you have to set nHeight as follows:

              nHeight = -MulDiv(20, GetDeviceCaps(hDC, LOGPIXELSY), 72);

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              1 Reply Last reply
              0
              • C CPallini

                garfield185 wrote:

                I guess it is all about that line that I do not understand

                You also have to be aware that the system matches your request again available fonts.

                garfield185 wrote:

                nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); Can you explain me what it means?

                OK. It simply states that nHeight isn't the pixel height of the font, but it is related with it by the factor GetDeviceCaps(hDC, LOGPIXELSY)/72 that is system-dependant. On my Laptop the factor is 1.33 meaning that I have to specify nHeight=27 (let's forget for the minus sign) to obtain a 20-point sized font (and nHeight=21 to obtain the 16-point one). On your system maybe the difference between the requested font heights it's so small that the same font is choosen. Hope that helps. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                G Offline
                G Offline
                garfield185
                wrote on last edited by
                #8

                Excuse my no-knowledge... No it says that "hDC" is not declared... What data does the function GetDeviceCaps need? Moreover, I guess LOGPIXELSY is a constant that I must define... :omg:

                Time to come clean... Vive y deja vivir / Live and let live Javier

                C D 2 Replies Last reply
                0
                • G garfield185

                  Excuse my no-knowledge... No it says that "hDC" is not declared... What data does the function GetDeviceCaps need? Moreover, I guess LOGPIXELSY is a constant that I must define... :omg:

                  Time to come clean... Vive y deja vivir / Live and let live Javier

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #9

                  hDC must be a valid handle of a device context (HDC). Using MFC you can pass the HDC of the current window (provided the code belongs to a window class)

                  nHeight = -MulDiv(20, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);

                  you can also use the screen HDC for the pourpose:

                  nHeight = -MulDiv(20, GetDeviceCaps(::GetDC(NULL), LOGPIXELSY), 72);

                  garfield185 wrote:

                  I guess LOGPIXELSY is a constant that I must define

                  No you must NOT define it, since it is already defined inside windows.h header file. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  G 1 Reply Last reply
                  0
                  • C CPallini

                    hDC must be a valid handle of a device context (HDC). Using MFC you can pass the HDC of the current window (provided the code belongs to a window class)

                    nHeight = -MulDiv(20, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);

                    you can also use the screen HDC for the pourpose:

                    nHeight = -MulDiv(20, GetDeviceCaps(::GetDC(NULL), LOGPIXELSY), 72);

                    garfield185 wrote:

                    I guess LOGPIXELSY is a constant that I must define

                    No you must NOT define it, since it is already defined inside windows.h header file. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                    G Offline
                    G Offline
                    garfield185
                    wrote on last edited by
                    #10

                    Fine. No errors. Anyway the size does no change. Am I missing something? Here is all the written code. ************************ CFont m_fuente; LOGFONT lf; m_fuente.CreateStockObject(DEFAULT_GUI_FONT); m_fuente.GetLogFont(&lf); strcpy(lf.lfFaceName,"MS Sans Serif"); lf.lfHeight = -MulDiv(50, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72); lf.lfWeight =700; m_fuente.DeleteObject(); m_fuente.CreateFontIndirect(&lf); m_label.SetFont(&m_fuente); ****************************** Should it be like this?

                    Time to come clean... Vive y deja vivir / Live and let live Javier

                    C 1 Reply Last reply
                    0
                    • G garfield185

                      Excuse my no-knowledge... No it says that "hDC" is not declared... What data does the function GetDeviceCaps need? Moreover, I guess LOGPIXELSY is a constant that I must define... :omg:

                      Time to come clean... Vive y deja vivir / Live and let live Javier

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

                      garfield185 wrote:

                      What data does the function GetDeviceCaps need?

                      See here.

                      garfield185 wrote:

                      Moreover, I guess LOGPIXELSY is a constant that I must define...

                      No.


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      G 1 Reply Last reply
                      0
                      • D David Crow

                        garfield185 wrote:

                        What data does the function GetDeviceCaps need?

                        See here.

                        garfield185 wrote:

                        Moreover, I guess LOGPIXELSY is a constant that I must define...

                        No.


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        G Offline
                        G Offline
                        garfield185
                        wrote on last edited by
                        #12

                        Pallini told me about that. I get no errors. What I say is that the code I wrote on the post before does not change the size of the text...

                        Time to come clean... Vive y deja vivir / Live and let live Javier

                        1 Reply Last reply
                        0
                        • G garfield185

                          Fine. No errors. Anyway the size does no change. Am I missing something? Here is all the written code. ************************ CFont m_fuente; LOGFONT lf; m_fuente.CreateStockObject(DEFAULT_GUI_FONT); m_fuente.GetLogFont(&lf); strcpy(lf.lfFaceName,"MS Sans Serif"); lf.lfHeight = -MulDiv(50, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72); lf.lfWeight =700; m_fuente.DeleteObject(); m_fuente.CreateFontIndirect(&lf); m_label.SetFont(&m_fuente); ****************************** Should it be like this?

                          Time to come clean... Vive y deja vivir / Live and let live Javier

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #13

                          The only problem I saw in your code was the declaration of

                          CFont m_fuente;

                          since m_fuente has to be a member of the Window class (otherwise it will be destroyed...) I declared CFont as a member of my CDialog class and your code is working fine (I see a label with very, very big characters :-D). :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                          G 1 Reply Last reply
                          0
                          • C CPallini

                            The only problem I saw in your code was the declaration of

                            CFont m_fuente;

                            since m_fuente has to be a member of the Window class (otherwise it will be destroyed...) I declared CFont as a member of my CDialog class and your code is working fine (I see a label with very, very big characters :-D). :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                            G Offline
                            G Offline
                            garfield185
                            wrote on last edited by
                            #14

                            YEAH!!!! You are the best!!!! Thanks a lot!! I was creatin m_fuente on each function. I didn´t know it had to be a member of the class... Thank you very much!!

                            Time to come clean... Vive y deja vivir / Live and let live Javier

                            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