[英]How can I count number of clicks anywhere on screen in C#?
I am trying to have a C# program running in the background on Windows that will print "Hello!" 我正在尝试在Windows的后台运行一个C#程序,该程序将显示“ Hello!”。 after seeing that the user has clicked his or her mouse 10 times. 看到用户单击了10次鼠标后。 But not just in the console window, anywhere on the screen. 但不仅是在控制台窗口中,还是在屏幕上的任何地方。
The following event handler for click-tracking is from msdn.microsoft.com: 以下用于点击跟踪的事件处理程序来自msdn.microsoft.com:
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e) {
// Checks the number of clicks.
if (e.ClickCount == 1) {
// Single Click occurred.
lblClickCount.Content = "Single Click";
}
if (e.ClickCount == 2) {
// Double Click occurred.
lblClickCount.Content = "Double Click";
}
if (e.ClickCount >= 3) {
// Triple Click occurred.
lblClickCount.Content = "Triple Click";
}
}
But, I'm not sure how to actually use this. 但是,我不确定如何实际使用它。 When I add this function anywhere, the MouseButtonEventArgs
type is undefined. 当我在任何地方添加此函数时, MouseButtonEventArgs
类型都是未定义的。
What "using" statements do I need? 我需要什么“使用”语句? How do I actually get this code to run properly -- do I call it once from main
? 我实际上如何使此代码正常运行-是否可以从main
调用一次? What do I do to call it? 我该怎么称呼它?
EDIT : Here is a picture showing Visual Studio not understanding MouseButtonEventArgs: 编辑 :这是一张图片,显示Visual Studio无法理解MouseButtonEventArgs:
I'm not entirely sure what you're trying to accomplish but.. To track user clicks I hooked up the "MouseDown" event on a form in a Windows Forms applications. 我不能完全确定您要完成什么,但是..为了跟踪用户的点击,我在Windows Forms应用程序中的表单上关联了“ MouseDown”事件。 From there I check click counts in the event handler. 从那里,我检查事件处理程序中的点击计数。
using System;
using System.Windows.Forms;
namespace WindowsFormsApplicationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent( );
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
// Count clicks
}
}
}
Initially You have to select the form and go to properties, Here you have to go events area and there is MouseClick event. 最初,您必须选择表单并转到属性,在这里您必须转到事件区域,并且有MouseClick事件。 Click that Mouse click. 单击该鼠标单击。 Go to Code behind window. 转到窗口后面的代码。 there is the click event generated automatically. 会自动产生点击事件。 In that Form_MouseClick event you can count the number of clicks. 在该Form_MouseClick事件中,您可以计算点击次数。
Initially declare a variable 最初声明一个变量
int count = 0;
In method 在方法中
Private void Form_MouseClick(object sender, MouseEventArgs e)
{
count++;
//add lable which will displays the count value
label.Text=count.ToString();
}
I think which will helps to count the clicks in the form. 我认为这将有助于计算表单中的点击次数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.