我想用winform应用程序实现下一个场景:
当应用程序启动时,它会保留在桌面上。如果用户没有使用该应用程序一段时间为例1min,我希望它失去它的透明度(主要形式的透明度减少到一半)
如果再次使用应用程序(焦点,鼠标悬停......),主窗体的透明度值将重新设置为100%。
所以实际上我需要从哪里开始?
我假设我需要在不同的线程中使用一个计时器来激活一些事件,并且它达到1分钟,但问题是,我将如何(以及哪些)从不同线程中的事件中听到(一个我用于计时器)
感谢
答案 0 :(得分:0)
答案 1 :(得分:0)
正如Lars所说,表格上有Opacity
属性。
要在表单处于非活动状态时将不透明度设置为一半,您需要处理Deactivated
或Application.Idle事件。在这个启动时启动一个计时器,它将消息发送回表单(在UI线程上)以实际设置值。
private void Form_Deactivate(object sender, EventArgs e)
{
this.inactiveTimer = new Timer();
this.inactiveTimer.Interval = 1000;
this.inactiveTimer.Tick += this.InactiveTimer_Tick;
// Start timer
this.inactiveTimer.Start();
}
private void InactiveTimer_Tick(object sender, EventArgs e)
{
// This is being handled on the UI thread
this.Opacity = 0.5;
this.inactiveTimer.Stop();
}
如果您希望表单逐渐获得透明度,请将计时器间隔设置为较小的量(例如100毫秒),并在每个刻度上按步骤降低透明度。然后当不透明度达到0.5时,停止计时器。
当表单再次激活时会触发Activated
事件:
private void Form_Activate(object sender, EventArgs e)
{
this.Opacity = 1.0;
// Stop the timer for the cases where the user reactivates the app
this.inactiveTimer.Stop();
}
还有其他一些事件,例如SizeChanged
,您可能需要陷阱以确保不透明度正确设置为1(当表单从最小化恢复时触发)和ResizeEnd
。