2016-03-27 24 views
0

我无法在任何地方找到此问题的答案。是否有任何命令显示指定毫秒的图片框?我知道我可以做thread.sleep或task.delay。但是有没有其他的选择?可替代的东西:C#Windows窗体短时间显示图片框

picturebox1.visible = true; 
thread.sleep(1000); 
picturebox1.visible = false; 

非常感谢!

+0

使用['Timer'](https://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v = vs.110).aspx ) –

+0

为什么你不想使用'Task.Delay'或者甚至是'Thread.Sleep'? –

回答

0
private void Form1_Load(object sender, EventArgs e) 
{ 
    picturebox1.visible = true; 
    Timer MyTimer = new Timer(); 
    MyTimer.Interval = (1000); 
    MyTimer.Tick += new EventHandler(MyTimer_Tick); 
    MyTimer.Start(); 
} 

private void MyTimer_Tick(object sender, EventArgs e) 
{ 
    picturebox1.visible = false; 
    (sender as Timer).Stop(); 
} 
2

您可以使用Thread.SleepTask.Delay或者您可以使用它在其他的答案中描述的Timer

也许你不喜欢使用Task.DelayThread.Sleep,因为你认为它会让你的程序进入阻塞和冻结状态。您可以使用Thread.Sleep在不同的线程,以免结冰形式:

this.pictureBox1.Visible = true; 
Task.Run(() => 
{ 
    Thread.Sleep(5000); 
    this.Invoke(new Action(() => 
    { 
     this.pictureBox1.Visible = false; 
    })); 
}); 
//Other codes which you put here, will not wait and will run immediately. 
//Then after 5 seconds the picture box will be invisible again. 
0

你也可以做到这一点使用GDI +。而不是使用PictureBox,只需为表单的Paint事件添加处理程序即可。在里面,使用e.Graphics.DrawImage()方法绘制图像。使用一个全局布尔变量,你应该在1秒后设置为false(或者不管你的要求是什么)。在Paint事件中,请在绘制图像之前检查此变量。类似这样的:

bool DrawImage = true; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    Task.Delay(1000).ContinueWith((t) => 
    { 
    DrawImage = false; 
    Invalidate(); 
    }); 
} 

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    if (DrawImage) 
    e.Graphics.DrawImage(YOUR_IMAGE_HERE, 0, 0); 
}