C#在任何地方按下鼠标左键时循环

C#在任何地方按下鼠标左键时循环,c#,loops,C#,Loops,我试着制作一个系统,当按下鼠标左键时,它可以随时运行循环,而不是按下按钮或标签,而是任何时候!!!。但代码如下: [DllImport(“user32.dll”)] 静态外部无效鼠标事件(int-dwFlags、int-dx、int-dy、, int dwData、int dwExtraInfo); [旗帜] 公共枚举MouseEventFlags { LEFTDOWN=0x00000002, LEFTUP=0x00000004, MIDDLEDOWN=0x00000020, MIDDLEUP

我试着制作一个系统,当按下鼠标左键时,它可以随时运行循环,而不是按下按钮或标签,而是任何时候!!!。但代码如下:

[DllImport(“user32.dll”)]
静态外部无效鼠标事件(int-dwFlags、int-dx、int-dy、,
int dwData、int dwExtraInfo);
[旗帜]
公共枚举MouseEventFlags
{
LEFTDOWN=0x00000002,
LEFTUP=0x00000004,
MIDDLEDOWN=0x00000020,
MIDDLEUP=0x00000040,
MOVE=0x00000001,
绝对值=0x00008000,
RIGHTDOWN=0x00000008,
RIGHTUP=0x00000010
}          
私有void Form1\u加载(对象发送方、事件参数e)
{
click.Enabled=false;//click是我添加到表单中的计时器!
点击,间隔=1000;
}
//它应该做循环时,鼠标左键单击我刚才按下的标签或按钮上没有!
私有void循环\u MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
单击.Enabled=true;
}
}
专用void Loop_MouseUp(对象发送器,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
单击.Enabled=false;
}
}
私有无效点击勾选(对象发送者,事件参数e)
{
鼠标事件((int)(MouseEventFlags.LEFTDOWN),0,0,0,0);
鼠标事件((int)(MouseEventFlags.LEFTUP),0,0,0,0);

}
您根本不需要循环。只需在MouseDown事件中将
click.Enabled
更改为
true
,在MouseUp事件中将其更改为
false

private void Loop_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
    {
        click.Enabled = true;
    }
}

private void Loop_MouseUp(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
    {
        click.Enabled = false;
    }
}
下面是一个完整的、有效的、经过测试的示例。此代码还包括设计器生成的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WorkingSolution
{
    public class Form1 : Form
    {
        private int _NumOfTicks;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                _NumOfTicks = 0;
                click.Enabled = true;
            }

        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                click.Enabled = false;
            }
        }

        private void click_Tick(object sender, EventArgs e)
        {
            this.lblTickCount.Text = _NumOfTicks.ToString();
            _NumOfTicks++;
        }

        #region designer code

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.click = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lblTickCount = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // click
            // 
            this.click.Interval = 1000;
            this.click.Tick += new System.EventHandler(this.click_Tick);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.Firebrick;
            this.label1.ForeColor = System.Drawing.Color.White;
            this.label1.Location = new System.Drawing.Point(37, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(207, 78);
            this.label1.TabIndex = 0;
            this.label1.Text = "Hold left mouse button over me";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
            this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(37, 149);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(109, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Number of timer ticks:";
            // 
            // lblTickCount
            // 
            this.lblTickCount.AutoSize = true;
            this.lblTickCount.Location = new System.Drawing.Point(152, 149);
            this.lblTickCount.Name = "lblTickCount";
            this.lblTickCount.Size = new System.Drawing.Size(0, 13);
            this.lblTickCount.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.lblTickCount);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Timer click;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label lblTickCount;

        #endregion designer code
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间工作解决方案
{
公开课表格1:表格
{
私人国际货币基金组织;
公共表格1()
{
初始化组件();
}
私有无效标签1u MouseDown(对象发送器,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
_NumOfTicks=0;
单击.Enabled=true;
}
}
专用无效标签1u MouseUp(对象发送器,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
单击.Enabled=false;
}
}
私有无效点击勾选(对象发送者,事件参数e)
{
this.lblTickCount.Text=\u NumOfTicks.ToString();
_NumOfTicks++;
}
#区域设计器代码
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
this.click=newsystem.Windows.Forms.Timer(this.components);
this.label1=new System.Windows.Forms.Label();
this.label2=new System.Windows.Forms.Label();
this.lblTickCount=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//点击
// 
this.click.Interval=1000;
this.click.Tick+=new System.EventHandler(this.click\u Tick);
// 
//标签1
// 
this.label1.BackColor=System.Drawing.Color.Firebrick;
this.label1.ForeColor=System.Drawing.Color.White;
this.label1.Location=新系统图纸点(37,24);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(207,78);
this.label1.TabIndex=0;
this.label1.Text=“在我上方按住鼠标左键”;
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
this.label1.MouseDown+=new System.Windows.Forms.MouseEventHandler(this.label1u MouseDown);
this.label1.MouseUp+=新系统.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
// 
//标签2
// 
this.label2.AutoSize=true;
this.label2.Location=新系统图纸点(37149);
this.label2.Name=“label2”;
this.label2.Size=新系统图纸尺寸(109,13);
this.label2.TabIndex=1;
this.label2.Text=“计时器节拍数:”;
// 
//lblTickCount
// 
this.lblTickCount.AutoSize=true;
this.lblTickCount.Location=新系统图点(152149);
this.lblTickCount.Name=“lblTickCount”;
this.lblTickCount.Size=新系统.Drawing.Size(0,13);
this.lblTickCount.TabIndex=2;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284
while (e.Button == MouseButtons.Left)
{
    click.Enabled = true;
}
click.Elapsed += new ElapsedEventHandler(click_Tick);
private void Form1_Load(object sender, EventArgs e)
{
    click.Enabled = false;      //click is timer which i added to forms!
    click.Interval = 1000;    
    click.Elapsed += new ElapsedEventHandler(click_Tick);
}
private void Loop_MouseDown(object sender, MouseEventArgs e)
{
while (e.Button == MouseButtons.Left)
    {
        click.Enabled = true;
    }
}

private void click_Tick(object sender, ElapsedEventArgs e)
{
      //Here is the loop!
}
using System.Windows.Forms;
private Timer click;
private Int16 testCounter;

private void Form1_Load(object sender, EventArgs e) {
    testCounter = 0;

    click = new Timer();
    click.Interval = 1000;
    click.Tick += new EventHandler(click_Tick);

    this.MouseDown += new MouseEventHandler(Loop_MouseDown);
    this.MouseUp += new MouseEventHandler(Loop_MouseUp);
}
private void Loop_MouseDown(object sender, MouseEventArgs e) {
    if(e.Button == MouseButtons.Left) {
        click.Start();
    }
}
private void Loop_MouseUp(object sender, MouseEventArgs e) {
    if(e.Button == MouseButtons.Left) {
        click.Stop();
    }
}

private void click_Tick(object sender, EventArgs e) {
    testCounter++;
}