[英]How to get the senders' position (coordinates) in Windows Form Application?
我刚刚开始学习WinForms ,目前对如何获取发件人 (鼠标)位置(坐标)感到困惑。 我尝试搜索,但无济于事。
这是我的尝试,但不幸的是,它最终出现错误:
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);
}
因此,我认为我的问题很清楚:我如何获得发件人的位置(在本例中为鼠标的位置)。 解释也将有所帮助。 谢谢。
由于找不到有效答案,因此我选择了更长的路线。
首先,我在namespace
声明了一个boolean
值,其值为false
(当鼠标触摸图片时,它将变为true)。 然后,我创建了两种新方法:一种用于获取鼠标的X
和Y
并在鼠标触摸图片时执行代码,另一种用于确定鼠标是否在触摸图片。
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
用于侦听鼠标的移动,第二个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.
做完了 我希望这会对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.