Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing the size of a check box to exactly fit the text

832 views
Skip to first unread message

Mark Robinson

unread,
Jul 13, 2011, 10:39:16 AM7/13/11
to

Hopefully a simple question that I'm struggling with.

If I create a check box on a dialog using the dialog editor and give
it a label, the control is made the correct size to nicely fit the text.
if I later change the text in my program, using SetWindowText(), can I
resize the control to fit the new text in the same way?

Cheers

mark-r

David Lowndes

unread,
Jul 13, 2011, 11:36:48 AM7/13/11
to
>If I create a check box on a dialog using the dialog editor and give
>it a label, the control is made the correct size to nicely fit the text.
>if I later change the text in my program, using SetWindowText(), can I
>resize the control to fit the new text in the same way?

There's no easy way Mark.

You usually design the check box control so that it would take the
longest string you'd expect - probably in German too :)

If you do want to make it dynamic you'd have to determine the width
the text could be then use SetWindowPos to resize the control window.

Dave

Joseph M. Newcomer

unread,
Jul 13, 2011, 6:12:20 PM7/13/11
to
I used to think German was the definitive "longest possible string" language, but one of
my books on localization says that Icelandic is the language to use to determine the
maximum length. All you need is an Icelandic translator...

I've done this by not putting any text in the control at all, except for one space. Then,
when I go to SetWindowText, I first compute CDC::GetTextExtent (the MFC function, which is
implemented by the ::GetTextExtentPoint32 API). So I do the following

In OnInitDialog:

note that ctlrect is declared in the class

checkbox.GetWindowRect(&ctlrect);
ScreenToClient(&ctlrect);
// This is the size of the control with one space in it

CClientDC dc(&checkbox);
CSize sz = dc.GetTextExtent(...string value...);
checkbox.SetWindowPos(NULL, 0, 0, ctlrect.Width() + sz.cx, ctlrect.Height(),
SWP_NOMOVE | SWP_NOZORDER);
checkbox.SetWindowText(...string value...);

This seems to work pretty well. The space that I put in accounts for the borders, and
because the controls come out with different pixel renderings based on the default font,
display, display resolution, display driver, phase of the moon, and angular relationship
of Mars and Jupiter, this means that my code works on any system, independent of the user
configuration.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Jul 14, 2011, 12:47:45 AM7/14/11
to
Oops. See fix below...

On Wed, 13 Jul 2011 18:12:20 -0400, Joseph M. Newcomer <newc...@flounder.com> wrote:

>I used to think German was the definitive "longest possible string" language, but one of
>my books on localization says that Icelandic is the language to use to determine the
>maximum length. All you need is an Icelandic translator...
>
>I've done this by not putting any text in the control at all, except for one space. Then,
>when I go to SetWindowText, I first compute CDC::GetTextExtent (the MFC function, which is
>implemented by the ::GetTextExtentPoint32 API). So I do the following
>
>In OnInitDialog:
>
>note that ctlrect is declared in the class
>
>checkbox.GetWindowRect(&ctlrect);
>ScreenToClient(&ctlrect);
>// This is the size of the control with one space in it

****
Whenever I want to change the text, I do
[I forgot to put the "context switch" in]

Also note that if I want multiple instances of the control, I will subclass CButton, and
instead of putting the above code in OnInitDialog I put it in PreSubclassWindow, and add a
method "SetWindowText" of my subclass (a bit sloppy, but in an intelligently-defined
system, SetWindowText would have been a virtual method instead of being badly-defined as
non-virtual, as it currently is)
****

Mark Robinson

unread,
Jul 14, 2011, 6:10:02 AM7/14/11
to
On 13/07/2011 16:36, David Lowndes wrote:

> There's no easy way Mark.

I suspected as much when nothing obvious came out of the docs.
I'm going to give Joseph's method a try.

> You usually design the check box control so that it would take the
> longest string you'd expect

That's my normal approach, but this was a bit trickier. I've got
8 check boxes corresponding to 8 stations of a test machine, and
I want to lay them out in a horizontal line. 99% of the time,
they'll be labelled "1" to "8", but one client wanted them called
"Left 1"-"Left 4" and "Right 1"-"Right 4" to reflect the arrangement
in their lab, so I've allowed the option of giving stations a
friendly name of up to 8 characters. Always leaving enough space for
8 characters makes the GUI look awkward for the majority of cases
where there's only one.

> - probably in German too :)

Luckily, I've not had to do a Welsh GUI yet! I think the biggest
challenge was supporting both German and Chinese, especially since
when testing the Chinese GUI, I had no idea at all what the squiggles
meant! Portuguese offered some challenges too; I only know one word
of Portuguese, and that's "perpendicularidade", the best translation
we had of "squareness".

Cheers

mark-r

0 new messages