
نوشته شده توسط
prankster
public partial class Form1 : Form
{
bool hasDrawn = false;
Graphics graphics;
int radius = 0;
Point center = new Point(0, 0);
Point mouseDownLocation = new Point(0, 0);
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += (sender, e) =>
{
if (!hasDrawn)
center = new Point(e.X, e.Y);
else
mouseDownLocation = new Point(e.X, e.Y);
};
pictureBox1.MouseMove += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
if (!hasDrawn)
{
graphics = pictureBox1.CreateGraphics();
radius = e.X - center.X;
DrawEllipse(center, radius);
}
else
DrawEllipse(new Point(center.X + (e.X - mouseDownLocation.X), center.Y + (e.Y - mouseDownLocation.Y)), radius);
}
};
pictureBox1.MouseUp += (sender, e) =>
{
if (!hasDrawn)
hasDrawn = true;
else
center = new Point(center.X + (e.X - mouseDownLocation.X), center.Y + (e.Y - mouseDownLocation.Y));
};
}
private void DrawEllipse(Point center, int radius)
{
graphics.Clear(pictureBox1.BackColor);
graphics.DrawEllipse(Pens.Black, center.X, center.Y, radius, radius);
}
}
عالی بود. متشکرم
حالا اگه بخواهیم یه مربع به این pictureBox اظافه کنیم که بعد از کشیدنش با کلیک روی هرکدام از اشکال (مربع یا دایره) بتوانیم آنها را جابجا کنیم.... چه باید کرد ؟