Добавьте к форме таймер с разумным интервалом (возможно, 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()
- это обработчик события Tick для Timer в форме. Нет необходимости иметь дополнительные обработчики событий для других элементов управления в форме. Это сделает вашу форму "одной гигантской кнопкой":)
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;
}
}