如何在Windows Form Application中获取发件人的位置(坐标)?

How to get the senders' position (coordinates) in Windows Form Application?

我刚开始学习 WinForms,目前对如何获得 senders'(鼠标)的位置(坐标)感到困惑。我尝试搜索但无济于事。

这是我的某种尝试,但不幸的是,它以错误告终:

private: System::Void pictureBox1_MouseHover(System::Object^  sender, System::EventArgs^  e) {
    this->pictureBox1->Location = System::Drawing::Point(sender::Position.X - 5, sender::Position.Y - 5);
    MessageBox::Show("Foo", "Bar", MessageBoxButtons::OK, MessageBoxIcon::Stop);
}

所以我的问题很清楚,我想:我怎样才能获得 senders' 位置(在本例中为鼠标')。解释也会有所帮助。谢谢。

因为我没有找到有效的答案,所以我选择了更长的路线。

首先,我在namespace中声明了一个boolean,值为false(当鼠标触摸到图片时会变为true)。然后我创建了两个新方法:一个获取鼠标的 XY 并在鼠标触摸图片时执行代码,第二个判断鼠标是否触摸图片.

private: System::Void picture_MouseMove(Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
    int VMouseX = e->X,
        VMouseY = e->Y;
    if (VMouseEntered) {
        VMouseEntered = false;
        this->picture->Location = System::Drawing::Point(VMouseX + 17, VMouseY + 17);
    }
}

private: System::Void picture_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
    VMouseEntered = true;
}

然后,我为图片创建两个新的EventHandlers。第一个EventHandler是监听鼠标移动,第二个是检测鼠标是否在触摸图片

this->picture->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::picture_MouseMove); // Checks for mouse movement.
this->picture->MouseEnter += gcnew System::EventHandler(this, &Form1::picture_MouseEnter); // Checks whether the mouse is touching the picture.

完成。我希望这会对某人有所帮助。