[英]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.