鼠标单击循环使用按钮(C#)

时间:2012-07-21 01:32:28

标签: c# button loops mouse

我是C#的新手,所以请耐心等待我!

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

我现在遇到的问题很有希望非常简单(但对我来说很难!)

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

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

非常感谢。

3 个答案:

答案 0 :(得分: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
        }
    }
}

答案 1 :(得分:0)

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

方法2: 而不是在鼠标按下事件上启动计时器,而是启动一个新线程,您可以在其中执行连续的数据发送循环。在鼠标注册事件中停止线程。

答案 2 :(得分: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;
        }
    }
}