Font Style: Bold

image_pdfimage_print


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Font font = new Font("Times New Roman", 12, FontStyle.Bold);

      g.DrawString("Bold", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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


           
          


Font Style: Regular

image_pdfimage_print


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Font font = new Font("Times New Roman", 12, FontStyle.Regular);
      int h = font.Height;

      g.DrawString("Regular", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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


           
          


Winding

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class FillModesOddity: Form
{
     public static void Main()
     {
          Application.Run(new FillModesOddity());
     }
     public FillModesOddity()
     {
          Text = "Alternate and Winding Fill Modes (An Oddity)";
          ClientSize = new Size(2 * ClientSize.Height, ClientSize.Height);
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Brush    brush = new SolidBrush(clr);
          PointF[] aptf = { new PointF(100.1f, 400.7f), new PointF(400.5f, 10.7f),
                            new PointF(200.5f, 300.1f), new PointF(300.9f, 20.1f),
                            new PointF(300.9f, 200.5f), new PointF(300.3f, 390.5f),
                            new PointF(400.3f, 0.9f), new PointF(200.7f, 200.9f),
                            new PointF(500.7f, 100.3f), new PointF(100.1f, 400.3f)};
          
          grfx.FillPolygon(brush, aptf, FillMode.Winding);
     }
}

    


Alternate

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class FillModesOddity : Form {
    public static void Main() {
        Application.Run(new FillModesOddity());
    }
    public FillModesOddity() {
        Text = "Alternate and Winding Fill Modes (An Oddity)";
        ClientSize = new Size(2 * ClientSize.Height, ClientSize.Height);
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
    }
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
        Brush brush = new SolidBrush(clr);
        PointF[] aptf = { new PointF(100.1f, 400.7f), new PointF(400.5f, 10.7f),
                            new PointF(200.5f, 300.1f), new PointF(300.9f, 20.1f),
                            new PointF(300.9f, 200.5f), new PointF(300.3f, 390.5f),
                            new PointF(400.3f, 0.9f), new PointF(200.7f, 200.9f),
                            new PointF(500.7f, 100.3f), new PointF(100.1f, 400.3f)};
        grfx.FillPolygon(brush, aptf, FillMode.Alternate);
    }
}

    


Draw an ellipse

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class TestGDI4 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        

        Graphics g = pe.Graphics ;
        Pen pn = new Pen( Color.Blue, 100 );
        Rectangle rect = new Rectangle(50, 50, 200, 100);
        g.DrawEllipse( pn, rect );         

    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI4());//display form
    }
}




           
          


Drag and drop shape

image_pdfimage_print


   

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class TryMouse : Form {
  private Region region; 
  private int oldX = 0;
  private int oldY = 0;

  public TryMouse() {
    Size = new Size(300,200);
    BackColor = Color.White;
    GraphicsPath p = new GraphicsPath();
    p.AddPolygon(new Point[]{new Point(50,10), new Point(10,50), new Point(150,100)});
    region = new Region(p);
  }

  protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRegion(Brushes.Red, region);
    base.OnPaint(e);
  }

  protected override void OnMouseDown(MouseEventArgs e) {
    if (region.GetBounds(CreateGraphics()).Contains(e.X, e.Y)){
      oldX = e.X;
      oldY = e.Y;
    }
    base.OnMouseDown(e);
  }

  protected override void OnMouseUp(MouseEventArgs e) {
    region.Translate(e.X-oldX, e.Y-oldY);
    oldX = e.X;
    oldY = e.Y;
    Invalidate();
    base.OnMouseUp(e);
  }
  public static void Main() {
    Application.Run(new TryMouse());
  }
}

           
          


Scribble: drag and draw

image_pdfimage_print


   

/*
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;

namespace Scribble
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class frmScribble : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private bool mouseDown = false;
        private Point lastPoint = Point.Empty;
        private string color = "black";
      private Graphics g;
      private Pen p;

        public frmScribble()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
         g = CreateGraphics();
         p = new Pen(Color.FromName(color));

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

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

        protected override void OnMouseDown(MouseEventArgs e)
        {
            mouseDown = true;
            if(e.Button==MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu();
                m.MenuItems.Add(0, new MenuItem("black", new EventHandler(RightMouseButton_Click)));
                m.MenuItems.Add(1, new MenuItem("white", new EventHandler(RightMouseButton_Click)));
                m.MenuItems.Add(2, new MenuItem("red", new EventHandler(RightMouseButton_Click)));
                m.MenuItems.Add(3, new MenuItem("green", new EventHandler(RightMouseButton_Click)));
                m.MenuItems.Add(4, new MenuItem("blue", new EventHandler(RightMouseButton_Click)));
                m.Show(this, new Point(e.X, e.Y));
            }
        }

        protected void RightMouseButton_Click(object sender, EventArgs e)
        {
            color = ((MenuItem)sender).Text;
         p = new Pen(Color.FromName(color));
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            mouseDown = false;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if(lastPoint.Equals(Point.Empty)) lastPoint = new Point(e.X, e.Y);
            if(mouseDown) 
            {
                Point pMousePos = new Point(e.X, e.Y);
            g.DrawLine(p, pMousePos, lastPoint);
            }
            lastPoint = new Point(e.X, e.Y);
        }

        #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()
        {
            // 
            // frmScribble
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(808, 542);
            this.MaximizeBox = false;
            this.Name = "frmScribble";
            this.Text = "Scribble";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new frmScribble());
        }
    }
}