我在System.Drawing.Graphics
对象上绘制文本。我使用DrawString
方法,将文本字符串,Font
,Brush
,边界RectangleF
和StringFormat
作为参数。在C中使用DrawString对齐文本#
展望StringFormat
,我发现我可以设置它的Alignment
属性Near
,Center
或Far
。但是我还没有找到将其设置为合理的方法。我怎样才能做到这一点?
谢谢你的帮助!
我在System.Drawing.Graphics
对象上绘制文本。我使用DrawString
方法,将文本字符串,Font
,Brush
,边界RectangleF
和StringFormat
作为参数。在C中使用DrawString对齐文本#
展望StringFormat
,我发现我可以设置它的Alignment
属性Near
,Center
或Far
。但是我还没有找到将其设置为合理的方法。我怎样才能做到这一点?
谢谢你的帮助!
有没有内置的方式来做到这一点。一些变通办法是在这个线程中提到:
http://social.msdn.microsoft.com/Forums/zh/winforms/thread/aebc7ac3-4732-4175-a95e-623fda65140e
他们建议使用覆盖RichTextBox
,覆盖SelectionAlignment
财产(见this page for how)并将其设置为Justify
。
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);
不知道如何好这个可以集成到现有的模型(因为我假设你正在绘制要比文本),但它可能:
围绕此的PInvoke调用的倍率的胆量是你唯一的选择。
我发现它:)
http://csharphelper.com/blog/2014/10/fully-justify-a-line-of-text-in-c/
在短暂的 - 当你知道整个段落的给定的宽度可以证明在每个单独行文字:
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.
}