Create Graphics from Form object : Graphics : Drawing 2D C# Examples


C# Examples » Drawing 2D » Graphics »

 

Create Graphics from Form object









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




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Drawing 2D
» Graphics