.
In Depth Banner
Skip Navigation Links

Select your preferred language

The GridSnap Test application.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace TestGridSnap

{

  /// <summary>

  /// Summary description for Form1.

  /// </summary>

  public class Form1 : System.Windows.Forms.Form

  {

    private WellFormed.GridSnap gridSnap1;

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

 

    public Form1()

    {

      //

      // 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()

    {

      this.gridSnap1 = new WellFormed.GridSnap();

      this.SuspendLayout();

      //

      // gridSnap1

      //

      this.gridSnap1.Dock = System.Windows.Forms.DockStyle.Fill;

      this.gridSnap1.Location = new System.Drawing.Point(0, 0);

      this.gridSnap1.Name = "gridSnap1";

      this.gridSnap1.Size = new System.Drawing.Size(292, 273);

      this.gridSnap1.Snap = true;

      this.gridSnap1.SnapX = 32;

      this.gridSnap1.SnapY = 32;

      this.gridSnap1.TabIndex = 0;

      this.gridSnap1.Text = "gridSnap1";

      this.gridSnap1.Click += new System.EventHandler(this.gridSnap1_Click);

      this.gridSnap1.Paint += new System.Windows.Forms.PaintEventHandler(this.gridSnap1_Paint);

      this.gridSnap1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.gridSnap1_MouseMove);

      //

      // Form1

      //

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.ClientSize = new System.Drawing.Size(292, 273);

      this.Controls.Add(this.gridSnap1);

      this.Name = "Form1";

      this.Text = "Form1";

      this.ResumeLayout(false);

 

    }

    #endregion

 

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

      Application.Run(new Form1());

    }

 

    ArrayList points=new ArrayList();

 

    private void gridSnap1_Click(object sender, System.EventArgs e)

    {

      points.Add(pos);

      this.gridSnap1.Invalidate();

    }

 

    private void gridSnap1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

    {

      foreach(Point p in points)

      {

        e.Graphics.FillEllipse(Brushes.Red, p.X-2,p.Y-2,4,4);

      }

 

      e.Graphics.DrawEllipse(Pens.Teal,pos.X-5,pos.Y-5,10,10);

    }

 

    Point pos;

 

    private void gridSnap1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

    {

      pos=new Point(e.X,e.Y);

      this.gridSnap1.Invalidate();

    }

  }

}

 

Note how the code doesn't do anything except respond to the mouse events in the normal way. Nothing needs to be done except set the grid snap value and turn it on.

Return to the article.

Copyright © Bob Powell 2000-.  All rights reserved.