【问题标题】:Mouse Click Loops using buttons (C#)使用按钮的鼠标单击循环 (C#)
【发布时间】:2013-04-08 18:10:24
【问题描述】:

我对 C# 有点陌生,所以请多多包涵!

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

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

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

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

非常感谢。

【问题讨论】:

    标签: c# button loops mouse


    【解决方案1】:

    挂钩到按钮上的 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
            }
        }
    }
    

    【讨论】:

    • 这种方法看起来效果很好。这很简单。鼠标按下时启动线程,鼠标抬起时停止。
    • 我虽然关于让线程仅在鼠标按下时暂停,但这会使 API 有点复杂,因为线程将继续存在,直到对象被正确处理或稍后完成。由于这是一个 GUI 应用程序,因此不断创建和销毁线程应该不会真正影响性能。
    【解决方案2】:

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

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

    【讨论】:

    • 提前感谢大家的热心回复:)我会开始工作并报告:)
    • 大家好,再次感谢您的热心帮助,最后决定使用计时器,我有一些非常丑陋的代码可能会让您感到愤怒,所以请原谅我;)仍在学习我的方式但玩得很开心,让我想起了在我的 Spectrum 和 Amiga 上再次成为一个孩子的感觉……有点 :)
    【解决方案3】:
    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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多