如何创建一个渐变窗体。






4.33/5 (24投票s)
2003年5月13日
3分钟阅读

165100

1249
本文将引导您完成创建窗体所需的所有步骤,该窗体在激活时淡入,在停用时淡出。
引言
本文将向您展示如何创建一个在停用时淡出,在激活时淡入的窗体。
背景
我一直在寻找一种方法,可以让应用程序中活动窗体以视觉上吸引人的方式脱颖而出。我非常喜欢 XP 和 2000 的开始菜单中使用的淡入/淡出效果,但我无法找到关于具有这种效果的窗体的任何帮助。因此,我着手创建了一个具有淡入淡出效果的窗体,并允许用户轻松区分活动窗体与其他窗体。结果是 FaderForm
,它提供了很好的视觉效果,并使活动窗体易于用户识别。
使用代码
基本上,我们将从一个基本的 Windows Form
开始,添加一个计时器来增加/减少 Opacity
的百分比,处理两个 Form
事件(Activated
& Deactivate
),并在 FaderForm
类的构造函数中添加一些初始化代码。
首先,从一个普通的窗体开始。将以下属性设置为这些值:
Text
:FaderForm
Name
:FaderForm
其次,向窗体添加一个 Timer
控件,并将以下属性设置为这些值:
Name
:FadeTimer
Interval
: 1Enabled
:False
现在您已经拥有了所需的所有组件。现在,为了让窗体淡入淡出,我们必须处理两个事件:Activated
和 Deactivate
。实际的窗体 Opacity
的增加/减少是在 FadeTimer
组件的 FadeTimer_Tick
事件处理程序中完成的。Activated
和 Deactivated
事件处理程序将设置 Status
标志并启用 FaderTimer
。当 Status
标志设置为 FADEIN
时,FadeTimer_Tick
事件处理程序将增加 Opacity
;当 Status
设置为 FADEOUT
时,FadeTimer_Tick
事件处理程序将减少 Opacity
。FadeTimer_Tick
事件处理程序会将 Opacity
增加到 100% 或减少到 50%(取决于 Status
标志),然后自行关闭。每个 ffTransitionTime
毫秒处理一次滴答。让我们先从类成员开始。
首先,我们需要在 FaderForm
类的开头创建四个 private
类成员。成员如下:
FadeFlag
:类型为Enum
。一个枚举,提供了在Status
变量中使用的FADEIN
和FADEOUT
值。Status
:类型为FadeFlag
。用于存储所需过渡效果的状态。intTransitionStep
:类型为Integer
。用于增加或减少Opacity
的量。bFadeEnabled
:类型为Boolean
。用于启用或禁用淡入淡出效果的变量。
Public Class FaderForm
Inherits System.Windows.Forms.Form
'Enumeration for the current status of the window.
Private Enum FadeFlag
FADEIN
FADEOUT
End Enum
'Var to keep track of which transition is
'going to take place (fade in or out)
Private Status As New FadeFlag()
'Var to hold the increment amount when
'increasing/decreasing the Opacity
Private intTransitionStep As New Integer()
'Var used to enable or disable the fade in/out effect
Private bFadeEnabled As New Boolean()
接下来,添加三个 public
属性(为了在属性列表中将属性分组在一起,它们都带有前缀 ff,代表 faderform)。范围检查可以根据您的需要进行更改。
ffTransitionTime
:在将Opacity
按intTransitionStep
增加或减少之前经过的毫秒数。ffTransitionStep
:Opacity
增加或减少的量。ffFadeEnabled
:用于启用或禁用淡入淡出效果的变量。
属性 ffTransitionTime
的定义如下:
'TransitionTime: The amount of time (in milliseconds) that elapses before
'increasing or decreasing the Opacity by intTransitionStep.
Public Property ffTransitionTime() As Integer
Get
Return FadeTimer.Interval
End Get
Set(ByVal Value As Integer)
'If the value is between 1 ms and 1000ms it's valid
'If not, set the intTransitionTime to 1 (default value)
If Value <= 0 Or Value > 1000 Then
FadeTimer.Interval = 1
Else
FadeTimer.Interval = Value
End If
End Set
End Property
属性 ffTransitionStep
的定义如下:
'TransitionStep: The value used to increment the Opacity.
Public Property ffTransitionStep() As Integer
Get
Return intTransitionStep
End Get
Set(ByVal Value As Integer)
'If the incremented value is between 1 and 50 it's valid
'If not, set it to the default value of 2
If Value < 1 Or Value > 50 Then
intTransitionStep = 5
Else
intTransitionStep = Value
End If
End Set
End Property
属性 ffFadeEnabled
的定义如下:
Public Property ffFadeEnabled() As Boolean
Get
Return bFadeEnabled
End Get
Set(ByVal Value As Boolean)
bFadeEnabled = Value
If Value = False Then
'Since the fade in/out effect is being turned off, set
'the opacity to 100%, disable the timer, and even though
'it's not necessary set the Status to FADEIN.
Me.Opacity = 1.0
FadeTimer.Enabled = False
Status = FadeFlag.FADEIN
End If
End Set
End Property
最后,为以下内容编写事件处理程序:
FadeOut
:处理窗体的Deactivate
事件。FadeIn
:处理窗体的Activated
事件。FadeTimer_Tick
:处理FadeTimer
的Tick
事件。
FadeOut
的事件处理程序:
'FadeOut: Fired when the form loses focus through code or by the user.
'If the bFadeIn_Out value is set to true then
'this routine will set the Status
'flag to FADEOUT, the timer will gradually decrease the opacity of the form.
Private Sub FadeOut(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Deactivate
'First check the status of the FadeIn_Out property.
If bFadeEnabled = True Then
'Set the status to FadeOut
Status = FadeFlag.FADEOUT
'Turn the timer ON
FadeTimer.Enabled = True
End If
End Sub
FadeIn
的事件处理程序:
'FadeIn:Fired when the form is activated through code or by the user.
' If the bFadeIn_Out value is set to true then this
'routine will set the Status
' flag to FADEIN, the timer will gradually increase the opacity of the form.
Private Sub FadeIn(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
'First check the status of the FadeIn_Out property.
If bFadeEnabled = True Then
'Set the status fo FadeIn
Status = FadeFlag.FADEIN
'Turn the timer On
FadeTimer.Enabled = True
End If
End Sub
FadeTimer_Tick
的事件处理程序:
'FadeTimer_Tick: When the timer is enabled,
'it will check the value held in the
' Status variable. If the value is FADEIN the form's opacity is
' gradually increased or decreased by the value held in intTransitionStep
' every ffTransitionTime milliseconds.
Private Sub FadeTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FadeTimer.Tick
'Check the value of Status
If Status = FadeFlag.FADEIN Then
'We know the status is set to "fade in". Increase the Opacity
'Check the level of opacity
If Me.Opacity >= 1 Then
'Shut the timer off. Our work is done here.
FadeTimer.Enabled = False
Else
'Increase the opacity by 1%
Me.Opacity = Me.Opacity + intTransitionStep / 100.0
End If
Else
'We know the status is set to "fade out". Decrease the Opacity
'Check the level of opacity
If Me.Opacity <= 0.5 Then
'Shut off the timer. Our work is done here
FadeTimer.Enabled = False
Else
'Decrease the opacity
Me.Opacity = Me.Opacity - intTransitionStep / 100.0
End If
End If
End Sub
最后,我们需要在 FaderForm
类的构造函数中添加一些初始化代码。
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'Set the status of the form when it is created.
'Set the default values for the FaderForm.
Status = FadeFlag.FADEIN
FadeTimer.Enabled = False
intTransitionStep = 5
bFadeEnabled = True
End Sub
您的 FaderForm
现在已经准备就绪。在我提供的示例中,FaderForm
构建在 MMM 文件夹内的类库中,实现它的项目包含在 AnotherTest 文件夹内。只需单击 AnotherTest.sln 文件,构建示例应用程序,然后尽情享用!欢迎提出建议和意见,感谢您阅读我的文章!
历史
- 2003 年 5 月 12 日 - 上传了原始文章和代码。