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. How to get the Font width ???

How to get the Font width ???

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 5 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.
  • H Offline
    H Offline
    HumanOsc
    wrote on last edited by
    #1

    Hello... I want to get the Font width from a font handle and the ::GetObject(...) function to retrieve the font data (in a LOGFONT structure)... But often the lfWidth member is zero or very small... :doh: How to get the correct font width ???

    S A M 3 Replies Last reply
    0
    • H HumanOsc

      Hello... I want to get the Font width from a font handle and the ::GetObject(...) function to retrieve the font data (in a LOGFONT structure)... But often the lfWidth member is zero or very small... :doh: How to get the correct font width ???

      A Offline
      A Offline
      alias0018
      wrote on last edited by
      #2

      //You can get it like this: double width; RECT Rect; width=Rect.right;//This is the width of your window.

      S 1 Reply Last reply
      0
      • H HumanOsc

        Hello... I want to get the Font width from a font handle and the ::GetObject(...) function to retrieve the font data (in a LOGFONT structure)... But often the lfWidth member is zero or very small... :doh: How to get the correct font width ???

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        That will give you the average width (if available) of a character (in logical units). That may not be what you want, unless it's a fixed pitch font. Better (but slower) is to select the font into a device context, and use the GetTextExtentPoint32 functions for specific strings. Alternatively, if you really want the average width, you can do this using GetCharABCWidths and a bit of math. Steve S Developer for hire!

        H 1 Reply Last reply
        0
        • A alias0018

          //You can get it like this: double width; RECT Rect; width=Rect.right;//This is the width of your window.

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

          which has to do with fonts how, exactly ? :) Steve S Developer for hire!

          1 Reply Last reply
          0
          • H HumanOsc

            Hello... I want to get the Font width from a font handle and the ::GetObject(...) function to retrieve the font data (in a LOGFONT structure)... But often the lfWidth member is zero or very small... :doh: How to get the correct font width ???

            M Offline
            M Offline
            mark novak
            wrote on last edited by
            #5

            I believe what you want is something like GetTextMetrics(), which will return an average width in units based on the DC your using. A LOGFONT's lfWidth returns an average width in logical units. Also you can use DrawText() with DT_CALCRECT to get accurate dimensions on a given peice of text on a given DC.

            H A 2 Replies Last reply
            0
            • S Steve S

              That will give you the average width (if available) of a character (in logical units). That may not be what you want, unless it's a fixed pitch font. Better (but slower) is to select the font into a device context, and use the GetTextExtentPoint32 functions for specific strings. Alternatively, if you really want the average width, you can do this using GetCharABCWidths and a bit of math. Steve S Developer for hire!

              H Offline
              H Offline
              HumanOsc
              wrote on last edited by
              #6

              Thanx for your very fast reply... I hoped that i mustn't select it to dc and can calculate the max font width or better max letter width from the LOGFONT structure or other else... But also big thanx...

              1 Reply Last reply
              0
              • M mark novak

                I believe what you want is something like GetTextMetrics(), which will return an average width in units based on the DC your using. A LOGFONT's lfWidth returns an average width in logical units. Also you can use DrawText() with DT_CALCRECT to get accurate dimensions on a given peice of text on a given DC.

                H Offline
                H Offline
                HumanOsc
                wrote on last edited by
                #7

                Yes and No... TEXTMETRIC struct defines the right member that i need (tmAveCharWidth), but i need always a dc to get them... So i hoped it exists any other way to calculate them from LOGFONT structure or other else... But always big THX for your reply... :-D

                M 1 Reply Last reply
                0
                • H HumanOsc

                  Yes and No... TEXTMETRIC struct defines the right member that i need (tmAveCharWidth), but i need always a dc to get them... So i hoped it exists any other way to calculate them from LOGFONT structure or other else... But always big THX for your reply... :-D

                  M Offline
                  M Offline
                  mark novak
                  wrote on last edited by
                  #8

                  Without a DC a real width doesn't exist until it's associated with a DC. If your printing it to a high dpi printer your width will be different then on a low dpi monitor. If you want something more "logical" use the font size which is calculated from LOGFONT's height.

                  H 1 Reply Last reply
                  0
                  • M mark novak

                    Without a DC a real width doesn't exist until it's associated with a DC. If your printing it to a high dpi printer your width will be different then on a low dpi monitor. If you want something more "logical" use the font size which is calculated from LOGFONT's height.

                    H Offline
                    H Offline
                    HumanOsc
                    wrote on last edited by
                    #9

                    Hello... Mhhh... Mark Petrik Sosa wrote: Without a DC a real width doesn't exist until it's associated with a DC. I don't understand ? :doh: I think the LOGFONT height it's the height of the font. The size is fixed and independent from the dc... What's the different about the width... When i create a font with a height of 20 pixels then is height from the font on a screen dc different as from the printer dc, but i think it's difference about the resolution and the count of pixel per inch... Can you explain your idea? Best regards...

                    M 1 Reply Last reply
                    0
                    • H HumanOsc

                      Hello... Mhhh... Mark Petrik Sosa wrote: Without a DC a real width doesn't exist until it's associated with a DC. I don't understand ? :doh: I think the LOGFONT height it's the height of the font. The size is fixed and independent from the dc... What's the different about the width... When i create a font with a height of 20 pixels then is height from the font on a screen dc different as from the printer dc, but i think it's difference about the resolution and the count of pixel per inch... Can you explain your idea? Best regards...

                      M Offline
                      M Offline
                      mark novak
                      wrote on last edited by
                      #10

                      Good question. From the MSDN on LOGFONT: If lfWidth is zero, the aspect ratio of the device is matched against the digitization aspect ratio of the available fonts to find the closest match, determined by the absolute value of the difference. The height of a font is in logical units. Which is why a font at size 8 can look huge on a computer with large fonts set. The Windows GDI was designed to make as little assumptions as possible about what it's drawing on. On a printer 20 pixels is very tiny because it's DPI is so high. There are even monitors that aren't at the normal 96 dpi, so these would not look right if it was embedded with a nonlogical number. Some devices don't even use pixels but other measurements, like twips, so until the DC is set with this information you can't get the width on some fonts. Why can't you get the font's average width logical units? I'm not sure, perhaps it has to do mostly with the font, whether an accurate font to that size has been selected or not, or whether a font author must set this information in the header. Maybe windows is just reluctant to provide such an inaccurate approximation, I don't know. In my opinion it doesn't seem like a big deal (apart from being interesting) since it's a logical value anyways. If you were to use it, you'd have to make all the calculations that GetTextMetrics makes for it to work across all DCs. I can't see where you'd use it without a DC. I hope this makes things somewhat clearer.

                      H 1 Reply Last reply
                      0
                      • M mark novak

                        Good question. From the MSDN on LOGFONT: If lfWidth is zero, the aspect ratio of the device is matched against the digitization aspect ratio of the available fonts to find the closest match, determined by the absolute value of the difference. The height of a font is in logical units. Which is why a font at size 8 can look huge on a computer with large fonts set. The Windows GDI was designed to make as little assumptions as possible about what it's drawing on. On a printer 20 pixels is very tiny because it's DPI is so high. There are even monitors that aren't at the normal 96 dpi, so these would not look right if it was embedded with a nonlogical number. Some devices don't even use pixels but other measurements, like twips, so until the DC is set with this information you can't get the width on some fonts. Why can't you get the font's average width logical units? I'm not sure, perhaps it has to do mostly with the font, whether an accurate font to that size has been selected or not, or whether a font author must set this information in the header. Maybe windows is just reluctant to provide such an inaccurate approximation, I don't know. In my opinion it doesn't seem like a big deal (apart from being interesting) since it's a logical value anyways. If you were to use it, you'd have to make all the calculations that GetTextMetrics makes for it to work across all DCs. I can't see where you'd use it without a DC. I hope this makes things somewhat clearer.

                        H Offline
                        H Offline
                        HumanOsc
                        wrote on last edited by
                        #11

                        Hello Mark... Thx for your comments... It helps me a lot to understand the difference about logical untits and the real size on dc'S... Mark Petrik Sosa wrote: I can't see where you'd use it without a DC I need the width of the font in some calculations of my new app... But before the window is drawing... At this point the explicit creation of a dc handle to retrieve only the width of the font looks very involved... Best regards... :)

                        1 Reply Last reply
                        0
                        • M mark novak

                          I believe what you want is something like GetTextMetrics(), which will return an average width in units based on the DC your using. A LOGFONT's lfWidth returns an average width in logical units. Also you can use DrawText() with DT_CALCRECT to get accurate dimensions on a given peice of text on a given DC.

                          A Offline
                          A Offline
                          Anonymous
                          wrote on last edited by
                          #12

                          I've used DrawText() with DT_CALCRECT, when the string is long enough, then the speed will be very slow.

                          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