2011-09-21 5 views
5

私はSystem.Drawing.Graphicsオブジェクトにテキストを描画しています。私は、テキスト文字列、FontBrush、境界RectangleF、およびStringFormatを引数としてDrawStringメソッドを使用しています。CでDrawStringを使用してテキストを揃える

StringFormatを見ると、私はそれがAlignment財産Nearに、CenterまたはFarだ設定ができることを発見しました。しかし私はそれを正当化する方法を見つけていない。どうすればこれを達成できますか?

ありがとうございました!

答えて

1

組み込みの方法はありません。いくつかの回避策は、このスレッドに記載されている:彼らはSelectionAlignmentプロパティを(this page for howを参照)をオーバーライドしJustifyにそれを設定し、上書きRichTextBoxを使用することをお勧め

http://social.msdn.microsoft.com/Forums/zh/winforms/thread/aebc7ac3-4732-4175-a95e-623fda65140e

。オーバーライドの

ガッツこれのPInvoke呼び出しを中心に展開:(私はあなたがテキストよりも多くを描画していると仮定するので)これは、既存のモデルに統合する方法もわからない

PARAFORMAT fmt = new PARAFORMAT(); 
fmt.cbSize = Marshal.SizeOf(fmt); 
fmt.dwMask = PFM_ALIGNMENT; 
fmt.wAlignment = (short)value; 

SendMessage(new HandleRef(this, Handle), // "this" is the RichTextBox 
    EM_SETPARAFORMAT, 
    SCF_SELECTION, ref fmt); 

を、それがかもしれませんあなたの唯一の選択肢です。

1

私はそれが簡単で

http://csharphelper.com/blog/2014/10/fully-justify-a-line-of-text-in-c/

:)をFOUND - あなたは段落全体の所定の幅を知っているとき、あなたはそれぞれ別の行のテキストを正当化することができます。

float extra_space = rect.Width - total_width; // where total_width is the sum of all measured width for each word 
int num_spaces = words.Length - 1; // where words is the array of all words in a line 
if (words.Length > 1) extra_space /= num_spaces; // now extra_space has width (in px) for each space between words 

残りがありますかなり直感的です:

float x = rect.Left; 
float y = rect.Top; 
for (int i = 0; i < words.Length; i++) 
{ 
    gr.DrawString(words[i], font, brush, x, y); 

    x += word_width[i] + extra_space; // move right to draw the next word. 
} 
関連する問題