Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Myoga1012/c61584bf5a408af1129c to your computer and use it in GitHub Desktop.
Save Myoga1012/c61584bf5a408af1129c to your computer and use it in GitHub Desktop.
WPF(言語はC#とXAML)で俳句を出力するプログラムですよっ!

このGistに上がっているXAML、C#ファイルの概要

  • WPFHaiku 1.0 : Ver. 1.0, 俳句を縦書きで出力するプログラムです。
    • Haiku.xaml : デザイナ部
    • Haiku.cs : 分離コード
  • WPFHaiku 2.0 : Ver. 2.1, WPFHaiku 1.0に入力ボックスとのデータバインディング機能を搭載しています。
    • Haiku2.xaml : デザイナ部(分離コードは初期化のみ)
    • Haiku2Converter.cs : 【旧コード】入力ボックスの文字列をカンマで区切って、要素の並びを反転させたIEnumerable型のリストに変換するコンバーター
    • Haiku2Converter2.cs : 【新コード】string.JoinメソッドとReplaceメソッドを使って、Haiku2Converter.csのコードを最適化したコンバーターコンバータークラス
// Auther : Myoga Screw-bright
// Twitter : https://twitter.com/Myoga1012
using System.Text;
using System.Windows;
namespace WPFHaiku {
class Haiku {
public string[] Phrase { get; set; }
public Haiku() {
string s = "初桜,折しも今日は,よき日なり";
StringBuilder sb = new StringBuilder( s );
for( int i = sb.Length - 1; i >= 0; i-- )
if( sb[i] != ',' ) sb.Insert( i + 1, "\n" );
Phrase = sb.ToString().Split( ',' );
}
}
public partial class MainWindow : Window {
public MainWindow() { InitializeComponent(); }
}
}
// Haiku2.cs(Haiku.xamlの分離コード)
// Copyright (c) 2014-2015 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
<!--
Auther : Myoga Screw-bright
Twitter : https://twitter.com/Myoga1012
-->
<Window x:Class="WPFHaiku.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WPFHaiku"
Title="俳句" Height="450" Width="230" ResizeMode="NoResize">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
<Setter Property="FontFamily" Value="HGSGyoshotai"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</Window.Resources>
<Window.DataContext><loc:Haiku/></Window.DataContext>
<Border Background="DarkGreen">
<Grid Margin="10" Background="Ivory">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="2" Text="{Binding Phrase[0]}"/>
<TextBlock Grid.Column="1" Text="{Binding Phrase[1]}"/>
<TextBlock Grid.Column="0" Text="{Binding Phrase[2]}"/>
</Grid>
</Border>
</Window>
<!--
Haiku.xaml( For WPF )
Copyright (c) 2014-2015 Myoga-TN All Rights Reserved.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
-->
<!--
Auther : Myoga Screw-bright
Twitter : https://twitter.com/Myoga1012
-->
<!-- 分離コードは初期化のみです。 -->
<Window x:Class="WPFHaiku.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WPFHaiku"
Title="俳句" Height="500" Width="230" ResizeMode="NoResize">
<Window.Resources>
<!-- 俳句のテキストの設定です。 -->
<Style x:Key="HaikuText" TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
<Setter Property="FontFamily" Value="HGSGyoshotai"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<!-- 文字列を縦書きに変換するコンバーターです。 -->
<loc:HaikuConverter x:Key="HaikuConverter"/>
</Window.Resources>
<Grid Background="DarkGreen">
<!-- 上部を俳句、下部を入力ボックスとします。 -->
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<!-- 俳句を入力するテキストボックスです。 -->
<TextBox Grid.Row="1" x:Name="HaikuInput" Margin="5"/>
<!-- 俳句を表示するエリアです。 -->
<Border Grid.Row="0">
<Grid Margin="10" Background="Ivory">
<!--
HaikuConverterを使って、HaikuInputに入力した文字列をカンマで区切り、
要素の並びを反転した時のIEnumerable<string>型のリストに変換します。
これをItemsControlのItemsSourceにバインディングし、StackPanelを使って
TextBlockを横方向に並べます。
-->
<ItemsControl ItemsSource="{Binding Text, Converter={StaticResource HaikuConverter},
ElementName=HaikuInput, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource HaikuText}"
Margin="5" Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Border>
</Grid>
</Window>
<!--
Haiku2.xaml( For WPF )
Copyright (c) 2014-2015 Myoga-TN All Rights Reserved.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
-->
// 旧コード
using System;
using System.Windows.Data;
using System.Globalization;
using System.Text;
using System.Linq;
namespace WPFHaiku {
// 文字列をカンマで区切り、リストを反転するコンバーターです。
class HaikuConverter : IValueConverter {
// このプログラムではConvertを使用します。
public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
// StringBuilderのインスタンスを生成し、コンバーターソースの文字列で初期化します。
StringBuilder sb = new StringBuilder( ( string )value );
// カンマ以外の文字の後に改行記号を挿入します。
for( int i = sb.Length - 1; i >= 0; i-- )
if( sb[i] != ',' ) sb.Insert( i + 1, "\n" );
// カンマで分割し、リストを反転します。
return sb.ToString().Split( ',' ).Reverse();
}
// ConvertBackは使用しません。
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
return null;
}
}
}
// Haiku2Converter.cs(Haiku2.xaml用コンバータークラス)
// Copyright (c) 2014-2015 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
// Auther : Myoga Screw-bright
// Twitter : https://twitter.com/Myoga1012
using System;
using System.Windows.Data;
using System.Globalization;
using System.Linq;
namespace WPFHaiku {
// 文字列をカンマで区切り、リストを反転するコンバーターです。
class HaikuConverter : IValueConverter {
// このプログラムではConvertを使用します。
public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
// 文字列をchar型配列に一旦分割し、各文字を改行記号で区切った文字列を生成します。
// カンマの直後の改行記号を削除してからカンマで分割し、リストを反転します。
return string.Join( "\n", ( ( string )value ).ToCharArray() ).Replace( ",\n", "," ).Split( ',' ).Reverse();
}
// ConvertBackは使用しません。
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
return null;
}
}
}
// Haiku2Converter2.cs(Haiku2.xaml用コンバータークラス)
// Copyright (c) 2014-2015 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment