472,362 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 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 4806
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...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.