使用按钮实现鼠标点击循环 (C#)

3

我对C#还有些陌生,请多包涵!

我正在编写一个程序,通过RS232将代码发送到自制的望远镜底座上。

我目前遇到的问题希望很简单(但对我来说相当困难!)

比如说,假设我有一个按钮,我想在按下鼠标左键时执行一个循环(这将是一个连续流的232数据),然后当鼠标左键被释放时,我需要停止循环并执行另一行代码。

我真诚地希望我提供的信息足够,并且有人足够友好地帮助我(相信我,我已经在互联网上搜索过答案!)

非常感谢。

3个回答

3

在按钮上挂接MouseDown和MouseUp事件。MouseDown事件应当生成线程或者向线程发送信号开始执行循环。MouseUp事件应当向线程发送信号停止执行循环。

像这样:

public class InterruptibleLoop
{
    private volatile bool stopLoop;
    private Thread loopThread;

    public void Start() {
        // If the thread is already running, do nothing.
        if (loopThread != null) {
            return;
        }

        // Reset the "stop loop" signal.
        stopLoop = false;

        // Create and start the new thread.
        loopThread = new Thread(LoopBody);
        loopThread.Start();
    }

    public void Stop() {
        // If the thread is not running, do nothing.
        if (loopThread == null) {
            return;
        }

        // Signal to the thread that it should stop looping.
        stopLoop = true;

        // Wait for the thread to terminate.
        loopThread.Join();

        loopThread = null;
    }

    private void LoopBody() {
        while (!stopLoop) {
            // Do your work here
        }
    }
}

这种方法看起来应该能很好地工作。而且它足够简单。在鼠标按下时启动线程,在鼠标松开时停止线程。 - DROP TABLE users
我考虑过只在鼠标按下时暂停线程,但这会使API有些复杂,因为线程会继续存在,直到对象被正确处理或稍后完成。由于这是一个GUI应用程序,常量线程的创建和销毁不应该真正影响性能。 - cdhowie

0
namespace Scope_Project_Ver_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
            // *** Output data timer ***
            otimer.Interval = 50;
            // otimer.Interval = isendFreq;
            otimer.Tick += new EventHandler(otimer_Tick);
            // *** Input data timer ***
            // itimer.Interval = 601;                        <- to be unchecked
            // itimer.Tick += new EventHandler(itimer_Tick); <- to be unchecked
        }

        public int b1,b2,b3,b4,b5;
        public string sb1, sb2, sb3, sb4, sb5;
        public int ivorSpeed;
        public string svorSpeed;
        public int ihorSpeed;
        public string shorSpeed;
        public int isendFreq;


        private void sendDataB_Click(object sender, MouseEventArgs e)
        {
            if (sendDataCB.Checked)
            {
                sendDataCB.Checked = false;
                if (otimer.Enabled)
                    otimer.Stop();
            }
            else
            {
                sendDataCB.Checked = true;
                if (!otimer.Enabled)
                    otimer.Start();
            }
        }


        void otimer_Tick(object sender, EventArgs e)
        {
            SerialPort port = new SerialPort(
            "COM1", 9600, Parity.None, 8, StopBits.One);
            port.Open();
            port.Write("Q"); //                         Q
            port.Write(sb1); //                         1
            port.Write(sb2); //                         2
            // Binary stuff  // Ver Speed Binary        3           
            byte[] bverbinary = new byte[1];
            byte verbinary = 0;
            verbinary = Byte.Parse(svorSpeed);
            bverbinary[0] = verbinary;
            port.Write(bverbinary, 0, bverbinary.Length);
            // End of Binary stuff for Ver Speed
            // Binary stuff // Hor Speed Binary         4
            byte[] bhorbinary = new byte[1];
            byte horbinary = 0;
            horbinary = Byte.Parse(shorSpeed);
            bhorbinary[0] = horbinary;
            port.Write(bhorbinary, 0, bhorbinary.Length);
            port.Write(sb5);  // Movement               5
            port.Close();
        }


        private void vorSpeed_ValueChanged(object sender, EventArgs e)
        {
            // MessageBox.Show((this.vorSpeed.Value).ToString());
            this.Text = "You changed the Vertical Speed to " + vorSpeed.Value;
            ivorSpeed = (int)vorSpeed.Value;
            svorSpeed = ivorSpeed.ToString(); 
        }

        private void horSpeed_ValueChanged(object sender, EventArgs e)
        {
            // MessageBox.Show((this.horSpeed.Value).ToString());
            this.Text = "You changed the Horizontal Speed to " + horSpeed.Value;
            ihorSpeed = (int)horSpeed.Value;
            shorSpeed = ihorSpeed.ToString(); 
        }

        private void scopeUp_MouseDown(object sender, MouseEventArgs e) // Scope Up On
        {
            b1 = 2;
            b2 = 0;
            b5 = 1;
            sb1 = b1.ToString();
            sb2 = b2.ToString();
            sb3 = b3.ToString();
            sb4 = b4.ToString();
            sb5 = b5.ToString();
        }

        private void scopeUp_MouseUp(object sender, MouseEventArgs e) // Scope Up Off
        {

        }

        private void scopeRight_MouseDown(object sender, MouseEventArgs e)
        {
            b1 = 1;
            b2 = 2;
            b5 = 1;
            sb1 = b1.ToString();
            sb2 = b2.ToString();
            sb3 = b3.ToString();
            sb4 = b4.ToString();
            sb5 = b5.ToString();
        }

        private void scopeRight_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void scopeDown_MouseDown(object sender, MouseEventArgs e)
        {
            b1 = 2;
            b2 = 1;
            b5 = 1;
            sb1 = b1.ToString();
            sb2 = b2.ToString();
            sb3 = b3.ToString();
            sb4 = b4.ToString();
            sb5 = b5.ToString();
        }

        private void scopeDown_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void scopeLeft_MouseDown(object sender, MouseEventArgs e)
        {
            b1 = 0;
            b2 = 2;
            b5 = 1;
            sb1 = b1.ToString();
            sb2 = b2.ToString();
            sb3 = b3.ToString();
            sb4 = b4.ToString();
            sb5 = b5.ToString();
        }

        private void scopeLeft_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void scopeStop_Click(object sender, EventArgs e)
        {
            b1 = 0;
            b2 = 0;
            b5 = 0;
            sb1 = b1.ToString();
            sb2 = b2.ToString();
            sb3 = b3.ToString();
            sb4 = b4.ToString();
            sb5 = b5.ToString();
        }

        private void sendFreq_ValueChanged(object sender, EventArgs e)
        {
            this.Text = "You changed the send Freq to " + sendFreq.Value + " m/s";
            isendFreq = (int)sendFreq.Value;
        }
    }
}

0

方法1: 首先创建一个定时器,将间隔设置为发送数据的频率。在tick事件中发送数据。为按钮的鼠标按下事件和按钮的鼠标松开事件创建一个事件。在鼠标按下事件中启动计时器,在鼠标松开事件中停止计时器。

方法2: 不要在鼠标按下事件中启动计时器,而是启动一个新线程,在其中进行连续的数据发送循环。在鼠标松开事件中停止线程。


提前感谢大家的友好回复 :) 我会开始工作并汇报 :) - Macko
大家好,再次感谢你们的帮助。最终决定使用计时器,在此之前我写了一些非常丑陋的代码,可能会让你们感到不爽,请原谅我;)我还在学习中,但是度过了很愉快的时光,这让我想起了小时候在我的 Spectrum 和 Amiga 上玩耍的日子...有点像 :) - Macko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接