After my signature you'll find a scribble like application that handles
mouse, click and paint events to create a scribble like program.
Have fun...
--
Bob Powell [MVP]
Visual C#, System.Drawing
The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed:
http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS:
http://www.bobpowell.net/tipstricks.xml
Bob's Blog:
http://bobpowelldotnet.blogspot.com/atom.xml
----------------------------------------------------------------------------
-----------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Scribbler
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//array used to store arrays of points.
ArrayList items=new ArrayList();
//The line under construction.
ArrayList line;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
}
/// <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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(800, 600);
this.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseUp);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseDown);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.form1_Paint);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//a semaphore used to store the state of the mouse button
bool _drawing;
//Draws the lines in the items array and the one under construction.
private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
foreach(Point[] pa in items)
{
if(pa.Length>0)
{
if(pa.Length==1)
{
e.Graphics.DrawLine(Pens.Black,pa[0],pa[0]);
}
else
e.Graphics.DrawLines(Pens.Black,pa);
}
}
//If a new line is under construction draw it too for the benefit of the
user.
if(_drawing)
{
if(line.Count>1)
{
Point[] pts=new Point[line.Count];
line.CopyTo(pts,0);
e.Graphics.DrawLines(Pens.Black,pts);
}
}
}
//Begins drawing a new line
private void form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
_drawing=true;
line=new ArrayList();
}
//When drawing a line this accumulates points in the line array
//Note that no drawing is performed.
private void form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(_drawing)
{
this.line.Add(new Point(e.X,e.Y));
Invalidate();
}
}
//Ends accumulating a new line by creating a new point array and storing it
with the ones already completed
private void form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
_drawing=false;
line.Add(new Point(e.X,e.Y));
Point[] pts=new Point[line.Count];
line.CopyTo(pts,0);
items.Add(pts);
Invalidate();
}
}
}