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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 20, 2009, 5:18 AM EDT
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);
}
}
}
}
Maharaje124 reputationPosted Aug 19, 2009, 1:51 AM EDT
it doesn't work
i need a clear code or a simple sample
Thank you
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 19, 2009, 2:09 AM EDT
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);
}
}
}
Maharaje124 reputationPosted Aug 19, 2009, 2:41 AM EDT
it was good but i need in run time user draw it and could move it in runtime not on code behind
Maharaje124 reputationPosted Aug 19, 2009, 3:05 AM EDT
Graphics g; private void Form2_Load(object sender, EventArgs e) { g = panel1.CreateGraphics(); } private void btnDraw_Click(object sender, EventArgs e) { g.DrawLine(Pens.Red, 27, 100, 450, 90); }Kirtan PatelRank #292 · 8.6K reputationPosted Aug 19, 2009, 3:25 AM EDT
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;
}
Maharaje124 reputationPosted Aug 19, 2009, 3:38 AM EDT
THank you
Maharaje124 reputationPosted Aug 20, 2009, 6:47 AM EDT
That's Correct what i want
Thanks alot
Honesty Pays