【问题标题】:DrawString with character wrapping带字符换行的 DrawString
【发布时间】:2011-02-28 10:17:14
【问题描述】:

我正在使用 C# 为 Windows Mobile 应用程序创建一个致命错误对话框。问题是当我尝试使用DrawString 绘制堆栈跟踪时,我的堆栈跟踪的一半被剪掉了,因为DrawString 使用自动换行而不是字符换行。

解释不明白的朋友:

当我绘制堆栈跟踪时,结果如下:

at
company.application.name.space.Funct
at
company.application.name.Function(St
at
etc. etc.

我希望它像这样打印:

at
company.application.name.space.Funct
ion(String sometext, Int32 somenumbe
r)
at
company.application.name.Function(St
ring sometext, Int32 somenumber, Int
32 anothernumber)
at
etc. etc.

这在 Csharp 中可行吗?

重现问题:

  1. 创建一个新的智能设备项目。 (设备应用)

  2. 将 Form1 代码替换为以下内容:

    public partial class Form1 : Form
    {
       RectangleF _rect = new RectangleF();
       String _stackTrace = @"at System.Net.Sockets.Socket.ReceiveNoCheck(Byte[] buffer, Int32 index, Int32 request, SocketFlags socketFlags)
       at System.Net.Sockets.Socket.ReceiveAsyncRequest.doRequest()
       at System.Net.Sockets.Socket.AsyncRequest.handleRequest()
       at System.Net.Sockets.Socket.WorkerThread.doWork()
       at System.Net.Sockets.Socket.WorkerThread.doWorkI(Object o)
       at System.Threading.ThreadPool.WorkItem.doWork(Object o)
       at System.Threading.Timer.ring()";
    
       protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
       {
          _rect.X = 24 * factor.Width;
          _rect.Y = 10 * factor.Height;
          _rect.Width = 192 * factor.Width;
          _rect.Height = 274 * factor.Height;
       }
    
       public Form1()
       {
          InitializeComponent();
       } 
    
       protected override void OnPaint(PaintEventArgs e)
       {
          base.OnPaint(e);
          using (Pen borderPen = new Pen(this.ForeColor, 1))
          {
             Rectangle borderRect = Rectangle.Round(_rect);
             borderRect.Inflate(1, 1);
             e.Graphics.DrawRectangle(borderPen, borderRect);
          }
          using (StringFormat stringFormat = new StringFormat())
          using (SolidBrush stringBrush = new SolidBrush(this.ForeColor))
          {
             e.Graphics.DrawString(_stackTrace, this.Font, stringBrush, _rect, stringFormat);
          }
       }
    

    }

  3. 运行项目。

【问题讨论】:

    标签: c# word-wrap drawstring


    【解决方案1】:

    如果您使用采用 RectangleF 的 DrawString 的重载,它应该使用字符换行。你是怎么调用 DrawString() 的?

    【讨论】:

    • 我在 OP 中添加了代码。 RectangleF 似乎没有解决我的问题 :)
    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 2011-05-04
    • 2016-06-16
    • 2017-04-27
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多