将面板位置设置为鼠标位置

Set a Panel Location to Mouse Position

我有问题,我想将面板位置设置为鼠标位置。一切都是 "fine" 但发生了这种情况:

https://i.stack.imgur.com/KdnMZ.gif

我希望面板位置在鼠标的中心。我该如何解决?我正在使用 Visual Studio。我的代码:

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

namespace Blocker
{
    public partial class Form1 : Form
    {

        Panel Selection_Box = new Panel();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Selection_Box.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            Selection_Box.Location = System.Windows.Forms.Control.MousePosition;
            Selection_Box.BackColor = Color.FromArgb(0, 159, 227);
            Selection_Box.Size = new Size(70, 70);
            Selection_Box.Name = "Selection_Box";
            this.Controls.Add(Selection_Box);

        }

        private void Form_MouseMove(object sender, MouseEventArgs e) 
        {

            Selection_Box.Location = System.Windows.Forms.Control.MousePosition;

        }

    }
}

System.Windows.Forms.Control.MousePosition returns 屏幕坐标中的位置。 您需要使用 PointToClient.

将其转换为客户坐标
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    Selection_Box.Location = PointToClient(MousePosition);
}