简体   繁体   English

如何计算C#屏幕上任意位置的点击次数?

[英]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.

相关问题 Unity2d(C#)如何检测玩家何时点击屏幕上的任意位置? - Unity2d (C#) how to detect when a player clicks anywhere on the screen? 我如何在C#中计算背景中的按键数 - How can i count the number of keypress in the background in C# 如何在C#中计算列表上相同值的数量 - How can I count the number of the same value on list in c# 计算点击次数并显示在另一页上[Windows Phone c#] - Count number of clicks and display on another page [Windows Phone c#] 如何使用 c# 计算句子中的单词数? 我刚刚开始学习这门语言 - How can I count the number of words in a sentence using c#? I am just starting to learn this language 如何在WPF中获取数字以在屏幕上向前计数? - How can I get a number to count forward on the screen in WPF? c# 检测任意位置的鼠标点击(表单内部和外部) - c# Detect mouse clicks anywhere (Inside and Outside the Form) 如何从其他 class 获取帐号和密码并在 c# 中查看登录屏幕 - How can I get the account number and password from the other class and check them for the login screen in c# 在C#中,如何遍历整个2D数组并计算0的数量? - In C#, How can I iterate through a entire 2D Array and count the number of 0s? 如何使用C#获取Excel中图表中系列总数的计数? - How can I get a count of the total number of series in a chart in excel using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM