2009-07-21 69 views
3

我需要將文本直接繪製到Windows Mobile窗體上。這是一個Compact Framework 2.0應用程序。我有以下測試代碼在Windows窗體應用程序中工作,但它不適用於緊湊框架,因爲沒有DirectionVertical StringFormatFlag。有沒有另一種方式在Windows Mobile上做同樣的事情?升級到Compact Framework 3.5沒有幫助。它具有與2.0相同的StringFormatFlags。如何使用Compact Framework垂直繪製文本

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, 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); 
} 

謝謝。

回答

6
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", myFont, myBrush, myRect, myFormat); 
+0

什麼圖形g在靜態方法? – 2017-04-27 06:14:25

0

轉換爲VB.NET

Public Function CreateRotatedFont(ByVal FontName As String, ByVal Height As Integer, ByVal AngleInDegrees As Integer, ByVal g As Graphics) As Font 

    Dim logf As 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) 

End Function 

用途:

Dim myGraphic As Graphics = Me.CreateGraphics() 
    Dim myFont As Font = CreateRotatedFont("Tahoma", 32, 90, myGraphic) 
    Dim myBrush As New SolidBrush(Color.Black) 
    myGraphic.DrawString("Hello", myFont, myBrush, 200, 103) 
1

我知道這個問題已經回答了,但我在微軟網站mo上找到this example徹底和有用的:

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using Microsoft.WindowsCE.Forms; 

namespace LogFontDemo 
{ 
    public class Form1 : System.Windows.Forms.Form 
    { 
     // Declare objects to draw the text. 
     Font rotatedFont; 
     SolidBrush redBrush; 

     // Specify the text to roate, the rotation angle, 
     // and the base font. 
     private string rTxt = "abc ABC 123"; 
     private int rAng = 45; 

     // Determine the vertial DPI setting for scaling the font on the 
     // device you use for developing the application. 
     // You will need this value for properly scaling the font on 
     // devices with a different DPI. 
     // In another application, get the DpiY property from a Graphics object 
     // on the device you use for application development: 
     // 
     // Graphics g = this.CreateGraphics(); 
     // int curDPI = g.DpiY; 

     private const int curDPI = 96; 

     // Note that capabilities for rendering a font are 
     // dependant on the device. 
     private string rFnt = "Arial"; 

     public Form1() 
     { 
      // Display OK button to close application. 
      this.MinimizeBox = false; 
      this.Text = "Rotated Font"; 

      // Create rotatedFont and redBrush objects in the custructor of 
      // the form so that they can be resued when the form is repainted. 
      this.rotatedFont = CreateRotatedFont(rFnt, rAng); 
      this.redBrush = new SolidBrush(Color.Red); 
     } 

     // Method to create a rotated font using a LOGFONT structure. 
     Font CreateRotatedFont(string fontname, int angleInDegrees) 
     { 
      LogFont logf = new Microsoft.WindowsCE.Forms.LogFont(); 

      // Create graphics object for the form, and obtain 
      // the current DPI value at design time. In this case, 
      // only the vertical resolution is petinent, so the DpiY 
      // property is used. 

      Graphics g = this.CreateGraphics(); 
      // Scale an 18-point font for current screen vertical DPI. 
      logf.Height = (int)(-18f * g.DpiY/curDPI); 

      // Convert specified rotation angle to tenths of degrees. 
      logf.Escapement = angleInDegrees * 10; 

      // Orientation is the same as Escapement in mobile platforms. 
      logf.Orientation = logf.Escapement; 

      logf.FaceName = fontname; 

      // Set LogFont enumerations. 
      logf.CharSet  = LogFontCharSet.Default; 
      logf.OutPrecision = LogFontPrecision.Default; 
      logf.ClipPrecision = LogFontClipPrecision.Default; 
      logf.Quality  = LogFontQuality.ClearType; 
      logf.PitchAndFamily = LogFontPitchAndFamily.Default; 

      // Explicitly dispose any drawing objects created. 
      g.Dispose(); 

      return Font.FromLogFont(logf); 
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      if(this.rotatedFont == null) 
       return; 

      // Draw the text to the screen using the LogFont, starting at 
      // the specified coordinates on the screen. 
      e.Graphics.DrawString(rTxt, 
       this.rotatedFont, 
       this.redBrush, 
       75, 
       125, 
       new StringFormat(StringFormatFlags.NoWrap | 
        StringFormatFlags.NoClip)); 
     } 

     protected override void Dispose(bool disposing) 
     { 

      // Dispose created graphic objects. Although they are 
      // disposed by the garbage collector when the application 
      // terminates, a good practice is to dispose them when they 
      // are no longer needed. 
      this.redBrush.Dispose(); 
      this.rotatedFont.Dispose(); 
      base.Dispose(disposing); 
     } 

     static void Main() 
     { 
      Application.Run(new Form1()); 
     } 
    } 
}