我是C#的新手,請耐心等待!鼠標單擊循環使用按鈕(C#)
我正在寫一個程序,通過RS232發送代碼到自制望遠鏡安裝。
我目前最大的問題是希望很簡單(但相當困難的我!)
舉個例子說,我有一個按鈕,我想執行一個循環時,鼠標左鍵按住,(這將是一個232個數據的連續流),然後當鼠標左鍵被釋放時,我需要循環停止並執行另一行代碼。
我真誠地希望我給的信息是足夠的,有人是一種足以幫助我一起(我在網上搜索答案相信我!)
非常感謝。
我是C#的新手,請耐心等待!鼠標單擊循環使用按鈕(C#)
我正在寫一個程序,通過RS232發送代碼到自制望遠鏡安裝。
我目前最大的問題是希望很簡單(但相當困難的我!)
舉個例子說,我有一個按鈕,我想執行一個循環時,鼠標左鍵按住,(這將是一個232個數據的連續流),然後當鼠標左鍵被釋放時,我需要循環停止並執行另一行代碼。
我真誠地希望我給的信息是足夠的,有人是一種足以幫助我一起(我在網上搜索答案相信我!)
非常感謝。
掛鉤到按鈕上的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: 首先創建一個設置爲你想要發送的數據的頻率間隔計時器。並在tick事件中發送數據。爲按鈕的鼠標向下事件和按鈕的鼠標向上事件創建一個事件。在鼠標停止事件中,啓動計時器。在鼠標向上事件中,停止計時器。
方法2: 而不是在鼠標停止事件啓動一個計時器,開始一個新的線程,你做一個連續的數據發送循環。停止鼠標向上事件的線程。
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;
}
}
}
這種方法看起來會很好。這很簡單。在鼠標上啓動一個線程,在鼠標上停下來。 – 2012-07-21 02:02:02
我雖然關於線程只能在鼠標懸停時暫停,但是這會使API複雜化一點,因爲線程會繼續活動,直到對象被妥善處置或稍後完成。由於這是一個GUI應用程序,所以不斷創建和銷燬線程不應該影響性能。 – cdhowie 2012-07-21 02:15:45