Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a custom WPF control like a cartoon bubble with constant corners

Tags:

I need to create a rectangle bubble with rounded corners with text inside, like a cartoon speech bubble. I need the bubble to expand horizontally and vertically depending on the size of the text it contain. I would like the speech arrow and the radius of the rounded corners to remain constant.

I could simply use a path to create my bubble, but I can't resize the bubble and keep the corners radius and arrow constant... it's the whole path that will be resized.

I'd appreciate that some one could point me in the right direction.

removed dead ImageShack link

Here is the final version of the cartoon bubble user-control. I've added a rectangle without a stroke to Jobi Joy's version to hide the end of path lines, instead of trying to make then appear flush with the rectangle.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <Rectangle Fill="#FF686868" Stroke="#FF000000" RadiusX="10" RadiusY="10"/>
    <Path Fill="#FF686868" Stretch="Fill" Stroke="#FF000000" HorizontalAlignment="Left" Margin="30,-5.597,0,-0.003" Width="25" Grid.Row="1" Data="M22.166642,154.45381 L29.999666,187.66699 40.791059,154.54395"/>                  
    <Rectangle Fill="#FF686868" RadiusX="10" RadiusY="10" Margin="1"/>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25" Text="Hello World" TextWrapping="Wrap"/>                       
</Grid>
like image 920
Frederic Avatar asked Dec 03 '08 14:12

Frederic


2 Answers

Use this XAML, You can create a PopUp or a ContentControl and can give this Grid as the control template on it to get a consistent look

<Grid x:Name="grid">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <Rectangle Fill="#FF686868" Stroke="#FF000000" RadiusX="10" RadiusY="10"/>
    <Path Fill="#FF686868" Stretch="Fill" Stroke="#FF000000" HorizontalAlignment="Left" Margin="30,-1.6,0,0" Width="25" Grid.Row="1" 
        Data="M22.166642,154.45381 L29.999666,187.66699 40.791059,154.54395"/>          
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25" Text="Hello World" TextWrapping="Wrap"/>           
</Grid>

removed dead ImageShack link

I have made a blog post on this

like image 129
Jobi Joy Avatar answered Sep 23 '22 09:09

Jobi Joy


The rounded corners can just be a Border with Corner Aliasing set.

The constant / speech arrow can be a path that is positioned in a grid along with the border.

Take a look at the control template for the GroupBox and see how the "Header" content is positioned to float over the Border of the group box.

You would do the same thing except you would position a Path at the bottom instead of the top.

like image 24
Bogdan Varlamov Avatar answered Sep 21 '22 09:09

Bogdan Varlamov