鼠标在c#中单击定时器中的事件

问题描述 投票:0回答:2

我在c#中的计时器中遇到鼠标点击事件的问题。

我的计时器的间隔是100并设置为True,我想每100个刻度进行操作并识别点击的类型。我想在按下鼠标时每100个刻度播放一次动作,但这只播放一次。编辑:我不想要启用/禁用时间。

   private void timer1_Tick(object sender, MouseEventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
    { 
       //Action...
    }

    if (MouseButtons == MouseButtons.Right)
    {  
        //Action...
    }
}
c# events timer click mouseevent
2个回答
1
投票

100个滴答是0.01毫秒,Interval是一个整数,所以我在这个测试中使用了秒。

此webform将检测最后单击的鼠标按钮,并根据上次单击的鼠标按钮更改计时器tick事件中的计时器间隔。我还添加了视觉指示标签:

public partial class Form1 : Form
{
    Timer t;
    MouseButtons lastMouseButtonClicked;
    Label lblStatus;

    public Form1()
    {
        InitializeComponent();

        lblStatus = new Label()
        {
            Text = "No click since tick."
            ,Width = 500
        };
        this.Controls.Add(lblStatus);

        t = new Timer();
        //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
        //t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
        t.Interval = 1000;
        t.Tick += T_Tick;
        t.Enabled = true;

        this.MouseClick += Form1_MouseClick;
    }

    private void T_Tick(object sender, EventArgs e)
    {
        switch (lastMouseButtonClicked)
        {
            case MouseButtons.Left:
                //Action...
                lblStatus.Text = "MouseButtons.Left";
                t.Interval = 1000;
                break;
            case MouseButtons.Right:
                //Action...
                lblStatus.Text = "MouseButtons.Right";
                t.Interval = 3000;
                break;
            default:
                lblStatus.Text = "No click since tick.";
                break;
        }
        LastMouseButtonClicked = MouseButtons.None;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        lastMouseButtonClicked = e.Button;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

定时器间隔更改为1或3秒,具体取决于鼠标单击。您还可以更改鼠标单击事件中的间隔以简化它。

这是一个例子:

public partial class Form1 : Form
{
    Timer t;
    MouseButtons LastMouseButtonClicked;
    Label lblStatus;
    DateTime previousTick;
    TimeSpan elapsed;

    public Form1()
    {
        InitializeComponent();

        lblStatus = new Label()
        {
            Text = "No click since tick."
            ,
            Width = 1000
        };
        this.Controls.Add(lblStatus);

        t = new Timer();
        //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
        //t.Interval = (int)(100 / TimeSpan.TicksPerMillisecond);//0.01 ms
        t.Interval = 1000;
        t.Tick += T_Tick;
        t.Enabled = true;

        this.MouseClick += Form1_MouseClick;

        elapsed = TimeSpan.Zero;
    }

    private void T_Tick(object sender, EventArgs e)
    {
        if (elapsed == TimeSpan.Zero)
        {
            elapsed += new TimeSpan(0, 0, 0, 0, 1);
        }
        else
        {
            elapsed += DateTime.Now - previousTick;
        }

        switch (LastMouseButtonClicked)
        {
            case MouseButtons.Left:
                //Action...
                lblStatus.Text = "MouseButtons.Left " + elapsed.Seconds;
                break;
            case MouseButtons.Right:
                //Action...
                lblStatus.Text = "MouseButtons.Right " + elapsed.Seconds;
                break;
            default:
                lblStatus.Text = "No click since tick. " + elapsed.Seconds;
                break;
        }

        previousTick = DateTime.Now;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButtons.Left:
                LastMouseButtonClicked = e.Button;
                t.Interval = 1000;
                break;
            case MouseButtons.Right:
                LastMouseButtonClicked = e.Button;
                t.Interval = 3000;
                break;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

0
投票

如果你真的很懒,并且不想在我的评论中阅读答案或重做逻辑,你可以像这样修改你的代码(如果这是你一直以来的意图):

private void timer1_Tick(object sender, EventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
    { 
       //Action...
    }

    if (MouseButtons == MouseButtons.Right)
    {  
        //Action...
    }
}

MouseButtons,如Control.MouseButtons

这是用于在timer_tick中获取鼠标按钮状态的方法,根据您的请求,无需鼠标单击事件。

更新你能提一下你用的是什么样的计时器吗?他们都有不同的行为和陷阱。 System.Windows.Forms.Timer,System.Timers.Timer和System.Threading.Timer。

我问,因为有时会有一个AutoReset属性,如果你想要发生多个timer_tick,你应该设置为true。我可能错了,但这听起来像你所描述的,所以值得一试!

@Soenhay:我怀疑OP想要在按住鼠标时“循环并执行每个滴答的动作”。据我所知。 MouseUp之后的MouseClick触发器。您可以修改代码以使用MouseButtons(静态WinForms奇怪)来检查按钮的状态。

@OP:如果没有你发布额外的代码,我没有办法让任何人帮助你进一步帮助你,除了用随机代码在黑暗中刺伤。现在你至少有3个类似于你需要的例子,关于静态MouseButtons类的新知识,所以我认为你可以而且应该从这里拿走它,否则你什么都学不到!

© www.soinside.com 2019 - 2024. All rights reserved.