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. Checkbox / How do I get the width/height of it?

Checkbox / How do I get the width/height of it?

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

    I have a button with BS_CHECKBOX style and text. I want to center the checkbox with my dialog so I need to get the "real" width of the control (Checkbox + gap + text = full width). I know how to get the width of the text part but how do I get the width and height of the checkbox itself? I looked at GetSystemMetrics and SystemParametersInfo but didn't find any. Any ideas where to look at? The width of the gap between is also needed. I need a plain WinAPI solution that works on all versions of Windows (not XP only). Thanks in advance, Sebastian ------------------------------------------- My website: http://www.hartwork.org -- modified at 1:03 Sunday 5th March, 2006

    S J 2 Replies Last reply
    0
    • S Sebastian Pipping

      I have a button with BS_CHECKBOX style and text. I want to center the checkbox with my dialog so I need to get the "real" width of the control (Checkbox + gap + text = full width). I know how to get the width of the text part but how do I get the width and height of the checkbox itself? I looked at GetSystemMetrics and SystemParametersInfo but didn't find any. Any ideas where to look at? The width of the gap between is also needed. I need a plain WinAPI solution that works on all versions of Windows (not XP only). Thanks in advance, Sebastian ------------------------------------------- My website: http://www.hartwork.org -- modified at 1:03 Sunday 5th March, 2006

      S Offline
      S Offline
      Sebastian Pipping
      wrote on last edited by
      #2

      More searching taught me that SM_CXMENUCHECK and SM_CXMENUSIZE seem to be what I was looking for. So my new problem is how do I tellGetTextExtentPoint32 to work with the new font and not the default one. Here's my code:

      const TCHAR * const szNeverAgain = TEXT( "Do not ask again" );

      SendMessage( hNeverAgain, WM_SETFONT, ( WPARAM )GetStockObject( DEFAULT_GUI_FONT ), ( LPARAM )TRUE );

      const HDC hdc = GetDC( hNeverAgain );
      SIZE size;
      GetTextExtentPoint32( hdc, szNeverAgain, _tcslen( szNeverAgain ), &size );
      ReleaseDC( hNeverAgain, hdc );

      size.cx is always the width for the old font. I need the new one - any ideas? ------------------------------------------- My website: http://www.hartwork.org

      S 1 Reply Last reply
      0
      • S Sebastian Pipping

        More searching taught me that SM_CXMENUCHECK and SM_CXMENUSIZE seem to be what I was looking for. So my new problem is how do I tellGetTextExtentPoint32 to work with the new font and not the default one. Here's my code:

        const TCHAR * const szNeverAgain = TEXT( "Do not ask again" );

        SendMessage( hNeverAgain, WM_SETFONT, ( WPARAM )GetStockObject( DEFAULT_GUI_FONT ), ( LPARAM )TRUE );

        const HDC hdc = GetDC( hNeverAgain );
        SIZE size;
        GetTextExtentPoint32( hdc, szNeverAgain, _tcslen( szNeverAgain ), &size );
        ReleaseDC( hNeverAgain, hdc );

        size.cx is always the width for the old font. I need the new one - any ideas? ------------------------------------------- My website: http://www.hartwork.org

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Select a font into the DC before calling GetTextExtentPoint32. e.g.

        const TCHAR * const szNeverAgain = TEXT( "Do not ask again" );
        
        SendMessage( hNeverAgain, WM_SETFONT, ( WPARAM )GetStockObject( DEFAULT_GUI_FONT ), ( LPARAM )TRUE );
        
        HDC hdc = GetDC( hNeverAgain );
        HFONT hOldFont = (HFONT)SelectObject(hdc, hYourFont);
        SIZE size;
        GetTextExtentPoint32( hdc, szNeverAgain, _tcslen( szNeverAgain ), &size );
        SelectObject(hdc, hOldFont);
        ReleaseDC( hNeverAgain, hdc );
        

        Steve

        S 2 Replies Last reply
        0
        • S Stephen Hewitt

          Select a font into the DC before calling GetTextExtentPoint32. e.g.

          const TCHAR * const szNeverAgain = TEXT( "Do not ask again" );
          
          SendMessage( hNeverAgain, WM_SETFONT, ( WPARAM )GetStockObject( DEFAULT_GUI_FONT ), ( LPARAM )TRUE );
          
          HDC hdc = GetDC( hNeverAgain );
          HFONT hOldFont = (HFONT)SelectObject(hdc, hYourFont);
          SIZE size;
          GetTextExtentPoint32( hdc, szNeverAgain, _tcslen( szNeverAgain ), &size );
          SelectObject(hdc, hOldFont);
          ReleaseDC( hNeverAgain, hdc );
          

          Steve

          S Offline
          S Offline
          Sebastian Pipping
          wrote on last edited by
          #4

          This is it! It works! I'm so happy - Thank you very much! Best regards, Sebastian ------------------------------------------- My website: http://www.hartwork.org

          1 Reply Last reply
          0
          • S Stephen Hewitt

            Select a font into the DC before calling GetTextExtentPoint32. e.g.

            const TCHAR * const szNeverAgain = TEXT( "Do not ask again" );
            
            SendMessage( hNeverAgain, WM_SETFONT, ( WPARAM )GetStockObject( DEFAULT_GUI_FONT ), ( LPARAM )TRUE );
            
            HDC hdc = GetDC( hNeverAgain );
            HFONT hOldFont = (HFONT)SelectObject(hdc, hYourFont);
            SIZE size;
            GetTextExtentPoint32( hdc, szNeverAgain, _tcslen( szNeverAgain ), &size );
            SelectObject(hdc, hOldFont);
            ReleaseDC( hNeverAgain, hdc );
            

            Steve

            S Offline
            S Offline
            Sebastian Pipping
            wrote on last edited by
            #5

            Btw in case you are the author of SpeedLoad - good work! :-D ------------------------------------------- My website: http://www.hartwork.org

            S 1 Reply Last reply
            0
            • S Sebastian Pipping

              Btw in case you are the author of SpeedLoad - good work! :-D ------------------------------------------- My website: http://www.hartwork.org

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Yeah I'm to blame for SpeedLoad. Thanks for your kind words. Steve

              1 Reply Last reply
              0
              • S Sebastian Pipping

                I have a button with BS_CHECKBOX style and text. I want to center the checkbox with my dialog so I need to get the "real" width of the control (Checkbox + gap + text = full width). I know how to get the width of the text part but how do I get the width and height of the checkbox itself? I looked at GetSystemMetrics and SystemParametersInfo but didn't find any. Any ideas where to look at? The width of the gap between is also needed. I need a plain WinAPI solution that works on all versions of Windows (not XP only). Thanks in advance, Sebastian ------------------------------------------- My website: http://www.hartwork.org -- modified at 1:03 Sunday 5th March, 2006

                J Offline
                J Offline
                janisha
                wrote on last edited by
                #7

                Create a variable for the check box. And call GetWindowRect :omg: Mythili

                S 1 Reply Last reply
                0
                • J janisha

                  Create a variable for the check box. And call GetWindowRect :omg: Mythili

                  S Offline
                  S Offline
                  Sebastian Pipping
                  wrote on last edited by
                  #8

                  No. The problem is the width can be much more than the text in it actually needs. If I want to center the checkbox I need the width of what the user sees, not the width of the window rect. Best regards, Sebastian ------------------------------------------- My website: http://www.hartwork.org

                  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