R.D.

The MSDN for CStatic shows a nice simple sample for getting a static text control on a window. However when I follow the sample, at least two issues immediately show up.

1.) What is the correct rectangle size

2.) The text displays with a bold font.

I tried calling pStatic->SetFont( GetFont() ); But the MSDN says the CFont may be temporary and should not be stored. The dynamically added static text displayed just like all other text added via the resource editor but I suspect this is not good code.

I created the CStatic and set its parent to be my dialog box. How do I get the CStatic to use the same CFont the parent window assigns to the static text objects constructed at dialog load/initialization time Get the logical font from the parent's font and use the create indirect function

If I must pass Create a real rectangle, what method do I call to calculate the rectangle Do I have to use GDI to draw the text using the calculate rectangle flag to pass to the Create function



Re: Visual C++ General Creating a CStatic on a dialog

Ramkrishna Pawar

You can set whatever size you want, so it's upto you, all sizes are correct (dont count negative).

Have you set the dialog class pointer as parent while creating this static object






Re: Visual C++ General Creating a CStatic on a dialog

Sreedhar DVS

Try this

1.)For geting the rectangle.

CRect rect;

m_stat.GetWindowRect(&rect)

2.)For font related

CFont *m_pFont;

.................

............

LOGFONT lf;

memset(&lf, 0, sizeof(LOGFONT));

strcpy(lf.lfFaceName,_T("Tahoma"));

m_pFont = new CFont();

m_pFont->CreateFontIndirect(&lf);

m_static.SetFont(m_pFont,true); //CStatic control variable

Cheers,

Sreedhar Durgavarjula






Re: Visual C++ General Creating a CStatic on a dialog

R.D.

This I have tried. I cannot set whatever size I want. If I set the size too small, the text is clipped. For the parent, when I called Create, I passed in the dialog as the parent. Picking a size that works has the peril that the text is not clipped in one language but may be clipped in another language like German where the translation tends to be a litttlebitlargerthanenglish as German tends to have verylongwords.





Re: Visual C++ General Creating a CStatic on a dialog

R.D.

I cannot get the rectangle until the CStatic is created. The Create method insists on a rectangle that definitely bounds the window for the control. Too small and the text is clipped. After calling Create, if I call GetWindowRect, and then ScreenToClient, I will find that the result is the same as what I passed into Create to start with. BTW, Create wants the rectangle in client coordinates.

I did somethong close to what you indicate to create a font. The difference is I called pParent->GetFont to get a CFont and then GetLogFont to obtain the parent's logical font and then called CreateFontIndirect. I did that because I want the text to appear the same as other text on the dialog. Of course, now I also have to manage the new CFon'ts lifetime. I did try simply setting the static's font to the font returned from the parent's GetFont call but GetFont can return a temp object and even if it does not, I would worry about lifetime management.

For the rectangle, either I have to provide a very large rectangle that can contain the text (including when it is translated to other languages) but does not overlap other items in the window (z-order will cause occlusion of one or the other overlapping item), or to be absolutely safe, I have to call DrawTextExW passing in the DT_CALCRECT. I use the "W" version of draw text because of multiple language support.

I was hoping that since I am dealing with a dialog box there would be an easier way to accomplish this but so far I have not found one. After all, a dialog box already assigns its own font to all of its items so I should not have to create yet another font if I want the same look for dynamically added static text objects that all other text on the dialog have.

A dialog ought to also have a method that calculates the rectangle for me or more useful yet would be a static (control) style like SS_LEFT (e.g., SS_CALCRECT) that designates that the Create method's input rectangle's top and left params are to be used for initial placement and that the dialog should calculate the actual bounds of the rectangle for the caller. After calling Create, if I really wanted the actual rectangle, I could then call GetWindowRect on the static. It would also be nice to have something like SS_USEPARENTFONT to avoid the font issue too.





Re: Visual C++ General Creating a CStatic on a dialog

Simple Samples

First, note that for creating a CStatic in the manner you are trying to do you will get better answers in the MFC UI newsgroup. The question of whether this is off-topic for this forum does not matter; the important thing is that you are just more likely to get the best answer for this question in the newsgroup.

Second, let's clarify one assumption. We assume that you need to create the CStatic dynamically, but that might not be necessary. The problem is likely to be significantly simpler if you don't need to create it dynamically. What requirement is there that the control must be created dynamically




Re: Visual C++ General Creating a CStatic on a dialog

R.D.

I am converting part of our UI to use a task panel. Part of the change is to place tooltips beside certain bitmap buttons without having to modify many hundreds of dialog boxes in the resource editor.

I may eventually add code to rewrite the dialog resources out (I saw some windows apis that purport to write a resource back to the dll).

If I can find more information about how dialog box templates look in memory and how to manipulate them it may be possible to modify the template directly. Unfortunately when I searched the MSDN for "dialog box template", I get way too many hits as "dialog" and "box" appear in an enormous number of pages. I guess I should have googled instead as the third hit from google is from the msdn and the hit is titled "About Dialog boxes" with the first sentence being "The dialog box template is a binary ...". Still the article only mentions some info about the header and other info contained in the binary data. I'll have to dig more to see how to read and modify the data.

But for now, I am modifying the dialogs on the fly. Adding a static control is not too hard. I was just hoping I missed something that would allow me to get the controls to directly use the dialog's font and to make rectangle determination easier (I use DrawText to calc the rectangle and also have each control added use the same font I derive from the dialog's font so I am done with the part of the code that adds the strings dynamically).





Re: Visual C++ General Creating a CStatic on a dialog

Simple Samples

I still don't understand the need to create the static control dynamically. Is it just because there is such an abundance of dialog templates that it is easier to add the static control dynamically

One possibility would be to use one of the following to draw the text directly without a control. TextOut, ExtTextOut, TabbedTextOut or DrawText.


I think you can use GetTextExtent to determine the width of text. Note that eventhough the documentation does not say so, I am nearly sure it does only one line at a time. The height I think is for just one line.

You can use AdjustWindowRect to set the size of the static control perfectly if you know the size of the text to be put into it. There is an MFC equivalent but I forget what it is. Probably it is better to use the SDK version.






Re: Visual C++ General Creating a CStatic on a dialog

R.D.

As I said, we have hundreds and hundreds of dialogs. We don't want to change all the resources manually. They happen to share a base class we have implemented, which makes it possible to influence the look&feel across all the UI.

I originally did draw the text when I drew my controls (mostly owner draw or our own controls) but we decided to add the static controls instead. This was done for a couple of reasons. One was so that subclasses can make use of the standard OnCtlColor to influence how the text is drawn in particular cases. Another is to allow for being able to rewrite the modified dialog boxes back out to a resource at some point in the future. There are other reasons too such as being able to allow clicks on the static controls if that is needed in some particular case, not worrying about layout calculations and who knows what else might arise if I simply did the drawing myself. For example, we dock these dialogs in a task panel that has scrolling capabilities and when the scroll bars appears depends on when controls start to be clipped as the user resizes the panel. So drawing the text directly is not the only code I have to modify if I don't create real controls on the dialogs. It is simply less code all around to create a real control up front. Also, as noted in my previous posts, I use DrawText to get the extents and I then pass the results to the Create function. So there is no need for AdjustWindowRect.