C# · 4月 3, 2022

c# – 如何使用Compact Framework垂直绘制文本

我需要直接在 Windows Mobile窗体上直接绘制文本.这是一个Compact Framework 2.0应用程序.我在Windows窗体应用程序中使用以下测试代码,但它不适用于紧凑框架,因为没有DirectionVertical StringFormatFlag.还有另一种方法可以在Windows Mobile上执行相同的操作吗?升级到Compact Framework 3.5没有帮助.它与2.0具有相同的StringFormatFlags.

private void TestDrawVertically(){ Font myFont = new Font(FontFamily.GenericSerif,10,FontStyle.Bold); System.Drawing.Brush myBrush = new SolidBrush(Color.Black); Rectangle myRect = new Rectangle(10,200,200); StringFormat myFormat = new StringFormat(); myFormat.LineAlignment = StringAlignment.Center; myFormat.Alignment = StringAlignment.Center; myFormat.FormatFlags = StringFormatFlags.DirectionVertical; Graphics myGraphic = this.CreateGraphics(); myGraphic.DrawString(“Hello”,myFont,myBrush,myRect,myFormat);}

谢谢.

解决方法 public static Font CreateRotatedFont(string fontname,int height,int angleIndegrees,Graphics g){ LogFont logf = new LogFont(); logf.Height = -1 * height; logf.FaceName = fontname; logf.Escapement = angleIndegrees * 10; logf.Orientation = logf.Escapement; logf.CharSet = LogFontCharSet.Default; logf.OutPrecision = LogFontPrecision.Default; logf.ClipPrecision = LogFontClipPrecision.Default; logf.Quality = LogFontQuality.ClearType; logf.PitchAndFamily = LogFontPitchAndFamily.Default; return Font.FromLogFont(logf);}

然后像这样使用它:

Graphics myGraphic = this.CreateGraphics();Font myFont = CreateRotatedFont(“Tahoma”,32,90,myGraphic);myGraphic.DrawString(“Hello”,myFormat);