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 calculate width and height of different font size ,font style and no of characters ?

How to calculate width and height of different font size ,font style and no of characters ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
10 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
    shiv nand
    wrote on last edited by
    #1

    Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.

    A E 2 Replies Last reply
    0
    • S shiv nand

      Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      You can get the font information from CWnd member GetFont(http://msdn.microsoft.com/en-us/library/cb4schcy(VS.80).aspx) And GetLogFont of the CFont will help get the information about the font. http://msdn.microsoft.com/en-us/library/zhcs623h(v=VS.71).aspx

      Величие не Бога может быть недооценена.

      1 Reply Last reply
      0
      • S shiv nand

        Hi i want to crate a rectangle based on the font size, font style,and number of charactes.So How can i decide width and height of different font size,font style & number of characters.

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        This function could calculate the text dimensions :) :

        void GetTextSize(CSize& cResultSize, // Receiving size of the text rectangle
        CDC* pcDC, // Surface context
        CFont* pcFont, // Font for the context
        const CString& cszText) // Text to measure
        {
        if (pcDC && pcFont) {
        CGdiObject* pcOldFont = pcDC->SelectObject(pcFont);
        cResultSize = pcDC->GetTextExtent(cszText); // dimensions of the text
        // at the given surface
        // with the given font
        pcDC->SelectObject(pcOldFont);
        }
        }

        virtual void BeHappy() = 0;

        modified on Friday, April 2, 2010 2:12 AM

        S 1 Reply Last reply
        0
        • E Eugen Podsypalnikov

          This function could calculate the text dimensions :) :

          void GetTextSize(CSize& cResultSize, // Receiving size of the text rectangle
          CDC* pcDC, // Surface context
          CFont* pcFont, // Font for the context
          const CString& cszText) // Text to measure
          {
          if (pcDC && pcFont) {
          CGdiObject* pcOldFont = pcDC->SelectObject(pcFont);
          cResultSize = pcDC->GetTextExtent(cszText); // dimensions of the text
          // at the given surface
          // with the given font
          pcDC->SelectObject(pcOldFont);
          }
          }

          virtual void BeHappy() = 0;

          modified on Friday, April 2, 2010 2:12 AM

          S Offline
          S Offline
          shiv nand
          wrote on last edited by
          #4

          Please can you me an example.

          E 1 Reply Last reply
          0
          • S shiv nand

            Please can you me an example.

            E Offline
            E Offline
            Eugen Podsypalnikov
            wrote on last edited by
            #5

            It could be something like this :) :

            /*virtual*/ void CYourView::OnDraw(CDC* pDC)
            {
            if (pDC) {
            CString cszText(_T("Hello World !"));
            // Calculate the text rectangle from the start point(100, 100)
            // at this surface with the actual set font
            CRect cTextRect(CPoint(100, 100),
            pDC->GetTextExtent(cszText));
            pDC->FillSolidRect(cTextRect, RGB(0, 200, 0)); // Text background
            pDC->DrawText(cszText, cTextRect, DT_LEFT | DT_TOP);
            }
            }

            virtual void BeHappy() = 0;

            S 1 Reply Last reply
            0
            • E Eugen Podsypalnikov

              It could be something like this :) :

              /*virtual*/ void CYourView::OnDraw(CDC* pDC)
              {
              if (pDC) {
              CString cszText(_T("Hello World !"));
              // Calculate the text rectangle from the start point(100, 100)
              // at this surface with the actual set font
              CRect cTextRect(CPoint(100, 100),
              pDC->GetTextExtent(cszText));
              pDC->FillSolidRect(cTextRect, RGB(0, 200, 0)); // Text background
              pDC->DrawText(cszText, cTextRect, DT_LEFT | DT_TOP);
              }
              }

              virtual void BeHappy() = 0;

              S Offline
              S Offline
              shiv nand
              wrote on last edited by
              #6

              actually i want create a Button based on fontsize fontstyle ,no of characters and font type.I dont know what actul text or characters. only i have a options like. No of characters : 50 Font Size : 16 Font Style : Regular/Bold/Italic Font Type : Times New Roman or Arial or Courier How width and height can be decided for the above ? so i can create a Button by assigning width and height easily .

              E 1 Reply Last reply
              0
              • S shiv nand

                actually i want create a Button based on fontsize fontstyle ,no of characters and font type.I dont know what actul text or characters. only i have a options like. No of characters : 50 Font Size : 16 Font Style : Regular/Bold/Italic Font Type : Times New Roman or Arial or Courier How width and height can be decided for the above ? so i can create a Button by assigning width and height easily .

                E Offline
                E Offline
                Eugen Podsypalnikov
                wrote on last edited by
                #7

                Try it :) :

                #define GAP 20
                CSize GetButtonDimensions(int iCharCount,
                int iFontSize,
                const CString& cszFontName)
                {
                CSize cResultSize(0, 0);

                CFont cFont;
                if (cFont.CreatePointFont(iFontSize * 10, cszFontname)) {
                CDC cDC;
                if (cDC.CreateCompatibleDC(NULL)) {
                CGdiObject* pcOldFont = cDC.SelectObject(cFont);

                  TEXTMETRIC tm = {0};
                  cDC.GetTextMetrics(&tm);
                
                
                  cResultSize.cy = tm.tmHeight                    + 2 \* GAP;
                  cResultSize.cx = tm.tmMaxCharWidth \* iCharCount + 2 \* GAP;
                
                
                  cDC.SelectObject(pcOldFont);
                }
                cFont.DeleteObject();
                

                }

                return cResultSize;
                }

                virtual void BeHappy() = 0;

                S 1 Reply Last reply
                0
                • E Eugen Podsypalnikov

                  Try it :) :

                  #define GAP 20
                  CSize GetButtonDimensions(int iCharCount,
                  int iFontSize,
                  const CString& cszFontName)
                  {
                  CSize cResultSize(0, 0);

                  CFont cFont;
                  if (cFont.CreatePointFont(iFontSize * 10, cszFontname)) {
                  CDC cDC;
                  if (cDC.CreateCompatibleDC(NULL)) {
                  CGdiObject* pcOldFont = cDC.SelectObject(cFont);

                    TEXTMETRIC tm = {0};
                    cDC.GetTextMetrics(&tm);
                  
                  
                    cResultSize.cy = tm.tmHeight                    + 2 \* GAP;
                    cResultSize.cx = tm.tmMaxCharWidth \* iCharCount + 2 \* GAP;
                  
                  
                    cDC.SelectObject(pcOldFont);
                  }
                  cFont.DeleteObject();
                  

                  }

                  return cResultSize;
                  }

                  virtual void BeHappy() = 0;

                  S Offline
                  S Offline
                  shiv nand
                  wrote on last edited by
                  #8

                  i am getting error : [CGdiObject* pcOldFont = cDC.SelectObject(cFont);] 'initializing' : cannot convert from 'HGDIOBJ' to 'CGdiObject *'

                  E 1 Reply Last reply
                  0
                  • S shiv nand

                    i am getting error : [CGdiObject* pcOldFont = cDC.SelectObject(cFont);] 'initializing' : cannot convert from 'HGDIOBJ' to 'CGdiObject *'

                    E Offline
                    E Offline
                    Eugen Podsypalnikov
                    wrote on last edited by
                    #9

                    Sorry, ..cDC.SelectObject(&cFont); :)

                    virtual void BeHappy() = 0;

                    S 1 Reply Last reply
                    0
                    • E Eugen Podsypalnikov

                      Sorry, ..cDC.SelectObject(&cFont); :)

                      virtual void BeHappy() = 0;

                      S Offline
                      S Offline
                      shiv nand
                      wrote on last edited by
                      #10

                      Thank You Eugen Podsypalnikov i got.

                      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