Draw line and string : Text « 2D Graphics « C# / C Sharp






Draw line and string

Draw line and string
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        public PenCaps1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // 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()
        {
            // 
            // PenCaps
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(424, 330);
            this.Name = "PenCaps";
            this.Text = "PenCaps";
            this.Resize += new System.EventHandler(this.PenCaps_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenCaps_Paint);

        }
        #endregion

        private void PenCaps_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 10);
            int y  = 20;
            
            foreach (LineCap cap in System.Enum.GetValues(typeof(LineCap)))
            {
                myPen.StartCap = cap;
                myPen.EndCap = cap;
                e.Graphics.DrawLine(myPen, 20, y, 100, y);
                e.Graphics.DrawString(cap.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
                y += 30;
            }
        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new PenCaps1());
        }

        private void PenCaps_Resize(object sender, System.EventArgs e)
        {
            this.Invalidate();
        }
    }
}



           
       








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.Text Rotate 2Text Rotate 2