开发者

DrawString with character wrapping

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开发者_如何学C 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜