على الرغم من أنني في الحقيقة مجرد نسخ لصق عندما يتعلق الأمر بأشكال Windows
الكود الذي نشرته لن يفعل أي شيء في حد ذاته!
للعمل ، يجب أن تكون أحداث Mousedown () و mousemove () من السيطرة على هذه الطرق:
- حدد عنصر التحكم في النموذج (PictureBox1).
- في جزء الخصائص (أسفل اليمين بشكل افتراضي) ، انقر فوق أيقونة "Lightning Bolt" للحصول على قائمة بالأحداث لهذا التحكم.
- أعثر على
MouseDown
الدخول وتغيير المنسدلة إلى يمينهاpictureBox1_MouseDown
. - أعثر على
MouseMove
الدخول وتغيير المنسدلة إلى يمينهاpictureBox1_MouseMove
.
الآن قم بتشغيله واسحب PictureBox1.
تحرير: هنا هو كيفية جعل الرمز يعمل لضوابط متعددة ، كما هو موضح في تعليقي أدناه.
bool isDragged = false;
Point ptOffset;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Button btn = (Button)sender;
ptOffset = new Point(btn.Location.X - Cursor.Position.X, btn.Location.Y - Cursor.Position.Y);
}
else
{
isDragged = false;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = Cursor.Position;
newPoint.Offset(ptOffset);
Button btn = (Button)sender;
btn.Location = newPoint;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isDragged = false;
}