我刚刚开始学习 WinForms ,目前我对如何获取发件人'(mouses')位置(坐标)感到困惑。我试着搜索但无济于事。
这是我的尝试,但遗憾的是,它最终出现了错误:
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);
}
所以我的问题很清楚,我认为:我怎样才能获得发件人'职位(在这种情况下,是鼠标)。解释也会有所帮助。谢谢。
答案 0 :(得分:0)
由于我没有找到有效的答案,我采取了更长的路线。
首先,我在boolean
中声明了namespace
,其值为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
是监听鼠标移动,第二个是检查鼠标是否正在触摸图片。
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.
完成。我希望这会对某人有所帮助。