我是c#的初學者,需要一些幫助。加載窗體後,我想在單擊鼠標時在鼠標的窗體座標上顯示。點擊可以在表格之外進行。例如在瀏覽器中。有人可以幫我弄這個嗎。如何獲取鼠標點擊時的座標
0
A
回答
0
我覺得你不能輕易地在你的Form
以外處理鼠標點擊。 裏面的表格使用MouseEventArgs
它可以簡單地處理。
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
// e.Location.X & e.Location.Y
}
在Mouse Events in Windows Forms瞭解關於此主題的更多信息。
我希望它有幫助。
0
Cursor.Position
和Control.MousePosition
都返回鼠標光標在屏幕座標中的位置。
以下文章處理捕獲Global
鼠標點擊事件:
Processing Global Mouse and Keyboard Hooks in C#
Global Windows Hooks
1
也許最簡單的方式是一種形式的Capture
屬性設置爲true
,然後處理單擊事件和轉換位置(這是與形式的左上角相關的位置)使用PointToScreen
形式的方法來屏幕位置。
例如,你可以把一個按鈕的形式和做:
private void button1_Click(object sender, EventArgs e)
{
//Key Point to handle mouse events outside the form
this.Capture = true;
}
private void MouseCaptureForm_MouseDown(object sender, MouseEventArgs e)
{
this.Activate();
MessageBox.Show(this.PointToScreen(new Point(e.X, e.Y)).ToString());
//Cursor.Position works too as RexGrammer stated in his answer
//MessageBox.Show(this.PointToScreen(Cursor.Position).ToString());
//if you want form continue getting capture, Set this.Capture = true again here
//this.Capture = true;
//but all clicks are handled by form now
//and even for closing application you should
//right click on task-bar icon and choose close.
}
但更正確的(略難)的方法是使用全局鉤子。
如果你真的需要做到這一點,你可以在這個鏈接看看:
+0
儘管我的答案不僅有一種方法,但它也是一個很好和簡單的答案。 –
0
你需要一個全球性的鼠標鉤子。
相關問題
- 1. 獲取鼠標屏幕座標點擊
- 2. 如何根據鼠標點擊獲取3D中點的座標?
- 3. 如何顯示圖片並獲取鼠標點擊座標
- 4. MATLAB如何讓鼠標點擊座標
- 5. CodedUi:鼠標點擊座標
- 6. 如何獲取鼠標的座標?
- 7. 如何獲取點擊的座標?
- 8. OpenGL在C++中鼠標點擊時獲得光標座標
- 9. Three.js - 獲取鼠標點擊的X,Y和Z座標
- 10. 用鼠標點擊獲取圖像地圖的座標
- 11. 獲取每次點擊的鼠標座標
- 12. 獲取與Xlib的鼠標點擊座標
- 13. 如何獲取鼠標座標和.push()?
- 14. JFreeChart獲取鼠標座標
- 15. 在C#中單擊鼠標獲取鼠標座標
- 16. JavaScript的鼠標點擊座標
- 17. 在SDK中首次獲取鼠標座標右鍵點擊
- 18. 又一次:點擊移動圖像後獲取鼠標座標
- 19. 獲取座標matplotlib情節圖python與鼠標點擊
- 20. 用鼠標點擊獲取X/Y座標
- 21. Swift - 在UIElement中獲取鼠標點擊座標
- 22. 獲取鼠標點擊座標在Python烏龜
- 23. 如何獲取轉換的WPF控件上鼠標點擊的座標?
- 24. 如何在鼠標上獲取控制座標單擊? C#
- 25. 閱讀鼠標座標點擊OpenGL
- 26. 記錄鼠標點擊座標
- 27. 蟒蛇列表鼠標點擊座標
- 28. 點擊熱圖和鼠標座標! - Javascript
- 29. 鼠標點擊無法提供座標
- 30. 座標從鼠標點擊比較
這是正確的,但如果他想處理click事件? (他說) –
@MohammadChamanpara的P/Invoke https://msdn.microsoft.com/en-us/library/ms646262.aspx(SetCapture)。編輯:我已經添加鏈接到文章,處理'全球'輸入事件。 – matteeyah