如何在C#中执行鼠标单击事件,并说明其过程 [英] How to do mouse click event in c#,tell the procedure for it

查看:192
本文介绍了如何在C#中执行鼠标单击事件,并说明其过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

告诉我有关在单击鼠标时处理事件的过程

Tell me procedure to handle a event on mouse click

推荐答案

请查看我对问题的评论:您未指定要使用的UI库,抱歉,没有确切的参考.您将可以轻松地自己找到它们.简而言之,您将需要将事件处理程序添加到某个控件的某个事件的调用列表中.看起来可能像这样:
Please see my comment to the question: you did not specify what UI library you will use so, sorry, no exact references. You will easily find them by yourself. In short, you will need to add a event handler to an invocation list of some event of some control. It can look like that:
Control myControl = // some concrete control type
Button myButton = //...

//...

myControl.MouseDown += (sender, eventArgs) => { DoItWhenMousePressedDown(); };
myControl.MouseUp += (sender, eventArgs) => { DoItWhenMouseReleased(); };
myControl.MouseMove += (sender, eventArgs) => { DoItWhenMouseMoved(eventArgs.X, evenArgs.Y); };
myButton.Click += (sender, eventArgs)  => { DoItWhenButtonClicked(); };



这样,您的方法(您可以自行定义,也可以在上面显示的"{}"块中编写其他代码)DoItWhenMousePressedDownDoItWhenMouseReleasedDoItWhenMouseMovedDoItWhenButtonClicked在控件上以相应方式操作鼠标(或键盘,如果Click的话.)

请注意,Click事件是更抽象的,与硬件没有直接关系:它将由鼠标单击(Enter键或空格键)触发,具体取决于键盘焦点.

参数eventArgs将包含其他信息:鼠标坐标,单击了哪个鼠标按钮等.您需要从MSDN帮助页面上了解有关事件的全部信息.对于不同的.NET UI库,这部分是不同的(再次,为什么不标记它呢?这很重要;谁会浪费时间为所有不同的库解释它?),但是有很多地方类似功能.

到目前为止,这只是一个食谱.除非您非常彻底地了解事件的工作方式,否则这对您来说是不够的.这个非常重要.您还应该非常了解委托和匿名方法.

—SA



This way, your methods (which you can define by yourself, or you can write some other code in "{ }" blocks shown above) DoItWhenMousePressedDown, DoItWhenMouseReleased, DoItWhenMouseMoved or DoItWhenButtonClicked will be called when the user operates mouse (or keyboard, in case of Click in correspondent way over your control.

Note that the Click event is more abstract, not directly related to hardware: it will be triggered by mouse click, be Enter or spacebar key, depending on keyboard focus.

The parameter eventArgs will carry additional information: mouse coordinates, which mouse button clicked, etc. You will need to learn if all from the MSDN help pages on relevant events. This part is different for different .NET UI libraries (again, why didn''t you tag it? it''s very important; and who will waste time for explaining it for all different libraries?), but there a lot of very similar feature.

So far, this is just a cookbook recipe. This is not good enough for you unless you learn very thoroughly how events work. This is very important. You should also understand very well the delegates and anonymous methods.

—SA


这篇关于如何在C#中执行鼠标单击事件,并说明其过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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