【发布时间】:2011-10-10 20:32:23
【问题描述】:
我想在按下鼠标左键时使用循环:
private void Loop_MouseDown(object sender, MouseEventArgs e)
{
while (e.Button==MouseButtons.Left)
{
//Loop
}
}
我无法使用此线程中的解决方案:
C# how to loop while mouse button is held down
因为我通过 RS232 数据发送并且使用具有自己间隔的计时器不起作用。此外,该主题的任何解决方案都不适合我。
它也不能像这里一样工作:
if (e.Button == MouseButtons.Left)
{
//loop
}
此解决方案也不起作用:
bool isLooping = false;
//on mouse down
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = true;
runLoop();
}
//on mouse up event
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = false;
}
//This is the main loop you care about. Put this in your application
//This should go in its own thread
void runLoop() {
while (isLooping) {
//do stuff
}
}
因为调用 runLoop 会阻塞线程,所以 MouseUp 事件永远不会触发。
那么如何让它正常工作呢?
【问题讨论】:
-
你有没有考虑过使用 API 来获取鼠标按键的当前状态?
标签: c# winforms mouseevent