我正在編寫一個愚蠢的小程序(正如我們所做的那樣),以處理一些基本的C#編碼。我相信你可以看到,點擊鼠標,圖片就會移動到鼠標點擊的地方。這一點工作正常。然後我想讓這個圖畫框隨機移動,這也是我管理的!我遇到的下一個問題是,當我單擊時,圖片框會繼續隨機移動,而不是移動到新的鼠標座標。任何幫助,將不勝感激!這是我認爲是問題的代碼。C# - 如何實時註冊onMouseClick?
非常感謝!
約翰
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
int destX = e.X;
int destY = e.Y;
HasArrived = false;
while (HasArrived == false)
{
moveImage(destX, destY, pictureBox1);
if (pictureBox1.Left == destX && pictureBox1.Top == destY)
{
HasArrived = true;
while (HasArrived == true)
{
randomMove(pictureBox1);
hungry1 += 1;
}
}
}
}
由於控件是在不同的線程中創建的,我是否必須使用Invoke(在這種情況下是BeginInvoke)才能訪問控件? –
是或者你可以使用progresschanged,但我不知道它是否會凍結gui? – Vloxxity
謝謝!有用! –