在C#中使用的DrawString文本两端 [英] Justifying text using DrawString in C#

查看:190
本文介绍了在C#中使用的DrawString文本两端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画一个 System.Drawing.Graphics 对象上的文字。我使用了的DrawString 法,文本字符串时,字体,一个,一个包围的RectangleF 的StringFormat 作为参数。<​​/ p>

展望的StringFormat ,我发现我可以将其设置的对齐属性到附近中心远东。但是我还没有找到一种方法将其设置为合理的。我怎样才能做到这一点?



感谢您的帮助!


解决方案

有没有内置的方式来做到这一点。一些变通都在这个线程提到的:



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



他们建议使用覆盖的RichTextBox ,覆盖 SelectionAlignment 财产(见此页如何),并将其设置为对齐



覆盖的胆量围绕此的PInvoke呼叫:

  PARAFORMAT FMT =新PARAFORMAT(); 
fmt.cbSize = Marshal.SizeOf(FMT);
fmt.dwMask = PFM_ALIGNMENT;
fmt.wAlignment =(短期)的价值;

的SendMessage(新HandleRef(这一点,手柄),//这在RichTextBox
EM_SETPARAFORMAT,
SCF_SELECTION,楼盘FMT);



不知道如何好这个可以集成到现有的模型(因为我假设你吸引更多的除了文本),但它可能是你唯一的选择。


I'm drawing text on a System.Drawing.Graphics object. I'm using the DrawString method, with the text string, a Font, a Brush, a bounding RectangleF, and a StringFormat as arguments.

Looking into StringFormat, I've found that I can set it's Alignment property to Near, Center or Far. However I haven't found a way to set it to Justified. How can I achieve this?

Thank you for your help!

解决方案

There is no built-in way to do it. Some work-arounds are mentioned on this thread:

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

They suggest using an overridden RichTextBox, overriding the SelectionAlignment property (see this page for how) and setting it to Justify.

The guts of the override revolve around this pInvoke call:

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);

Not sure how well this can be integrated into your existing model (since I assume you're drawing more than text), but it might be your only option.

这篇关于在C#中使用的DrawString文本两端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆