DoboWiki
Top
> .NETプログラミング研究/86 をテンプレートにして作成
.NETプログラミング研究/86 をテンプレートにして作成
開始行:
#title(Visual Studio International Pack: Japanese Text Al...
#navi(.NETプログラミング研究)
#contents
*Visual Studio International Pack: Japanese Text Alignmen...
Japanese Text Alignment Library(日本語テキスト整列ライブ...
文字列を描画するメソッドと言えばGraphics.DrawStringメソッ...
**ダウンロードとインストール [#wd828c82]
Japanese Text Alignment Libraryをインストールする手順は、...
また、プロジェクトの参照に「JapaneseTextAlignment.dll」を...
**Utility.DrawJapaneseStringメソッド [#w77799f2]
はじめに断らせていただきますが、今号の内容も前号と同様に...
Japanese Text Alignment Libraryで文字列を描画するには、Ut...
文字列を均等割付で描画する時、文字列内のどこに隙間を入れ...
Japanese Text Alignment LibraryにはIAlignmentUnitInfoProv...
**整列スタイルの違い [#ie424e36]
Utility.DrawJapaneseStringメソッドのパラメータであるTextA...
|TextAlignmentStyle列挙体のメンバー|説明|h
|Left|文字列が左に配置されるように指定する。|
|Center|文字列が中央に配置されるように指定する。|
|Right|文字列が右に配置されるように指定する。|
|Justify|両端に間を空けないで文字列が均等割付で配置される...
|FullJustify|両端に間を空けて文字列が均等割付で配置される...
このような違い以外に、以下のような違いが確認できました。
-JustifyとFullJustifyでは改行文字で改行されませんが(スペ...
具体的にこれらにどのような違いがあるかを調べる例を以下に...
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをFullJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをLeftに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをRightに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをCenterに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをFullJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをLeftに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをRightに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをCenterに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//後始末
f.Dispose();
}
}}
このコードを実行した結果、以下のように表示されます。
&ref(JapaneseTextAlignment1.png);
上記の例では使いませんでしたが、TextAlignmentStyleInfo構...
**SimpleAlignmentUnitInfoProviderとSmartAlignmentUnitInfo...
SimpleAlignmentUnitInfoProviderは最も単純なIAlignmentUnit...
SmartAlignmentUnitInfoProviderでは、日本語の文字の間には...
-前の文字がCJK統合漢字。
-前の文字が '(' (U+0028)でなく、現在の文字が '('。
-前の文字が ')' (U+0029)で、現在のキャラクタが ')'ではな...
-前の文字がアルファベット (a-z and A-Z), 数字 (0-9) およ...
上記の例では、SimpleAlignmentUnitInfoProviderを使用してい...
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'SimpleAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'SmartAlignmentUnitInfoProviderの作成
Dim provider As Microsoft.International.JapaneseTextA...
New Microsoft.International.JapaneseTextAlignment...
'SmartAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, _
provider)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//SimpleAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//SmartAlignmentUnitInfoProviderの作成
Microsoft.International.JapaneseTextAlignment.IAlignm...
new Microsoft.International.JapaneseTextAlignment...
//SmartAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider);
//後始末
f.Dispose();
}
}}
このコードを実行した結果、以下のように表示されます。
&ref(JapaneseTextAlignment2.png);
このように、SimpleAlignmentUnitInfoProviderで描画したとき...
**IAlignmentUnitInfoProviderを実装して割り付け方を変更す...
IAlignmentUnitInfoProviderを実装したクラスを作成して、ど...
Utility.DrawJapaneseStringメソッドで文字列を均等割付で描...
はじめの割付け単位の分割は自動的に行われ、スペース文字(...
ここからがIAlignmentUnitInfoProviderの仕事です。まずIniti...
このように割付け単位の最後の文字まで1文字ずつIsNewAlignme...
"This is a pen."の例で言えば、次のようになります。
+Initializeが呼び出され、"T"がパラメータとして渡されます。
+IsNewAlignmentUnitが呼び出され、"h"が渡されます。
+IsNewAlignmentUnitが呼び出され、"i"が渡されます。
+IsNewAlignmentUnitが呼び出され、"s"が渡されます。
+ここで新しい割付け単位になるため、Initializeが呼び出され...
+IsNewAlignmentUnitが呼び出され、"s"が渡されます。
+次の割付け単位は"a"一文字なので、Initializeが呼び出され...
+Initializeが呼び出され、"p"が渡されます。
+IsNewAlignmentUnitが呼び出され、"e"が渡されます。
+IsNewAlignmentUnitが呼び出され、"n"が渡されます。
+IsNewAlignmentUnitが呼び出され、"."が渡されます。
少し分かりにくかったかもしれませんが、簡単に言えば、均等...
具体例を示します。以下に示すIAlignmentUnitInfoProviderを...
#code(vbnet){{
'IAlignmentUnitInfoProviderを実装したクラス
Public Class TestAlignmentUnitInfoProvider
Implements Microsoft.International.JapaneseTextAlignm...
'前の文字
Private previousString As String
Public Sub Initialize(ByVal s As String) _
Implements Microsoft.International.JapaneseTextAl...
IAlignmentUnitInfoProvider.Initialize
If String.IsNullOrEmpty(s) Then
Throw New ArgumentException("sがnullか空です...
End If
'文字を覚えておく
Me.previousString = s
End Sub
Public Function IsNewAlignmentUnit(ByVal s As String)...
Implements Microsoft.International.JapaneseTextAl...
IAlignmentUnitInfoProvider.IsNewAlignmentUnit
If String.IsNullOrEmpty(s) Then
Throw New ArgumentException("sがnullか空です...
End If
'英数字以外かを判断する
Dim reg As New System.Text.RegularExpressions.Reg...
"^[^a-zA-Z0-9]", _
System.Text.RegularExpressions.RegexOptions.C...
'前の文字と今の文字が英数字のときだけ間隔をあけな...
Dim ret As Boolean = reg.IsMatch(Me.previousStrin...
'前の文字を更新
Me.previousString = s
'結果を返す
Return ret
End Function
End Class
}}
#code(csharp){{
//IAlignmentUnitInfoProviderを実装したクラス
public class TestAlignmentUnitInfoProvider :
Microsoft.International.JapaneseTextAlignment.IAlignm...
{
//前の文字
private string previousString;
public void Initialize(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentException("sがnullか空です...
//文字を覚えておく
this.previousString = s;
}
public bool IsNewAlignmentUnit(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentException("sがnullか空です...
//英数字以外かを判断する
System.Text.RegularExpressions.Regex reg =
new System.Text.RegularExpressions.Regex(
"^[^a-zA-Z0-9]",
System.Text.RegularExpressions.RegexOptio...
//前の文字と今の文字が英数字のときだけ間隔をあけ...
bool ret = reg.IsMatch(this.previousString) || re...
//前の文字を更新
this.previousString = s;
//結果を返す
return ret;
}
}
}}
このクラスを使って文字列を均等割付で描画する例を示します。
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'TestAlignmentUnitInfoProviderの作成
Dim provider As New TestAlignmentUnitInfoProvider()
'TestAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//TestAlignmentUnitInfoProviderの作成
TestAlignmentUnitInfoProvider provider = new TestAlig...
//TestAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider);
//後始末
f.Dispose();
}
}}
結果は、以下の図のようになります。
&ref(JapaneseTextAlignment3.png);
**スペース文字の部分だけに隙間を入れる [#af06ea47]
スペース文字の部分だけに隙間が入るようにして均等割付で描...
これとは逆にIsNewAlignmentUnitメソッドで常にTrueを返すの...
**コメント [#y3b5a8d6]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2008-1...
終了行:
#title(Visual Studio International Pack: Japanese Text Al...
#navi(.NETプログラミング研究)
#contents
*Visual Studio International Pack: Japanese Text Alignmen...
Japanese Text Alignment Library(日本語テキスト整列ライブ...
文字列を描画するメソッドと言えばGraphics.DrawStringメソッ...
**ダウンロードとインストール [#wd828c82]
Japanese Text Alignment Libraryをインストールする手順は、...
また、プロジェクトの参照に「JapaneseTextAlignment.dll」を...
**Utility.DrawJapaneseStringメソッド [#w77799f2]
はじめに断らせていただきますが、今号の内容も前号と同様に...
Japanese Text Alignment Libraryで文字列を描画するには、Ut...
文字列を均等割付で描画する時、文字列内のどこに隙間を入れ...
Japanese Text Alignment LibraryにはIAlignmentUnitInfoProv...
**整列スタイルの違い [#ie424e36]
Utility.DrawJapaneseStringメソッドのパラメータであるTextA...
|TextAlignmentStyle列挙体のメンバー|説明|h
|Left|文字列が左に配置されるように指定する。|
|Center|文字列が中央に配置されるように指定する。|
|Right|文字列が右に配置されるように指定する。|
|Justify|両端に間を空けないで文字列が均等割付で配置される...
|FullJustify|両端に間を空けて文字列が均等割付で配置される...
このような違い以外に、以下のような違いが確認できました。
-JustifyとFullJustifyでは改行文字で改行されませんが(スペ...
具体的にこれらにどのような違いがあるかを調べる例を以下に...
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをFullJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをLeftに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをRightに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをCenterに
align.Style = Microsoft.International.JapaneseTextAli...
'文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをFullJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをLeftに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをRightに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをCenterに
align.Style = Microsoft.International.JapaneseTextAli...
//文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//後始末
f.Dispose();
}
}}
このコードを実行した結果、以下のように表示されます。
&ref(JapaneseTextAlignment1.png);
上記の例では使いませんでしたが、TextAlignmentStyleInfo構...
**SimpleAlignmentUnitInfoProviderとSmartAlignmentUnitInfo...
SimpleAlignmentUnitInfoProviderは最も単純なIAlignmentUnit...
SmartAlignmentUnitInfoProviderでは、日本語の文字の間には...
-前の文字がCJK統合漢字。
-前の文字が '(' (U+0028)でなく、現在の文字が '('。
-前の文字が ')' (U+0029)で、現在のキャラクタが ')'ではな...
-前の文字がアルファベット (a-z and A-Z), 数字 (0-9) およ...
上記の例では、SimpleAlignmentUnitInfoProviderを使用してい...
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'SimpleAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align)
'範囲を表す四角を描画
rect.Y += rect.Height + 5
e.Graphics.DrawRectangle(Pens.Red, rect)
'SmartAlignmentUnitInfoProviderの作成
Dim provider As Microsoft.International.JapaneseTextA...
New Microsoft.International.JapaneseTextAlignment...
'SmartAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, _
provider)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//SimpleAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align);
//範囲を表す四角を描画
rect.Y += rect.Height + 5;
e.Graphics.DrawRectangle(Pens.Red, rect);
//SmartAlignmentUnitInfoProviderの作成
Microsoft.International.JapaneseTextAlignment.IAlignm...
new Microsoft.International.JapaneseTextAlignment...
//SmartAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider);
//後始末
f.Dispose();
}
}}
このコードを実行した結果、以下のように表示されます。
&ref(JapaneseTextAlignment2.png);
このように、SimpleAlignmentUnitInfoProviderで描画したとき...
**IAlignmentUnitInfoProviderを実装して割り付け方を変更す...
IAlignmentUnitInfoProviderを実装したクラスを作成して、ど...
Utility.DrawJapaneseStringメソッドで文字列を均等割付で描...
はじめの割付け単位の分割は自動的に行われ、スペース文字(...
ここからがIAlignmentUnitInfoProviderの仕事です。まずIniti...
このように割付け単位の最後の文字まで1文字ずつIsNewAlignme...
"This is a pen."の例で言えば、次のようになります。
+Initializeが呼び出され、"T"がパラメータとして渡されます。
+IsNewAlignmentUnitが呼び出され、"h"が渡されます。
+IsNewAlignmentUnitが呼び出され、"i"が渡されます。
+IsNewAlignmentUnitが呼び出され、"s"が渡されます。
+ここで新しい割付け単位になるため、Initializeが呼び出され...
+IsNewAlignmentUnitが呼び出され、"s"が渡されます。
+次の割付け単位は"a"一文字なので、Initializeが呼び出され...
+Initializeが呼び出され、"p"が渡されます。
+IsNewAlignmentUnitが呼び出され、"e"が渡されます。
+IsNewAlignmentUnitが呼び出され、"n"が渡されます。
+IsNewAlignmentUnitが呼び出され、"."が渡されます。
少し分かりにくかったかもしれませんが、簡単に言えば、均等...
具体例を示します。以下に示すIAlignmentUnitInfoProviderを...
#code(vbnet){{
'IAlignmentUnitInfoProviderを実装したクラス
Public Class TestAlignmentUnitInfoProvider
Implements Microsoft.International.JapaneseTextAlignm...
'前の文字
Private previousString As String
Public Sub Initialize(ByVal s As String) _
Implements Microsoft.International.JapaneseTextAl...
IAlignmentUnitInfoProvider.Initialize
If String.IsNullOrEmpty(s) Then
Throw New ArgumentException("sがnullか空です...
End If
'文字を覚えておく
Me.previousString = s
End Sub
Public Function IsNewAlignmentUnit(ByVal s As String)...
Implements Microsoft.International.JapaneseTextAl...
IAlignmentUnitInfoProvider.IsNewAlignmentUnit
If String.IsNullOrEmpty(s) Then
Throw New ArgumentException("sがnullか空です...
End If
'英数字以外かを判断する
Dim reg As New System.Text.RegularExpressions.Reg...
"^[^a-zA-Z0-9]", _
System.Text.RegularExpressions.RegexOptions.C...
'前の文字と今の文字が英数字のときだけ間隔をあけな...
Dim ret As Boolean = reg.IsMatch(Me.previousStrin...
'前の文字を更新
Me.previousString = s
'結果を返す
Return ret
End Function
End Class
}}
#code(csharp){{
//IAlignmentUnitInfoProviderを実装したクラス
public class TestAlignmentUnitInfoProvider :
Microsoft.International.JapaneseTextAlignment.IAlignm...
{
//前の文字
private string previousString;
public void Initialize(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentException("sがnullか空です...
//文字を覚えておく
this.previousString = s;
}
public bool IsNewAlignmentUnit(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentException("sがnullか空です...
//英数字以外かを判断する
System.Text.RegularExpressions.Regex reg =
new System.Text.RegularExpressions.Regex(
"^[^a-zA-Z0-9]",
System.Text.RegularExpressions.RegexOptio...
//前の文字と今の文字が英数字のときだけ間隔をあけ...
bool ret = reg.IsMatch(this.previousString) || re...
//前の文字を更新
this.previousString = s;
//結果を返す
return ret;
}
}
}}
このクラスを使って文字列を均等割付で描画する例を示します。
#code(vbnet){{
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As System.Obje...
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
'表示する文字列
Dim str As String = ".NET Framework 3.5(Microsoft)で...
'フォントと色
Dim f As New Font("MS UI Gothic", 12)
Dim c As Color = Color.Black
'描画する範囲を指定
Dim rect As New Rectangle(5, 5, 500, 30)
'TextAlignmentStyleInfoの作成
Dim align As New Microsoft.International.JapaneseText...
'範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect)
'整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
'TestAlignmentUnitInfoProviderの作成
Dim provider As New TestAlignmentUnitInfoProvider()
'TestAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider)
'後始末
f.Dispose()
End Sub
}}
#code(csharp){{
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventA...
{
//表示する文字列
string str = ".NET Framework 3.5(Microsoft)で実行中";
//フォントと色
Font f = new Font("MS UI Gothic", 12);
Color c = Color.Black;
//描画する範囲を指定
Rectangle rect = new Rectangle(5, 5, 500, 30);
//TextAlignmentStyleInfoの作成
Microsoft.International.JapaneseTextAlignment.TextAli...
new Microsoft.International.JapaneseTextAlignment...
//範囲を表す四角を描画
e.Graphics.DrawRectangle(Pens.Red, rect);
//整列スタイルをJustifyに
align.Style = Microsoft.International.JapaneseTextAli...
//TestAlignmentUnitInfoProviderの作成
TestAlignmentUnitInfoProvider provider = new TestAlig...
//TestAlignmentUnitInfoProviderで文字列を描画する
Microsoft.International.JapaneseTextAlignment.Utility...
e.Graphics, str, f, c, rect, align, provider);
//後始末
f.Dispose();
}
}}
結果は、以下の図のようになります。
&ref(JapaneseTextAlignment3.png);
**スペース文字の部分だけに隙間を入れる [#af06ea47]
スペース文字の部分だけに隙間が入るようにして均等割付で描...
これとは逆にIsNewAlignmentUnitメソッドで常にTrueを返すの...
**コメント [#y3b5a8d6]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2008-1...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]