TextRenderer.DrawText有问题

北京的雾霾天 2007-12-24 02:00:33
TextRenderer.DrawText方法会不受剪切区域的限制,请问怎么能受限制?

我测试代码如下,可以从运行效果上很显示的看出Graphics.DrawsString方法是可以的,但是TextRenderer.DrawText不行。


protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle rect = new Rectangle(50,50,50,50);
Rectangle _rect = new Rectangle(50, 50, 100, 50);
e.Graphics.SetClip(rect);

e.Graphics.FillRectangle(SystemBrushes.ControlDark, rect);
e.Graphics.DrawString("1234567890", this.Font, SystemBrushes.Window, _rect);
TextRenderer.DrawText(e.Graphics, "1234567890", this.Font, _rect, Color.Empty, Color.Empty, TextFormatFlags.VerticalCenter| TextFormatFlags.Left);
}
...全文
1990 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawnxu 2010-06-17
  • 打赏
  • 举报
回复
谢谢.....
lethwei 2009-11-27
  • 打赏
  • 举报
回复
很好的一篇讨论, 刚好在研究这块
Efcndi 2007-12-26
  • 打赏
  • 举报
回复
oh 是我把这个想得太复杂了。
我一直把textrenderer作为文本的格式化类。对于gdi和gdi+不甚了解。
把print理解为draw...haha 自作聪明了。

16楼的说的是。xx lz。
北京的雾霾天 2007-12-26
  • 打赏
  • 举报
回复
Print就是指的向打印机输出,就是打印,而Draw是指向Windows界面上呈现。
Efcndi 2007-12-26
  • 打赏
  • 举报
回复
TextRenderer.MeasureText确实有用。
弱弱的问lz一句,这里的print和draw的区别?
谢谢!
北京的雾霾天 2007-12-26
  • 打赏
  • 举报
回复
指定格式TextFormatFlags.PreserveGraphicsClipping是正确的,多谢lalac。我正忽略了DrawText的文本输出格式。

TextRenderer.DrawText方法在MSDN上确实是说不支持打印,但是对于Windows界面上的文字基本上都是用了TextRenderer.DrawText所对应的API来输出的。

和Graphics.DrawString来比,这个方法有着DrawString无法替代的一面,因此有的地方我一定要使用这个方法来呈现文本信息,在打印的方面,我使用打印Image或使用Graphics.DrawString就可以了。
wzd24 2007-12-25
  • 打赏
  • 举报
回复
呵呵………………
刚刚反射了代码,TextRenderer并不是利用GDI+来绘制文本的,SetClip对它跟本没用,看来你只能用Graphics.DrawString()来绘制了。
北京的雾霾天 2007-12-25
  • 打赏
  • 举报
回复
哎,不知道DrawText本来就是这样,还是微软在这个问题上给忽略了。非常感谢牧野的回复。
wzd24 2007-12-25
  • 打赏
  • 举报
回复
不好意思,还是看错了,上面的话做废!
wzd24 2007-12-25
  • 打赏
  • 举报
回复
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle rect = new Rectangle(50, 50, 50, 50);
Rectangle _rect = new Rectangle(50, 50, 30, 50);
e.Graphics.SetClip(rect);

e.Graphics.FillRectangle(SystemBrushes.ControlDark, rect);
//e.Graphics.DrawString("1234567890", this.Font, SystemBrushes.Window, _rect);
TextRenderer.DrawText(e.Graphics, "1234567890", this.Font, _rect, Color.Empty, Color.Empty, TextFormatFlags.VerticalCenter | TextFormatFlags.Left);

}

你可以试试看,超出范围的都被截掉了!
你上面的_rect好像太宽了一点。rect是50宽,_rect是100宽。
Efcndi 2007-12-25
  • 打赏
  • 举报
回复
这里print的意思不是“打印”, 而是“draw”。
Efcndi 2007-12-25
  • 打赏
  • 举报
回复
楼上的不知道是我理解错了,还是你的理解错了。
请看:http://english.zhuaxia.com/item/578199259


One of my Customer has a windows application(VS2005, VB.NET) where they are trying to print the Malayalam language string (Kartika font). As recommended they are using the TextRenderer class to view the string and it works fine.

It mentioned in the MSDN that TextRenderer is not used for printing:

The DrawText methods of TextRenderer are not supported for printing. You should always use the DrawString methods of the Graphics class

So to print the Malayalam string we have following two options

1. We can save the string in a bitmap image and print it using the Graphics.DrawImage function.
2. We can directly print the string from textbox using the Graphics.DrawString.
lalac 2007-12-25
  • 打赏
  • 举报
回复
To Efcndi:


msdn的说法是TextRenderer是用来浏览string的,而不是用来print string的。
所以在需要画string的地方,应该使用Graphics.DrawString()来画。


技术的东西,请不要太随意,想当然会祸害自己,也会给别人带来混淆,下面是从MSDN上摘录:


TextRenderer 类


注意:此类在 .NET Framework 2.0 版中是新增的。

提供用于测量呈现文本的方法。无法继承此类。

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)

语法


public sealed class TextRenderer


备注
TextRenderer 类提供了一组 static 方法,可用于在 Windows 窗体控件上测量绘制文本。

您可以通过使用带有 TextFormatFlags 参数的 DrawText 重载之一,控制文本的绘制方式。例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。如果绘制一行文本时不需要这些额外空白,您应当使用带有 Size 和 TextFormatFlags 参数的 DrawText 和 MeasureText 版本。有关示例,请参见 MeasureText(IDeviceContext,String,Font,Size,TextFormatFlags)。

注意
TextRenderer 的 DrawText 方法不支持打印您应当持续使用 Graphics 类的 DrawString 方法。


示例
下面的代码示例说明如何使用 DrawText 方法。若要运行此示例,请将该代码粘贴到一个 Windows 窗体中,并从该窗体的 Paint 事件处理程序中调用 RenderText1 ,将 e 作为 PaintEventArgs 传递。


private void RenderText1(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
new Point(10, 10), SystemColors.ControlText);
}
王集鹄 2007-12-25
  • 打赏
  • 举报
回复
TextFormatFlags.PreserveGraphicsClipping


来晚了...
Efcndi 2007-12-25
  • 打赏
  • 举报
回复
msdn的说法是TextRenderer是用来浏览string的,而不是用来print string的。
所以在需要画string的地方,应该使用Graphics.DrawString()来画。
lalac 2007-12-25
  • 打赏
  • 举报
回复
不要空发感叹!
TextRenderer是使用GDI画的,要保留Clip区,加上TextFormatFlags.PreserveGraphicsClipping就可以了。


protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle rect = new Rectangle(50, 50, 50, 50);
Rectangle _rect = new Rectangle(50, 50, 100, 50);
e.Graphics.SetClip(rect);

e.Graphics.FillRectangle(SystemBrushes.ControlDark, rect);
e.Graphics.DrawString("1234567890", this.Font, SystemBrushes.Window, _rect);

TextRenderer.DrawText(e.Graphics, "1234567890", this.Font, _rect, Color.Empty, Color.Empty, TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.PreserveGraphicsClipping);
}
北京的雾霾天 2007-12-25
  • 打赏
  • 举报
回复
Graphics.DrawString和TextRenderer.DrawText本质上是不一样的。

不知微软为什么不给设定剪切区域。
北京的雾霾天 2007-12-24
  • 打赏
  • 举报
回复
多谢了牧野。
wzd24 2007-12-24
  • 打赏
  • 举报
回复
别,让我晚上帮你看看,现在上班不方便!
北京的雾霾天 2007-12-24
  • 打赏
  • 举报
回复
看来要不满意揭帖了。
加载更多回复(4)
这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧