473,769 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.MouseEven tArgs e)
{
MouseEvent1 = e;
label1.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
}
private void Form1_MouseUp(o bject sender,
System.Windows. Forms.MouseEven tArgs e)
{
MouseEvent2 = e;
label2.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
isCanPaint = true;
Refresh();
}

private void Form1_Paint(obj ect sender,
System.Windows. Forms.PaintEven tArgs e)
{
if (isCanPaint)
{
Pen myPen = new Pen(Color.Blue, 3);
e.Graphics.Draw Line(myPen,Mous eEvent1.X, MouseEvent1.Y, MouseEvent2.X,
MouseEvent2.Y);
}
}
--
Serge
Nov 16 '05 #1
2 4910
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.Collecti ons;

using System.Componen tModel;

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.Componen tModel.Containe r components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeCompo nent();

this.SetStyle(C ontrolStyles.Al lPaintingInWmPa int |

ControlStyles.U serPaint |

ControlStyles.D oubleBuffer,

true);

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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 InitializeCompo nent()

{

//

// Form1

//

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

this.ClientSize = new System.Drawing. Size(800, 600);

this.MouseUp += new
System.Windows. Forms.MouseEven tHandler(this.f orm1_MouseUp);

this.MouseMove += new
System.Windows. Forms.MouseEven tHandler(this.f orm1_MouseMove) ;

this.MouseDown += new
System.Windows. Forms.MouseEven tHandler(this.f orm1_MouseDown) ;

this.Paint += new System.Windows. Forms.PaintEven tHandler(this.f orm1_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(obj ect sender, System.Windows. Forms.PaintEven tArgs
e)

{

foreach(Point[] pa in items)

{

if(pa.Length>0)

{

if(pa.Length==1 )

{

e.Graphics.Draw Line(Pens.Black ,pa[0],pa[0]);

}

else

e.Graphics.Draw Lines(Pens.Blac k,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.Draw Lines(Pens.Blac k,pts);

}

}

}
//Begins drawing a new line

private void form1_MouseDown (object sender,
System.Windows. Forms.MouseEven tArgs 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.MouseEven tArgs e)

{

if(_drawing)

{

this.line.Add(n ew 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(o bject sender,
System.Windows. Forms.MouseEven tArgs 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*********@we b.de> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.MouseEven tArgs e)
{
MouseEvent1 = e;
label1.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
}
private void Form1_MouseUp(o bject sender,
System.Windows. Forms.MouseEven tArgs e)
{
MouseEvent2 = e;
label2.Text = "X = " + e.X.ToString() + " ; Y = " + e.Y.ToString();
isCanPaint = true;
Refresh();
}

private void Form1_Paint(obj ect sender,
System.Windows. Forms.PaintEven tArgs e)
{
if (isCanPaint)
{
Pen myPen = new Pen(Color.Blue, 3);
e.Graphics.Draw Line(myPen,Mous eEvent1.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
1419
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
6542
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 "hover" over and click. (see the links left on http://www.dpreview.com to see what I mean) I have code from smartwebby.com (DHTML) but I'm not sure if it's the best, and I'm not sure how to integrate any menus of my own into it. The code...
3
2706
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 "name" property (which sources from "name" HTML attribue). The problem is, that if the form contains form control named "name", it overwrites the form name property. In fact, I'm quite surprised that it's so easy to spoil any of the form object...
11
4177
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 soul please tell me what I'm doing wrong?
4
6091
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?)". This error occures in "using Microsoft.WindowCE ..Form". I am installing .net visual studio 2003 and sp2. Do i need to get some .dll or i just need changing some setting in .net v.s 2003?? Actually, I am not sure if the lib...
0
1678
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 GetWindowLong Lib "user32" _ Alias "GetWindowLongA" _ (ByVal hwnd As System.IntPtr, ByVal nIndex As Integer) As Integer
8
2046
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 products whatsoever. Now click or page up/down away from this new record. You just created a new order without a single item having been ordered. Not something a user should be able to do.
8
3087
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 the values, but when I update Request.form does not return me anything for disabled fields. How to read disabled fields at the server side
2
1900
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 than just one form) ... I need in the textbox for the new edit form to have the user type in a item# then the form populates. I'm having tremedous trouble with the logic of this. Thanks!!! Dav
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.