我在整個表格中計算出how to capture mouse clicks,但這種方法對於MouseEnter
和MouseLeave
的轉換效果不好。我的表單佈局由許多和TableLayoutPanels
組成,所以沒有可以監控事件的全部控件,顯然按鈕的MouseLeave
事件並不意味着光標離開了整個表單。有沒有人想出瞭解決這個問題的好方法?什麼是最好的方式來判斷鼠標是否在表格之上?
回答
正如有人指出here有可能使用調用SetWindowsHookEx()或只是掛鉤MouseMove事件到表單中的所有控件。後者對我很好。唯一的缺點是,如果您在運行時添加/刪除控件,則可能需要另一個解決方案。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsForms_MouseEvents
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MouseMove += OnMouseMove;
MouseLeave += OnMouseLeave;
HookMouseMove(this.Controls);
}
private void HookMouseMove(Control.ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
ctl.MouseMove += OnMouseMove;
HookMouseMove(ctl.Controls);
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
BackColor = Color.Plum;
Control ctl = sender as Control;
if (ctl != null)
{
// Map mouse coordinate to form
Point loc = this.PointToClient(ctl.PointToScreen(e.Location));
Console.WriteLine("Mouse at {0},{1}", loc.X, loc.Y);
}
}
private void OnMouseLeave(object sender, EventArgs e)
{
BackColor = Color.Gray;
}
}
}
一個開始的地方是檢查ClientRectangle是否包含當前的鼠標位置。因此,舉例來說,你的MouseMove處理程序,你可以有:
if (ClientRectangle.Contains(e.Location))
{
bool mouseIsOverThisControl = true;
}
添加一個計時器到形式合理區間(也許50毫秒)。使用此代碼在Tick事件處理程序,以查看是否鼠標在形式:
// Check if mouse is currently over the form
bool temp_mof = ClientRectangle.Contains(
Form.MousePosition.X - Location.X,
Form.MousePosition.Y - Location.Y);
編輯:這裏是檢測鼠標在形式和該按鈕被點擊了更完整的解決方案。 timer1Tick()
是窗體上Timer的Tick事件處理程序。沒有必要爲表單上的其他控件添加額外的事件處理程序。這將使你的形式「一個巨大的按鈕」 :)
bool m_mouse_over_form = false;
// Assume the left button is down at onset
bool m_left_button_down = true;
void timer1Tick (object sender, EventArgs e)
{
// Check if mouse is currently over the form
bool temp_mof = ClientRectangle.Contains(
Form.MousePosition.X - Location.X,
Form.MousePosition.Y - Location.Y);
// were we already over the form before this tick?
if (temp_mof && m_mouse_over_form)
{
// we need to detect the mouse down and up to avoid
// repeated calls if the mouse button is held down for more
// than our Tick interval
// was the mouse button up prior to now?
if (!m_left_button_down)
{
// is the button down now?
m_left_button_down = (MouseButtons == MouseButtons.Left);
if (m_left_button_down)
{
// the button was down and has now been released
LeftButtonClickHandler();
}
else
{
// do nothing, the button has not been release yet
}
}
else
{
// update the button state
m_left_button_down = (MouseButtons == MouseButtons.Left);
}
}
else if (temp_mof)
{
// the mouse just entered the form
m_mouse_over_form = true;
// set the initial state of the left button
m_left_button_down = MouseButtons == MouseButtons.Left);
}
else
{
// the mouse is not currently over the form
m_mouse_over_form = false;
m_left_button_down = true;
}
}
-1:請不要這樣做。 SlavaGu給出了一個非常好的答案..explore Api調用,如果你一定要。只是不要這樣做。永遠。 – Rusty 2010-05-21 16:52:11
我發現了幾個接近我想要的答案,但我完成了一些不同的事情。我想檢測,如果鼠標左鍵的形式區(包括標題欄),這爲我工作:
在窗體構造函數中,我添加了一個計時器:
time.Interval = 250;
time.Tick += time_Tick;
time.Start();
然後在蜱方法,我執行以下操作:
void time_Tick(object sender, EventArgs e)
{
switch (RectangleToScreen(Bounds).Contains(PointToScreen(Cursor.Position))) {
case true:
if (Opacity != .9999D)
Opacity = .9999D;
break;
case false:
if (Opacity != .5D)
Opacity = .5D;
break;
}
}
通過窗體和窗體控件執行MouseEnter和MouseLeave事件;使用布爾值來確定鼠標是進入還是離開。
一個例子是
private static bool mouseEnteredForm
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
mouseEnteredForm = true;
Form.MouseLeave += Form1_MouseLeave;
CheckMouseLocation();
}
private void Form1_MouseLeave(object sender, MouseEventArgs e)
{
mouseEnteredForm = false
CheckMouseLocation();
}
private static void CheckMouseLocation()
{
if(!mouseOverForm)
{
MessageBox.Show("Mouse Not Over Form!);
}
else if(mouseOverForm) //else if is optional. You could also use else in this case. I used else if for the sake of the example.
{
MessageBox.Show("Mouse Is Over Form");
}
}
這可能會很麻煩,如果你有過的形式
- 1. 什麼是最好的方式來判斷一個方法是否是策略注入中的一個屬性?
- 2. 將鼠標置於表單上的最佳方式是什麼?
- 3. 什麼是最好的方式來保持主鍵在網格
- 4. 什麼是最有效的方式來判斷一個字符是否在PHP中是字母數字?
- 5. 斷言numpy.array是否相等的最好方法是什麼?
- 6. 什麼是最好的方法來判斷總線是否包含verilog中的單個x?
- 7. 用什麼方法來判斷用戶是否連接到facebook
- 8. 什麼是最快的方式來判斷我們是否在特定的小時範圍內工作?
- 9. 什麼是最好的方式來清理URL中的標題
- 10. 什麼是最好的方式,以確定是否NamedDataSlot存在
- 11. 什麼是最好的方式來搜索SQL與多個表?
- 12. 如何判斷是否存在W.I.F標記? - 最佳方法
- 13. 什麼是有效的方法來判斷位圖是否全黑?
- 14. 什麼是簡單的方法來判斷一行數據是否已更改?
- 15. 什麼是最好的方式來追加HTML只是一次?
- 16. 任何方式來判斷Oracle導出是否仍在運行?
- 17. 在網頁上寫上標的最好方法是什麼?
- 18. 什麼是最好的表格結構?
- 19. 什麼是最好的方式返回
- 20. 什麼是最好的方式來存儲是否已閱讀rss項目
- 21. 什麼是最好的方式來保存在骨幹集合?
- 22. 什麼是最好的方式來查詢這在貓鼬?
- 23. 如何判斷鼠標指針圖標是否改變
- 24. 用鼠標不斷調整元素大小的最佳方式是什麼?
- 25. 什麼是最好的方式來包裝第三方類c#
- 26. 什麼是最簡單的方式來製作座標列表?
- 27. 最好的方式來判斷是否在.NET的生產環境或開發環境中
- 28. 任何方式來判斷DuplexClientBase是否忙?
- 29. 使用鼠標創建地圖的最佳方式是什麼?
- 30. 什麼是git命令來判斷我的repo目前是否在特定提交之前或之後?
將這項工作對於整個表格,壽多少東西?我可以看到這將如何捕獲MouseEnter事件,但是當鼠標離開窗體時,它會停止調用MouseMove事件,因此它不會檢查它是否離開窗體,否則不會。 – dlras2 2010-05-20 14:43:43
你什麼時候需要知道鼠標在你的表單上? – bentsai 2010-05-20 14:57:58
整個表單就像一個巨大的按鈕。 (這是一個來電的通知。)因此,當用戶鼠標在表格上時,我想讓它點亮,表示按下它將接聽電話。 – dlras2 2010-05-20 17:36:06