C#使用鼠标动态移动Picturebox类

时间:2018-11-20 05:11:23

标签: c# dynamic picturebox

我目前正在尝试制作一个视频游戏级编辑器,并试图将其放置在用户可以将对象添加到屏幕上,然后操纵其位置和大小以及其他内容的位置上。我首先拥有一个平台类,这是我用来允许用户编辑平台位置的方法:

<TextView
    android:background="@drawable/border_style"
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:text="1. If you somehow found a way to extract all of the gold from the 
     bubbling core of our lovely little planet, you would be able to cover all of the 
     land in a layer of gold up to your knees."
    android:textSize="24sp"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.96"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    app:layout_constraintVertical_bias="1.0" />

问题是它不起作用。我知道using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LevelEditor { class Platform : PictureBox { private PictureBox platform = new PictureBox(); public Platform(int width, int height, int x, int y) { platform.Width = width; platform.Height = height; platform.Location = new Point(x, y); platform.BackColor = Color.Red; platform.BorderStyle = BorderStyle.FixedSingle; } public void drawTo(Form form) { form.Controls.Add(platform); } public void setPosition(int x, int y) { platform.Location = new Point(x, y); } private Point MouseDownLocation; protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button == MouseButtons.Left) { MouseDownLocation = e.Location; } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { platform.Left = e.X + platform.Left - MouseDownLocation.X; platform.Top = e.Y + platform.Top - MouseDownLocation.Y; } } } } 甚至没有检测到MouseDown事件,因为我已经对其进行了调试。我不确定问题出在哪里,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

正如评论中所指出的,问题是我从Picturebox继承而来,但是使用了Picturebox变量的实例作为对象本身。正确的代码是这样的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LevelEditor
{
    class Platform : PictureBox
    {

        public Platform(int width, int height, int x, int y)
        {
            this.Width = width;
            this.Height = height;
            this.Location = new Point(x, y);
            this.BackColor = Color.Red;
            this.BorderStyle = BorderStyle.FixedSingle;
        }

        public void drawTo(Form form)
        {
            form.Controls.Add(this);
        }

        public void setPosition(int x, int y)
        {
            this.Location = new Point(x, y);
        }

        private Point MouseDownLocation;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == MouseButtons.Left)
            {
                this.Left = e.X + this.Left - MouseDownLocation.X;
                this.Top = e.Y + this.Top - MouseDownLocation.Y;
            }
        }
    }
}