|
-
September 25th, 2004, 06:07 AM
#1
DrawText Problem Figuring Out the Size of a Radio Button
I've been messing around with this for several days. It appears that DrawText with the DT_CALCRECT does not calculate the rectangle right for a Radio or Checkbox button.
I created a simple project and placed a Radio and Checkbox button on a blank form view and did some experimenting. If I place the button with no text in it at all, do a GetWindowRect on the control, read the string from the control and do DrawText with DT_CALCRECT, the rectangle returned will be exactly the same size as it was before calling DrawText. I suppose that's because the function failed. In any case, the return is zero.
If I create a string to to go in the control, or have default text I read from the control, the rectangle gets resized by DrawText, but the new size is only big enough for the text, it is not big enough for the text and the check box or radio circle of the control.
How do I either calculate the size needed for the entire control with the check or radio button with the text? In my application, I am changing the text in radio buttons frequently. Sometimes the text is only a partial line and other times it is multiple lines, so I need to resize the control dynamically. To do that, I need an accurate calculation of the size!
-
September 25th, 2004, 06:20 AM
#2
Re: DrawText Problem Figuring Out the Size of a Radio Button
 Originally Posted by wdolson
I suppose that's because the function failed. In any case, the return is zero.
Yes, a return value of 0 means that DrawText() failed. Have you called GetLastError() to find out why it failed?
-
September 25th, 2004, 06:37 AM
#3
Re: DrawText Problem Figuring Out the Size of a Radio Button
GetLastError returned 0. I assumed that DrawText returned 0 simply because the string I gave it was a null string.
I was just doing this as an experiment to flesh out DrawText's behavior. What I really need to know is how to get the correct size of the radio and check box control with the text in it. DrawText is only telling me the size of the text, not the whole control.
-
September 25th, 2004, 07:09 AM
#4
Re: DrawText Problem Figuring Out the Size of a Radio Button
 Originally Posted by wdolson
What I really need to know is how to get the correct size of the radio and check box control with the text in it. DrawText is only telling me the size of the text, not the whole control.
I fear that there is no "official" way for finding out the size of those controls - and what's worse, you can't even tackle that with an empirical approach, since the actual size of the controls varies depending on the OS version, the XP personality, the system's default font size etc. What's more, the size of a checkbox control is more than just the sum of the sizes of the checkbox and the text, since the control might deliberately add any margins / spacing when drawing the control.
What you can do, however, is a dirty little workaround. Just create a (hidden) checkbox without any text (or maybe you will need a single character) and get its size with GetWindowRect(). Then you do your usual DrawText() thing with the actual string, and add the width you get to the previously obtained rectangle you got for the empty control. I know it's not very elegant - just give it a try.
-
September 25th, 2004, 08:08 AM
#5
Re: DrawText Problem Figuring Out the Size of a Radio Button
Resizing controls can be tricky. The following may be helpful.
Sometimes the GetSystemMetrics function can help. For instance the height of a combo box is the same as the arrows on a scroll bar (::GetSystemMetrics(SM_CXVSCROLL)). I think in the past I found that the check in a check box is the same as the checkmark on a menu. The menu check mark size can be found with ::GetSystemMetrics(SM_CXMENUCHECK) and ::GetSystemMetrics(SM_CYMENUCHECK). You would then need to use ::GetSystemMetrics(SM_CXEDGE) and ::GetSystemMetrics(SM_CYEDGE) to get the border edge sizes and add them to the checkmark size to get the overall size. As for the radio button size....I don't know.
-
September 26th, 2004, 05:15 PM
#6
Re: DrawText Problem Figuring Out the Size of a Radio Button
 Originally Posted by gstercken
I fear that there is no "official" way for finding out the size of those controls - and what's worse, you can't even tackle that with an empirical approach, since the actual size of the controls varies depending on the OS version, the XP personality, the system's default font size etc. What's more, the size of a checkbox control is more than just the sum of the sizes of the checkbox and the text, since the control might deliberately add any margins / spacing when drawing the control.
Yes, I thought of much of that already.
 Originally Posted by gstercken
What you can do, however, is a dirty little workaround.  Just create a (hidden) checkbox without any text (or maybe you will need a single character) and get its size with GetWindowRect(). Then you do your usual DrawText() thing with the actual string, and add the width you get to the previously obtained rectangle you got for the empty control. I know it's not very elegant - just give it a try.
That was the only idea I had, but I thought it was way too much of a kludge to even mention it. I finally got the time to try it and it does work.
The more I get into Windows programming the more things I find that are done ugly, or you need to come up with a work around like this. I was sure there had to be some kind of way to get the right size for the entire control. After all, Visual Studio manages to figure out how big the control will be when you're in the Resource Editor.
I guess I was expecting too much of Microsoft. I have done a lot of creative work arounds in other OSs and even had to work with some custom OSs, but MFC was touted as this complete toolkit. Another fine product of the Sirius Cybernetics Corporation...
I don't have the time to get into it now, but I think a more elegant solution would be to inherit the CButton class and override SetWindowText so it resizes the control automatically. I think that would take a lot of digging around in the existing class to see how they do things though. I would think CButton would be a fairly complex class.
Thanks for the help.
-
September 26th, 2004, 05:18 PM
#7
Re: DrawText Problem Figuring Out the Size of a Radio Button
 Originally Posted by TDM
Resizing controls can be tricky. The following may be helpful.
Sometimes the GetSystemMetrics function can help. For instance the height of a combo box is the same as the arrows on a scroll bar (::GetSystemMetrics(SM_CXVSCROLL)). I think in the past I found that the check in a check box is the same as the checkmark on a menu. The menu check mark size can be found with ::GetSystemMetrics(SM_CXMENUCHECK) and ::GetSystemMetrics(SM_CYMENUCHECK). You would then need to use ::GetSystemMetrics(SM_CXEDGE) and ::GetSystemMetrics(SM_CYEDGE) to get the border edge sizes and add them to the checkmark size to get the overall size. As for the radio button size....I don't know.
This is quite the work around. And if it doesn't work for radio buttons, it's not all that helpful in the end. GetSystemMetrics may be useful to me somewhere else in the program, so it may be helpful in some other way.
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|