Hi
I want in runtime user could draw a line or circle and then when it finish could move it(i mean when clicked on it could move it)in windowsapplication
how it is possible?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Aug 20, 2009, 9:18 AM
I included Project Files Take a Look At it :)
namespace WindowsFormsApplication38
{
public partial class Form1 : Form
{
private bool dragging;
private Pen penFore, penBack;
private Rectangle rect;
private Graphics g;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dragging = false;
penBack = new Pen(this.BackColor);
penFore = Pens.Black;
rect = new Rectangle(100, 100, 100, 100);
g = this.CreateGraphics();
}
private void button1_Click(object sender, EventArgs e)
{
g.DrawRectangle(penFore, rect);
button1.Enabled = false;
button1.Visible = false;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (rect.Contains(e.Location))
{
dragging = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
g.DrawRectangle(penBack, rect);
rect.Location = e.Location;
g.DrawRectangle(penFore, rect);
}
}
}
}
MaharajePosted Aug 20, 2009, 10:47 AM
That's Correct what i want
Thanks alot
Honesty Pays
MaharajePosted Aug 19, 2009, 7:38 AM
THank you
Kirtan PatelPosted Aug 19, 2009, 7:25 AM
Graphics g;
private void button1_Click(object sender, EventArgs e)
{
//Select Random Point to Draw A Circle
Random r = new Random();
int x = r.Next(20, 100);
int y = r.Next(30, 50);
DrawCircle(x, y, 400, 400);
}
public void DrawCircle(int x,int y,int height,int width)
{
g = panel1.CreateGraphics();
Pen p = new Pen(new SolidBrush(Color.Red));
Rectangle rect = new Rectangle(x,y,height,width);
g.DrawEllipse(p, rect);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
}
MaharajePosted Aug 19, 2009, 7:05 AM
MaharajePosted Aug 19, 2009, 6:41 AM
it was good but i need in run time user draw it and could move it in runtime not on code behind
Kirtan PatelPosted Aug 19, 2009, 6:09 AM
This is not Exactly you want but i have try to do so .
it moves Circle Drawn By Graphic Object by Mouse may be it can be helpful to you :)
don't forget to mark "Do you like Answer" it will give me some credits :)
namespace ObjectMove
{
public partial class Form1 : Form
{
private int x = 0;
private int y = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen RedPen = new Pen(Brushes.Red);
e.Graphics.DrawEllipse(RedPen, x - 150, y - 150, 300, 300);
}
}
}
MaharajePosted Aug 19, 2009, 5:51 AM
it doesn't work
i need a clear code or a simple sample
Thank you