473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

help with example "paint on form by mouse"

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
2 4889
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: prasat | last post by:
Hi Does anybody work with mouse package for cfd analysis in c++. I would like to decide issues. Please reply --
7
by: Larry R Harrison Jr | last post by:
I am looking for javascript and a basic tutorial on how to make mouse-over drop-down menus--the type that when you "hover" over a subject links relevant to that subject "emerge" which you can then...
3
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their...
11
by: Pete Wilson | last post by:
Hi folks -- The page at http://www.pwilson.net/submit-demo.html will not validate. The validator at http://validator.w3.org tells me I can't have an input inside a form. Would some kind...
4
by: ginee | last post by:
Hi all, While i try to build a example c# project, i get a error message "The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)"....
0
by: Per Larsson | last post by:
I have been nagging for help on this subject for quite some time now. Finally got it sorted out by my self. Here is the code if some one else ever need it. 'Declarations: Declare Function...
8
by: Martin | last post by:
I hope not, but, I think the answer to this question is "it can't be done". Northwind sample database. Orders form. Go to a new record. Select a customer in "Bill To:" Don't enter any...
8
by: abcd | last post by:
I can get the value on the form at the server side by using Request.form("max") when max field is disabled I dont get value. For GUI and business logic purpose I have disabled some fields with...
2
by: Parasyke | last post by:
Please help.... I have a form that I successfully add records to that I want to copy and turn into a form for editing records from that same table (It is imperitive that it be done this way, rather...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.