鼠标检测随处可见 [英] Mouse Detection everywhere

查看:90
本文介绍了鼠标检测随处可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道光标是否在我的表格之外。

How do I know if the cursor is outside of the my form.

推荐答案

因为你知道表格的坐标总是在屏幕坐标中,你知道MousePosition将在屏幕坐标中返回鼠标的当前位置,无论它在哪里,然后看看鼠标是否在给定的 Form 中是非常简单的:
Since you know that a Form's co-ordinates are always in screen co-ordinates, and you know that MousePosition will return the current location of the Mouse in screen co-ordinates, no matter where it is, then seeing if the Mouse is inside a given Form is pretty simple:
private bool MouseInForm(Form theForm)
{
    return(theForm.Bounds.Contains(MousePosition));
}

所有控件和表单都有一个Bounds属性,它是一个矩形。并且,对于Controls,Bounds总是返回一个Rectangle,它的左,上,右和底值(当然是整数)是 relative 偏移到它们的容器(它们的Parent's)左上角。



但是,对于一个Form,没有Parent(它的Parent,总是,内部:null),Bounds将返回屏幕坐标...除非:



1.你犯了一个把表格放在另一个表格或控制中的可怕错误;在这种情况下,它的父母是它的容器,你应该认真考虑自杀,除非这是一个非常高薪的工作或客户要求你。



2你有不得不使用或工作MDI架构的WinForm实现。在这种情况下,作为MDI ChildForm的每个Form都将作为其Parent的MDIClient虚拟窗口,其MDIParentForm由该架构的偷偷摸摸的低层方法创建。



家庭作业:



1.研究并了解控件和表格的ClientRectangle,DisplayRectangle属性以及它们的值与返回值的不同之处由Bounds属性。创建一些嵌套控件(如面板中面板中的面板),并检查这三个矩形返回属性返回的每个控件的值。





光标:这可能是一个比获取鼠标更有趣的区域,我需要了解你可能需要/使用什么来找出什么 Cursor在任何给定的时刻都是。



就像,当你与表格上的任何控件进行交互时,你知道鼠标是在形式:当你与表格上的不同控件互动时,光标可能会改变;当你在TextBox中单击并且光标变成工字梁时。



但是,要开始,请查看CursorChanged事件,您可以定义它适用于任何表单或控件的EventHandler。



然后,看看Cursor.Current方法,它类似于MousePosition,是一个静态的控件方法类:可以在任何地方使用它来获取当前的Cursor:如果光标不可见,Cursor.Current将返回null。



如果你想知道什么当焦点位于正在运行的应用程序之外时,当前的Cursor是,或检测光标更改事件...在桌面上,在另一个应用程序的窗口中:那么你将不得不使用像Global Hook这样的东西。



挑战:给出屏幕坐标中当前鼠标位置的值,以及鼠标在表格中的信息:概念化您需要做什么来确定是否老鼠结束了只有表格中没有任何控件涵盖的表格区域,或表格中的控制范围内。

All Controls, as well as Forms, have a Bounds property which is a rectangle. And, for Controls, Bounds always returns a Rectangle whose left, top, right, and bottom values (integers, of course) which are relative offsets to their container's (their "Parent's") upper left corner.

But, for a Form, having no Parent (its Parent, is always, internally: null), Bounds will return screen co-ordinates ... unless:

1. you made the horrible mistake of putting a Form inside another Form or Control; in that case, its Parent is its container, and you should seriously consider suicide, unless this is what a very high-paying job or client demands of you.

2. you have the mis-fortune to have to use, or work on, an MDI architecture WinForm implementation. In that case every Form which is an MDI ChildForm will have as its Parent the MDIClient "virtual window" of its MDIParentForm created by that architecture's sneaky low-down methods.

Homework:

1. study and understand what the ClientRectangle, and DisplayRectangle properties of Controls, and Forms are, and how their values may differ from the values returned by the Bounds property. Create some nested Controls (like a Panel in a Panel in a Panel), and examine the values, for each of the Controls returned by these three "Rectangle returning" properties.


Cursor: this is potentially a more interesting area than getting where the Mouse is, and I'd need to understand what your possible need/use of finding out what the Cursor is at any given moment in time is.

Just as, when you interact with any Control on a Form, you know the Mouse is "in" the Form: as you interact with different Controls on the Form, the Cursor may change; as when you click in a TextBox and the Cursor becomes the I-Beam.

But, to get started, take a look at the CursorChanged Event, which you can define an EventHandler for, for any Form, or Control.

And, take a look at the Cursor.Current method, which like MousePosition, is a static method of the Control Class: it can be used anywhere to get the current Cursor: if the cursor is not visible, Cursor.Current will return null.

If you are thinking of knowing what the current Cursor is, or of detecting Cursor change Events when focus is outside of your running application ... on the Desktop, in another application's windows: then you are going to have to use something like a Global Hook.

Challenge: given a value for the current mouse position in screen-coordinates, and the information that the mouse is within a Form: conceptualize what you would have to do to determine whether the mouse was over only the area of the Form not covered by any Control, or within a Control on the Form.


这篇关于鼠标检测随处可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆