I just began learning WinForms and am currently baffled on how to get the senders' (mouses') position (coordinates). I tried searching but to no avail.
This is my, somewhat, of a try but, sadly, it ended up with an error:
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);
}
So my question here is quite clear, I think: how can I get the senders' position (in this case, the mouses'). Explanations would also be of help. Thank you.
Since I didn't find a valid answer I took the longer route.
Firstly, I declared a boolean
in the namespace
with the value of false
(it will change to true when the mouse will touch the picture). Then I create two new methods: one to get the X
and Y
of the mouse and execute code if the mouse is touching the picture and the second one to determine whether the mouse is touching the picture or not.
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;
}
Then, I create two new EventHandlers
for the picture. The first EventHandler
is to listen for mouse movement, the second one is to check whether the mouse is touching the picture.
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.
Done. I hope that this will help someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.