I'm wanting to dynamically resize a CButton to the width of the text within it. Is there either a built-in way to do this in MFC, or a way of calculating the pixel width of some specified text (so that I can use CWnd::SetWindowPos
)?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You can use CDC::GetTextExtent to calculate the width of text in a certain font. Use CWnd::GetDC to get the Device Context from the control displaying the text.
It's tedious. You need to use
CWnd::GetFont()
on the button to get the font it's using, and then use the standardGetTextText
on aCDC
object where you will have selected that font. It looks something likeIn addition to @demoncodemonkey's answer, you can call CDC::DrawText with the
DT_CALCRECT
flag. This way the text won't be drawn, but the CRect you pass to the function will have the width and height of the text to draw.This is especially useful if you want to draw text with line breaks (using the
DT_WORDBREAK
flag). You won't be able to do that usingCDC::GetTextExtent
.