Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:33:55+00:00 2026-06-08T02:33:55+00:00

I want to adjust a Static control’s size to its content size, so I

  • 0

I want to adjust a Static control’s size to its content size, so I need to calculate the size of its text content first. I found a way to use GetTextExtentPoint32 to calculate the size, but I need to set the DC’s font to the same as the control’s font first. Is there a better way to do this? I’ve set the Static control’s font once, I think maybe I don’t need to set the DC’s font the second time.

What is the best way to calculate the size of a Static control’s text content? And is there a better way to autosize the Static control?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:33:58+00:00Added an answer on June 8, 2026 at 2:33 am

    It sounds to me like you’ve already figured out the correct way to do it. Call GetTextExtentPoint32 to figure out the ideal size of the control given the text that it contains, and then resizing the control to the calculated size.

    It’s a lot of work, but that’s what happens when you’re working with the raw Win32 API. You don’t have a handy wrapper library that abstracts all this for you in a Control.AutoSize() function. You could easily write your own function and re-use it, but the Win32 standard controls do not expose an “auto-size” API.

    As far as the font, you will definitely need to make sure that the device context is using the same font as the control, otherwise you’ll calculate the wrong size. But you don’t have to create a new device context, request a handle the static control’s font, and select that into your new DC. Instead, you can just use the static control’s DC using the GetDC function and passing in the handle to your static control window. Make sure that if you call GetDC, you always follow up with a call to ReleaseDC when you’re finished!

    However, do note some caveats of the GetTextExtentPoint32 function that may interfere with the accuracy of the size you calculate:

    • It ignores clipping.
    • It does not take into account new lines (\n) or carriage returns (\r\n) when computing the height.
    • It does not take into account prefix characters (those preceded in the string with ampersand) and used to denote keyboard mnemonics if your static control does not have the SS_NOPREFIX style.
    • It may not return an accurate result in light of the kerning that may be implemented automatically by some devices.

    (This is all mentioned in the linked documentation, but does anyone actually read that?)

    Perhaps an easier alternative is to draw the text the same way that the static control is already doing. Unless you have the SS_SIMPLE style set (which uses TextOut or ExtTextOut to draw text as an optimization), static controls draw their text by calling the DrawText function with the appropriate parameters, given the other control styles that are set (reference).

    You can do exactly the same thing, and add the DT_CALCRECT flag in your call to the DrawText function, which causes it to determine the width and height of the rectangle required to draw the specified text without actually drawing the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to adjust the size of my text area in action script based
I have a DIV I want to adjust its size regarding the window size.
when my form is shown, i want to adjust the height of certain control,
I want to adjust the width of DropDownList control in flex 4. I could
I use a QTextEdit for some inputs. But I want to adjust the height
I want to use FreeType in a c# project. I found this binding ,
I want to adjust zsh so that I can tab complete: myprog <tab> using
I am working on a blog at www.tosreport.com/scripting and might want to adjust where
Hey Simple little app for educational purposes, its a simple uitableview and I want
I want to adjust the colour levels of an image in python. I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.