2013年6月9日日曜日

画像の表示とStretch

WPFで画像の表示をしてみます
固定の画像を表示させる場合は、xamlに以下の記述をするだけです

<Image Source="C:\Users\hogehoge\Pictures\110929-193431.jpg">

Stretchプロパティにより表示領域に対する表示方法を指定することができます
None(左上) : そのままの画像の大きさ。表示領域が画像より小さいと一部のみの表示
Fill(右上) : 上下左右を表示領域にあわせて拡大縮小。縦横比も保持されない。
Uniform(左下) : 表示領域に縦横比を保持して表示。これが一番使いそう!?
UniformToFill(右下) : 縦横比を保持した最大の表示
といったところの様子



--- xamlのコード (ファイル名(パス)の指定はお好みの画像にて!)
<Window x:Class="Graphic.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="512" Width="1024">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="256" />
            <RowDefinition Height="256" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="512" />
            <ColumnDefinition Width="512" />
        </Grid.ColumnDefinitions>
        <Image Source="C:\Users\hogehoge\Pictures\110929-193431.jpg" Stretch="None" Grid.Column="0" Grid.Row="0"/>
        <Image Source="C:\Users\hogehoge\Pictures\110929-193431.jpg" Stretch="Fill" Grid.Column="1" Grid.Row="0"/>
        <Image Source="C:\Users\hogehoge\Pictures\110929-193431.jpg" Stretch="Uniform" Grid.Column="0" Grid.Row="1"/>
        <Image Source="C:\Users\hogehoge\Pictures\110929-193431.jpg" Stretch="UniformToFill" Grid.Column="1" Grid.Row="1"/>
    </Grid>
</Window>

2013年6月4日火曜日

TextBlockの文字を修飾する

文字を修飾する方法の良いサンプルがMSDNに掲載されていましたので真似してみます

ボタンの文字を"Run"で修飾してみます
- Runで文字の属性変更
- <Run TextDecorations="****">で修飾
の模様
日本語ではベースラインとアンダーラインの区別はわかりにくいですが、アルファベットを使う場合は効果的ですね

<Window x:Class="StringDecorate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Width="200" Height="150">
            <TextBlock>
                ふつーのTextBlock<LineBreak />
                <Run FontWeight="Bold" FontSize="13">フォントサイズ+Bold</Run><LineBreak />
                <Run TextDecorations="Underline">アンダーライン</Run><LineBreak />
                <Run TextDecorations="Strikethrough">打消し線</Run><LineBreak />
                <Run TextDecorations="Overline">オーバーライン</Run><LineBreak />
                <Run TextDecorations="Baseline">gベースライン</Run>
                <Run TextDecorations="Underline">gアンダーライン</Run>
            </TextBlock>
        </Button>
    </Grid>
</Window>


詳しくはMSDNのこちら参照
http://code.msdn.microsoft.com/windowsdesktop/XAML-WPF-165daa55