创建一个草图,其中的一部分可以在运行时使用箭头键移动

问题描述 投票:0回答:1

我有一个非常旧的软件,没有源代码,我什至不确定使用什么语言来开发它。我正在尝试使用 Windows 窗体 (C#) 复制它。

我偶然发现了一个我不知道如何复制的功能。下面显示的草图有一些小方块,可以用鼠标选择并使用箭头键移动。这些方块的位置也链接到文本框。如果它们的位置发生变化,文本框中的值也会发生变化,反之亦然。在下图中,我用红色箭头指向其中一个方块,并用红色框将其相应的文本框框起来。

运动前:

Before movement of square

运动后:

After movement of square

我不确定如何复制此功能。我倾向于相信这些正方形必须是草图顶部的单独对象,而不是草图的一部分。换句话说,需要创建一个自定义 winform 控件,可以使用鼠标选择该控件并使用箭头键移动该控件。我的思考方向正确吗还是有更好的方法来解决这个问题?如果这是正确的方法,那么我该如何创建具有此类属性的自定义控件?

c# winforms controls
1个回答
0
投票

假设这些小方块是彩色按钮,我实现了你提到的功能。只需稍加修改即可实现相同的效果。 Here is an image of a simple form that I created to implement the feature asked

三个红色按钮是可选的,点击后会变成绿色,选中后可以用箭头键移动。没有越界逻辑实现,但很容易实现。编写的代码是为了使其易于理解,因此没有进行大小优化。 下面是Form1的代码实现:

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private List<Button> buttonList = new List<Button>();
        private List<TextBox> textboxList = new List<TextBox>();
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
            buttonList.Add(button1);
            buttonList.Add(button2);
            buttonList.Add(button3);
            textBox1.Text = button1.Location.ToString();
            textBox2.Text = button2.Location.ToString();
            textBox3.Text = button3.Location.ToString();
            textboxList.Add(textBox1);
            textboxList.Add(textBox2);
            textboxList.Add(textBox3);
            buttonList.ForEach(button =>
            {
                button.PreviewKeyDown += new PreviewKeyDownEventHandler(button_PreviewKeyDown);
                button.LocationChanged += new EventHandler(OnButtonLocationChange);
            });
            this.KeyDown += new KeyEventHandler(Form1_KeyPress);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button_logic(sender, e);
        }
        private void OnButtonLocationChange(object sender, EventArgs e)
        {
            textboxList[buttonList.IndexOf((Button)sender)].Text = ((Button)sender).Location.ToString();
        }
        private void Form1_KeyPress(object sender, KeyEventArgs e)
        {
            if (buttonList.FirstOrDefault(x => x.BackColor == Color.Green) == null)
                return;
            if (e.KeyCode == Keys.Up)
            {
                Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
                buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X, tempButton.Location.Y - 10);
            }
            else if (e.KeyCode == Keys.Down)
            {
                Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
                buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X, tempButton.Location.Y + 10);
            }
            else if (e.KeyCode == Keys.Left)
            {
                Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
                buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X - 10, tempButton.Location.Y );
            }
            else if (e.KeyCode == Keys.Right)
            {
                Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
                buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X + 10, tempButton.Location.Y);
            }
        }
        private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            var keys = new[] { Keys.Left, Keys.Right, Keys.Up, Keys.Down };
            if (keys.Contains(e.KeyData))
                e.IsInputKey = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button_logic(sender, e);
            this.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button_logic(sender, e);
        }
        private void button_logic(object sender, EventArgs e)
        {
            foreach(Button button in buttonList)
            {
                if((Button)sender == button)
                {
                    button.BackColor = Color.Green;
                }
                else
                {
                    button.BackColor = Color.Red;
                }
            }
            this.Focus();
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.