我想在C#中使用下面的代碼,但我似乎無法擺脫它。如果用戶按下某個鍵或移動鼠標(又名鼠標),我想終止該應用程序。這是我的代碼(不笑!)。如何阻止我的while循環?
private void frmDots_KeyDown(object sender, KeyEventArgs e)
{
bgNotClicked = false;
Close();
}
private void frmDots_Click(object sender, EventArgs e)
{
bgNotClicked = false;
Close();
}
while (bgNotClicked)
{
// Clear the first element in our XY position. This is the reverse of the way I normally create the dots application
System.Drawing.Rectangle clearDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);
// Create the black color and brush to clear dots
Color clearDotsColor = Color.Black;
SolidBrush clearDotsBrush = new SolidBrush(clearDotsColor);
// Finally clear the dot
e.Graphics.FillEllipse(clearDotsBrush, clearDots);
GetRandomPosition(iCounter);
// Fill the elements to display colors on the displays canvas
System.Drawing.Rectangle colorDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);
// Create the color and brush to show dots
Color colorRandom = GetRandomColor();
SolidBrush colorBrush = new SolidBrush(colorRandom);
// Finally show the dot
e.Graphics.FillEllipse(colorBrush, colorDots);
Thread.Sleep(5);
iCounter++;
if (iCounter == 399)
{
iCounter = 0;
}
}
}
我想我要從現在開始稱它爲「齧齒動物」,始終是 – 2009-05-01 17:55:01
哦,我的上帝,我只是意識到齧齒動物的意思是鼠標。棒極了。 – AndreiM 2009-05-01 17:57:54
@Peter:我更新了我的答案以迴應您的修改。問題在於你的UI消息泵被阻塞,所以你的處理程序從不運行。 – 2009-05-01 18:08:26