By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

help with example "paint on form by mouse"

P: n/a
Hi!

1. Please, help with example "paint on form by mouse"
2. Below is my example, but it clear the line after each Refresh()... how to
fix?
3. How to draw the line in Mouse_Move event?
private Boolean isCanPaint = false;
private MouseEventArgs MouseEvent1;
private MouseEventArgs MouseEvent2;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent1 = e;
label1.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
}
private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent2 = e;
label2.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
isCanPaint = true;
Refresh();
}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (isCanPaint)
{
Pen myPen = new Pen(Color.Blue, 3);
e.Graphics.DrawLine(myPen,MouseEvent1.X, MouseEvent1.Y, MouseEvent2.X,
MouseEvent2.Y);
}
}
--
Serge
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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();

}

}

}
----------------------------------------------------------------------------
-----------------------


"Serge Klokov" <Su*********@web.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi!

1. Please, help with example "paint on form by mouse"
2. Below is my example, but it clear the line after each Refresh()... how to fix?
3. How to draw the line in Mouse_Move event?
private Boolean isCanPaint = false;
private MouseEventArgs MouseEvent1;
private MouseEventArgs MouseEvent2;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent1 = e;
label1.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
}
private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MouseEvent2 = e;
label2.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
isCanPaint = true;
Refresh();
}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (isCanPaint)
{
Pen myPen = new Pen(Color.Blue, 3);
e.Graphics.DrawLine(myPen,MouseEvent1.X, MouseEvent1.Y, MouseEvent2.X,
MouseEvent2.Y);
}
}
--
Serge

Nov 16 '05 #2

P: n/a
Hi!

Method's:

1. Using ArrayList of points for save and draw
2. Using class GraphicsPath for make figure
3. Using "shadow bitmap"
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.