Text Rotate 2 : Text « 2D Graphics « C# / C Sharp





Text Rotate 2

Text Rotate 2
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;

namespace Altamira
{
    /// <summary>
    /// Summary description for Altamira.
    /// </summary>
    public class Altamira : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Altamira()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "Text direction";
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Altamira
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(224, 141);
            this.Name = "Altamira";
            this.Text = "Altamira";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Altamira());
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = CreateGraphics();          
            string txt = "HELLO";
            float alpha = 45.0f;
            int fontSize = 24;
            Point center = new Point(90,20);

            // Vertical text:
            FontFamily ff = new FontFamily("Times New Roman");
            Font f = new Font(ff, fontSize, FontStyle.Regular);
            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.DirectionVertical;
            g.DrawString(txt, f, new SolidBrush(Color.Blue), center, sf);

            // Global shift of the origin:
            g.TranslateTransform(center.X, center.Y);  // X + fontSize/2
            g.DrawEllipse(Pens.Magenta, new Rectangle(0,0, 1, 1));  // center

            // Local rotation of vertcal text (sf):
            GraphicsPath gp = new GraphicsPath();
            gp.AddString(txt, ff, (int)FontStyle.Bold, fontSize + 4, 
                new Point(0, 0), sf);
            Matrix m = new Matrix();
            m.Rotate(alpha);  // clockwise
            gp.Transform(m);
            g.DrawPath(Pens.Red, gp);  //g.FillPath(Brushes.Black, gp);

            // Global rotation of vertical text (sf):
            g.RotateTransform(-alpha);  // anticlockwise
            g.DrawString(txt, f, new SolidBrush(Color.Black), 0, 0, sf);

            gp.Dispose();  g.Dispose();  m.Dispose();
        }
    }
}


           
       










Related examples in the same category

1.Center each line of text horizontally and verticallyCenter each line of text horizontally and vertically
2.Text string's containing rectangleText string's containing rectangle
3.Draw Vertical String TextDraw Vertical String Text
4.Deal with Tab and New Line in Painting StringDeal with Tab and New Line in Painting String
5.String Format based on Tab info dataString Format based on Tab info data
6.Draw the string, using the rectangle as a bounding boxDraw the string, using the rectangle as a bounding box
7.Draw rectangle and string insideDraw rectangle and string inside
8.Tall in the Center
9.Engrave
10.Emboss
11.Clip Text
12.Draw multiline text: auto wrapDraw multiline text: auto wrap
13.Draw string with new font and solid brushDraw string with new font and solid brush
14.Text RotateText Rotate
15.Text FormatText Format
16.Draw line and stringDraw line and string