While studying C# ,one of my friends had an idea to write  a C# program that enables user to make a free drawing shapes using C# graphics

so i have open my compiler ,have a moment of silence and started to code

My idea was to get the point when the user first clicked on the form,then while he is holding down the mouse button & moving i have to get the distance between the first position the mouse was clicked & the current position (width& height argument).

Lest’s start coding,

to get the first moue click position here we got two data members to store the first position,also w have a boolean data member to know if the mouse is still down or not

bool paintThis = false;
int x;
int y;

then in the mouse down event we get these two guys x&y of the first position and set the paintThis to true

 

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
paintThis = true;
x = e.X;
y = e.Y;

}

then in the mouse up event we set the paintThis to false

 

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
paintThis = false;
}

 

here in the mouse move event all the work will be done,we’ll calculate the width & the height of the rectangle we want to draw and start drawing when the mouse is down(actually when paintThis=true ) at the first position the mouse was down (x & y)

 

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (paintThis)
{
Refresh();
Graphics gf = this.CreateGraphics();
gf.DrawRectangle(new Pen(Color.Black), x,y, e.X-x, e.Y-y);
}
}

 

here the source files    http://www.mediafire.com/?mbr69mixi76vimh

hope you have enjoyed

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s