Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Label, like this

HTML
<Label Background="AliceBlue" Name="txtTest" Content="This is a test " FontSize="48" FontWeight="Bold"/>


and some window properties is
HTML
Opacity=".9" AllowsTransparency="True" WindowStyle="None" Background="Yellow"


and into the code behind
C#
void window1Loaded(object sender, RoutedEventArgs e)
{

    FormattedText ft = new FormattedText(this.txtTest.Content.ToString(),
        System.Globalization.CultureInfo.CurrentCulture,
        System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight,
        new Typeface(this.txtTest.FontFamily, this.txtTest.FontStyle, this.txtTest.FontWeight, this.txtTest.FontStretch),
        this.txtTest.FontSize,
        this.txtTest.Foreground);

    this.Width = ft.Width;
    this.Height = ft.Height;
}

but window's width/height don't fit to the text's width/height completely!
How to fit completely?

thanks in advance!

HZ
Posted
Updated 4-Oct-11 7:47am
v2

You have to realize that the Width and Height properties of a window is the width and height of the entire window and not just the client area. So you'll need to get the height of the title bar and the with and height of the window borders, so you can calculate the correct size of the window.
Assuming you have not restyled your window, you can get those values like this
C#
var horizontalBorderHeight = SystemParameters.ResizeFrameHorizontalBorderHeight;
var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
var captionHeight = SystemParameters.CaptionHeight;
 
Share this answer
 
Comments
hzawary 4-Oct-11 14:05pm    
My 5, Yes it was that you said.
Sergey Alexandrovich Kryukov 4-Oct-11 15:16pm    
Good catch, quite correct, my 5. However, it may or may not be necessary for the case. In some cases this is important technique, but in many cases it can be done much simpler.
Please see my solution.
--SA
Simon Bang Terkildsen 4-Oct-11 16:52pm    
I actually started by thinking that the OP should use SizeToContent, why I decided not to mention it I don't know/remember :S
Simon Bang Terkildsen 4-Oct-11 16:55pm    
Ah now I remember it was because it contained a general proof for Fermat's last theorem, so I thought to myself; no one will is interested in this so I'll just leave it out :)
You use of FormattedText is a good thing, and Simon's fix to is is quite correct, I'll vote my 5.

However, I have just one note: you might need to think of some alternative. Let's see. What you do is really good if you want what I would call "Message box effect": 1) you want to have your text word-wrapped around end-of-lines, auto-formatted, 2) you want your text to get formatted into some neat bounding box, with reasonable and pleasant aspect ratio albeit not precise, depending on the string content. This way, you may actually create your own message box or something else.

In other cases, there is a simpler way. You can use System.Windows.Window.SizeToContent property having your windows and all its children to size according to content (in you case, ultimately to your string content) automatically, just using this property and appropriate layout.

See http://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent.aspx[^].

—SA
 
Share this answer
 
v3
Comments
hzawary 4-Oct-11 15:45pm    
My 5, I tested it, Very good!
But I want fit and cropping window to my text exactly!
In act, I want to have a (transparency) text without any background in the desktop space and moving it with mouse;)
Sergey Alexandrovich Kryukov 4-Oct-11 15:51pm    
OK, so what's the problem? We discussed different options which are good for different cases.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900