C# 如何实现按钮长按事件的响应。
我做一个三维场景漫游的功能,例如按下“前进按钮”,当长按时,一直向前进,鼠标按钮谈起时,停止前进。请问,该如何实现呢? --------------------编程问答-------------------- 在MouseDown事件里实现且没有MouseUp事件触发。 --------------------编程问答-------------------- 我之前试了下,MouseDown事件里,漫游的动作并不会一直执行,里面的代码只会运行一次,效果和用Click事件是一样的。另外,你说没有MouseUp事件触发?可以说明得详细点吗?如果没有MouseUp事件,怎么知道如何停止漫游呢? --------------------编程问答-------------------- 放一个Timer,定时检查鼠标按键的状态和位置,当标志符合时一直执行某个动作,也可以用线程。而按钮的MouseDown事件里启动检查,MouseUp事件里结束检查。
--------------------编程问答-------------------- 我觉得应该是这样
--------------------编程问答--------------------
//设置一个标志位,标识是否向前结束
private bool IsEnd ;
//模拟执行范围
private int range = 10;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.IsEnd = false;
int i = 0;
while(!this.IsEnd)
{
i++;//模拟执行
if(i > range) this.IsEnd = true;
}
}
这种方法是可以的,但是执行范围Rangge该如何设定呢?我的模拟执行范围是不固定的,要一直到鼠标释放。 --------------------编程问答-------------------- MouseDown事件里启动Timer,MouseUp事件里停止Timer,你所要做的前进动作,写在Timer事件里 --------------------编程问答--------------------
--------------------编程问答-------------------- 同意楼上,但要用线程
//设置一个标志位,标识是否向前结束
private bool IsEnd ;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.IsEnd = false;
while(!this.IsEnd)
{
//执行
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.IsEnd = true;
}
private bool IsEnd ;
private system void mythread(Object objparam)
{
this.IsEnd = false;
while(!this.IsEnd)
{
//执行
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
ParameterizedThreadStart MyImpDelegate=new ParameterizedThreadStart(this,this.mythread);
Thread MyimpThread=new Thread(MyImpDelegate);
MyimpThread.IsBackground=true;
MyimpThread.Start(yourparam);
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.IsEnd = true;
}
--------------------编程问答--------------------
我通过下面的代码试验,但是CameraTranFront()这个函数并不会被调用, CameraTranFront()声明如下:private void CameraTranFront(object obj, System.Timers.ElapsedEventArgs e),哪里出问题了呢?
System.Timers.Timer _Timer;
private void btnFront_MouseDown(object sender, MouseEventArgs e)
{
_Timer = new System.Timers.Timer(500);
_Timer.Elapsed += new System.Timers.ElapsedEventHandler(CameraTranFront);
_Timer.AutoReset = true;
_Timer.Enabled = true;
_Timer.Start();
}
private void btnFront_MouseUp(object sender, MouseEventArgs e)
{
_Timer.Stop();
} --------------------编程问答-------------------- 确实问题想简单了,以前也没有做过什么winform!!!1
--------------------编程问答--------------------
这种方法可以实现,但是请问,这里只是开启了线程,如何在哪里关闭这个线程呢,否则是否一直存在后台运行中。 --------------------编程问答-------------------- private void button3_MouseDown(object sender, MouseEventArgs e)
{
this.timer1.Interval = 200;
this.timer1.Enabled = true;
}
private void button3_MouseUp(object sender, MouseEventArgs e)
{
this.timer1.Enabled = false;
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
i++;
this.label1.Text = i.ToString();
} --------------------编程问答-------------------- 简单测试,按下之后,label1的text文本不停变换,松开鼠标,停止变化 --------------------编程问答--------------------
/// <summary>--------------------编程问答--------------------
/// 按下按钮一
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.timer1.Enabled = true;
}
}
/// <summary>
/// 时间控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//移动名为btnMove的按钮
this.btnMove.Location = new Point(this.btnMove.Location.X + 1, this.btnMove.Location.Y);
}
/// <summary>
/// 释放按钮一
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.timer1.Enabled = false;
}
执行完线程就结束了啊 --------------------编程问答-------------------- 看来只能用timer或者是线程了
补充:.NET技术 , C#