Create Graphics from Form object : Graphics « 2D « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Windows.Forms;
   
class Scribble: Form
{
     bool  bTracking;
     Point ptLast;
   
     public static void Main()
     {
          Application.Run(new Scribble());
     }
     public Scribble()
     {
          Text = "Scribble";
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          if (mea.Button != MouseButtons.Left)
               return;
   
          ptLast = new Point(mea.X, mea.Y);
          bTracking = true;
     }
     protected override void OnMouseMove(MouseEventArgs mea)
     {
          if (!bTracking)
               return;
   
          Point ptNew = new Point(mea.X, mea.Y);
          
          Graphics grfx = CreateGraphics();
          grfx.DrawLine(new Pen(ForeColor), ptLast, ptNew);
          grfx.Dispose();
   
          ptLast = ptNew;
     }
     protected override void OnMouseUp(MouseEventArgs mea)
     {
          bTracking = false;
     }
}








27.1.Graphics
27.1.1.Create Graphics from Form object
27.1.2.Draw String
27.1.3.TranslateTransform a Graphics
27.1.4.Graphics.DrawImage
27.1.5.Graphics.DrawLine
27.1.6.Graphics.DrawLines
27.1.7.Set SmoothingMode
27.1.8.Draw along with the Tab
27.1.9.Graphics.DrawIcon
27.1.10.Double Buffering Example
27.1.11.No Double Buffering Example