2010-05-20 81 views

回答

0

正如有人指出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; 
     } 

    } 
} 
3

一個開始的地方是檢查ClientRectangle是否包含當前的鼠標位置。因此,舉例來說,你的MouseMove處理程序,你可以有:

if (ClientRectangle.Contains(e.Location)) 
{ 
    bool mouseIsOverThisControl = true; 
} 
+0

將這項工作對於整個表格,壽多少東西?我可以看到這將如何捕獲MouseEnter事件,但是當鼠標離開窗體時,它會停止調用MouseMove事件,因此它不會檢查它是否離開窗體,否則不會。 – dlras2 2010-05-20 14:43:43

+0

你什麼時候需要知道鼠標在你的表單上? – bentsai 2010-05-20 14:57:58

+0

整個表單就像一個巨大的按鈕。 (這是一個來電的通知。)因此,當用戶鼠標在表格上時,我想讓它點亮,表示按下它將接聽電話。 – dlras2 2010-05-20 17:36:06

0

添加一個計時器到形式合理區間(也許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; 
    } 
} 
+0

-1:請不要這樣做。 SlavaGu給出了一個非常好的答案..explore Api調用,如果你一定要。只是不要這樣做。永遠。 – Rusty 2010-05-21 16:52:11

0

我發現了幾個接近我想要的答案,但我完成了一些不同的事情。我想檢測,如果鼠標左鍵的形式區(包括標題欄),這爲我工作:

在窗體構造函數中,我添加了一個計時器:

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; 
    } 
} 
0

通過窗體和窗體控件執行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"); 
     } 
    } 

這可能會很麻煩,如果你有過的形式

相關問題