带字符换行的抽绳

发布于 2024-09-04 09:48:33 字数 2002 浏览 3 评论 0原文

我正在使用 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 代码替换为以下内容:

    公共分部类 Form1 : Form
    {
       矩形F _rect = 新矩形F();
       String _stackTrace = @"at System.Net.Sockets.Socket.ReceiveNoCheck(Byte[] 缓冲区, Int32 索引, Int32 请求, SocketFlags socketFlags)
       在 System.Net.Sockets.Socket.ReceiveAsyncRequest.doRequest()
       在 System.Net.Sockets.Socket.AsyncRequest.handleRequest()
       在 System.Net.Sockets.Socket.WorkerThread.doWork()
       在 System.Net.Sockets.Socket.WorkerThread.doWorkI(Object o)
       在 System.Threading.ThreadPool.WorkItem.doWork(Object o)
       在 System.Threading.Timer.ring()";
    
       protected override void ScaleControl(SizeF 因子,BoundsSpecified 指定)
       {
          _rect.X = 24 * 因子.宽度;
          _rect.Y = 10 * 因子.高度;
          _rect.Width = 192 * 因子.Width;
          _rect.Height = 274 * 因子.Height;
       }
    
       公共表格1()
       {
          初始化组件();
       } 
    
       protected override void OnPaint(PaintEventArgs e)
       {
          基.OnPaint(e);
          使用 (Pen borderPen = new Pen(this.ForeColor, 1))
          {
             矩形边框矩形 = 矩形.Round(_rect);
             borderRect.Inflate(1, 1);
             e.Graphics.DrawRectangle(borderPen, borderRect);
          }
          使用 (StringFormat stringFormat = new StringFormat())
          使用 (SolidBrush stringBrush = new SolidBrush(this.ForeColor))
          {
             e.Graphics.DrawString(_stackTrace, this.Font, stringBrush, _rect, stringFormat);
          }
       }
    

    }

  3. 运行项目。

I'm creating a fatal error dialog for a Windows Mobile Application using C#. The problem is when I try to draw the stacktrace using DrawString, half of my stacktrace is getting clipped off because DrawString uses word wrapping instead of character wrapping.

For those who don't understand the explanation:

When i draw the stacktrace, it comes out as this:

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

And i want it to print like this:

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.

Is this possible in Csharp?

Reproducing the issue:

  1. Create a new smart device project. (Device application)

  2. Replace the Form1 code with the following:

    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. Run the project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-09-11 09:48:33

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

If you use the overload of DrawString that takes a RectangleF, it should use character-wrapping. How are you calling DrawString()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文