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

时间:2017-07-26 13:23:57

标签: c# loops

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



 
        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy,
                      int dwData, int dwExtraInfo);
        [Flags]
        public enum MouseEventFlags
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010
       }          
    private void Form1_Load(object sender, EventArgs e)
    {
         click.Enabled = false;      //click is timer which i added to forms!
         click.Interval = 1000;      
    }
    //it should do loop when left mouse click i just pressed NOT ON LABEL OR BUTTON!
    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;
            }
        }
                
    private void click_Tick(object sender, EventArgs e)
    {
        mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
        mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
    }



 如果有0个工作答案,有人可以告诉我为什么我对这个问题有如此多的不喜欢?并且存在类似这样的类型问题,但这个问题指出ISN相同。这个问题点是,如果在应用程序,桌面,任何地方随时按下鼠标左键!

5 个答案:

答案 0 :(得分:8)

根本不需要循环。只需将MouseDp事件上的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
    }
}

答案 1 :(得分:1)

您需要添加一个事件处理程序来触发计时器

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!
}

答案 2 :(得分:0)

你似乎把你的循环混淆了。循环就是这个部分:

while (e.Button == MouseButtons.Left)
{
    click.Enabled = true;
}

当条件为真时,该代码将继续执行{ } 中的部分:e.Button == MouseButtons.Left。你永远不会改变e.Button的值,这意味着它会一直循环。

猜测,当条件为真时,您不想设置click.Enabled = true; ;你想设置它如果条件为真。将while更改为if,它只会发生一次,并且您的程序不会陷入无限循环。

答案 3 :(得分:0)

为了清楚这一切,我编写了代码,其中包含一些必要的代码,这包括一些已经发布的答案:

对于Timer和MouseButton事件,您需要在课程上方添加:

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++;
}

一旦按下mouseButton,方法Loop_MouseDown将被触发,这将启动计时器。

释放mouseButton后,方法Loop_MouseUp将被触发,这将停止(并重置)计时器。

只要计时器处于活动状态,方法click_Tick每1000ms(1s)将被调用,这会使计数器增加1;

所以总结: 对于每个整个secon,mouseButton都被按下,计数器将增加1。


此外,如果您无法调试正在编写的代码,并且无法提供人们帮助您所需的信息,您可以考虑从更简单的程序开始学习所需的一切。


编辑: 当然在System.Windows.Forms中有Timer: enter image description here

答案 4 :(得分:0)

这已经得到了解答。检查一下,你应该能够提出一个解决方案。

C# how to loop while mouse button is held down