Share via


方法 : テキストを回転します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework は回転されたテキスト効果の作成用の LogFont クラスを提供します。 LOGFONT オブジェクトを取得する、 FromLogFont メソッドで使用するネイティブの Windows CE Font (論理フォント) 構造にそのして対応します。

使用例

フォームの中央に 45 度で文字列を描画するコード例を次に示します。 回転角度とをパラメーターとして回転させるテキストを取得し、CreateRotatedFont クラスを使用して、回転したフォントを返す LogFont メソッドを定義するとします。 回転したテキストは OnPaint イベント ハンドラーで、フォームに描画されます。

使用例は DPI 96 がする必要があります設定値に、ターゲット デバイスに適した。 値は、DpiYGraphics オブジェクトのプロパティを取得で判断できます。 このコード例には異なる DPI 値を持つデバイスのフォントを拡大する数式が含まれています。

                        Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms

PublicClass Form1
    Inherits System.Windows.Forms.Form

    ' Declare objects to draw the text.Private rotatedFont As System.Drawing.Font
    Private redBrush As SolidBrush

    ' Specify the text to roate, the rotation angle,    ' and the base font.Private rTxt AsString = "abc ABC 123"Private rAng AsInteger = 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:    '     '   Dim g As Graphics = Me.CreateGraphics()    '   Dim curDPI As Integer = g.DpiYPrivateConst curDPI AsInteger = 96

    ' Note that capabilities for rendering a font are     ' dependant on the device.Private rFnt AsString = "Arial"PublicSubNew()
        MyBase.New()

        ' Display OK button to close application.Me.MinimizeBox = FalseMe.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.Me.rotatedFont = CreateRotatedFont(rFnt, rAng)
        Me.redBrush = New SolidBrush(Color.Red)
    EndSub
    ' Method to create a rotated font using a LOGFONT structure.PrivateFunction CreateRotatedFont(ByVal fontname AsString, _
        ByVal angleInDegrees AsInteger) As Font

        Dim logf As LogFont = 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. Dim g As Graphics = Me.CreateGraphics

        ' Scale an 18-point font for current screen vertical DPI.
        logf.Height = Fix(-18.0F * 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 System.Drawing.Font.FromLogFont(logf)
    EndFunctionProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs)
        If (Me.rotatedFont IsNothing) ThenReturnEndIf        ' Draw the text to the screen using the LogFont, starting at        ' the specified coordinates on the screen.
        e.Graphics.DrawString(rTxt, Me.rotatedFont, Me.redBrush, _
            75, 125, New StringFormat( _
            (StringFormatFlags.NoWrap Or StringFormatFlags.NoClip)))
    EndSubProtectedOverridesSub Dispose(ByVal disposing AsBoolean)

        ' 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.Me.redBrush.Dispose()
        Me.rotatedFont.Dispose()
        MyBase.Dispose(disposing)
    EndSubPublicSharedSub Main()
        Application.Run(New Form1)
    EndSubEndClass
                        using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace LogFontDemo
{
    publicclass 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.privatestring rTxt = "abc ABC 123";
        privateint 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;privateconstint curDPI = 96;

        // Note that capabilities for rendering a font are// dependant on the device.privatestring 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);
        }

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

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

        staticvoid Main()
        {
            Application.Run(new Form1());
        }
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

堅牢性の高いプログラム

この例は、フォームを再描画するとき Font メソッドによって使用されるように、フォームのコンストラクターで、フォント、 SolidBrush および OnPaint オブジェクトをレンダリングするためのオブジェクト作成します。 破棄して、フォームの Dispose メソッドのオーバーライドでします。

参照

参照

LogFont