• 0

Fade .NET window out when closing application


Question

This tutorial shows me how to:

 

  • Fade the program in when it is opened
  • Fade the program out when a Button on the form is clicked

 

I want to adapt it so that the program fades out when the user clicks Explorer's X (top right).

 

I have set the appropriate code for Timer2 but I just need help with how to apply Timer2 to when the program is closed.

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

I'd recommend not doing this at all, Windows 10 at least already has a window closing animation. But if you need to then the second link in the above post has the right info, watch for the FormClosing event.

Link to comment
Share on other sites

  • 0

When I double-click on Form1, it brings up the code.

 

At the top, I have:

 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.StartPosition = FormStartPosition.CenterParent
    Me.Opacity = 0
    Timer1.Interval = 20
    Timer1.Enabled = True
    Timer2.Interval = 20
    Timer2.Enabled = False
End Sub

Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Me.Opacity = 1 Then
        Timer2.Enabled = True
    End If
End Sub

At the bottom, I have:

 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Me.Opacity = 1 Then
        Timer1.Stop()
    Else
        Me.Opacity += 0.04
    End If
End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    If Me.Opacity = 0 Then
        Timer2.Stop()
        End
    Else
        Me.Opacity -= 0.02
    End If
End Sub

 

The program is fading in just fine.

 

However, closing the window isn't fading out.

 

Any ideas?

Link to comment
Share on other sites

  • 0

You have to cancel the actual FormClosing event, so your code can then close the window. Apparently you set e.Cancel = true.

 

Edit: And then close the form yourself when the timer finishes, and make sure you don't catch the event again, so you'll need a guard variable.

Link to comment
Share on other sites

  • 0

Implementing it using timers seems grossly inefficient to me. When I was toying with the idea, I just did it in a loop:

        private void fadeOut() {
            const int steps = 175;

            float x = 100f;
            float step = x / steps;
            for (int i = 0; i < steps; i++) {
                Opacity = x / 100;
                this.Refresh();
                x -= step;
            }
        }

Seemed to work fine at the time.

Link to comment
Share on other sites

  • 0

I'm not sure you could get more efficient that the inbuilt Timer class, they're wrappers around kernel functions iirc. They also let the main thread process messages when they're not running.

Link to comment
Share on other sites

  • 0

Doesn't seem worth the loss in responsivity to me. When the user clicks the top-right X, his expectation is that the application closes immediately, unless there's very good reason to delay it (e.g. data loss).

Link to comment
Share on other sites

  • 0
On 23/12/2015 at 1:15 PM, The_Decryptor said:

You have to cancel the actual FormClosing event, so your code can then close the window. Apparently you set e.Cancel = true.

 

Edit: And then close the form yourself when the timer finishes, and make sure you don't catch the event again, so you'll need a guard variable.

I'm a bit lost without code :(

 

On 23/12/2015 at 5:30 PM, simplezz said:

Implementing it using timers seems grossly inefficient to me. When I was toying with the idea, I just did it in a loop:


        private void fadeOut() {
            const int steps = 175;

            float x = 100f;
            float step = x / steps;
            for (int i = 0; i < steps; i++) {
                Opacity = x / 100;
                this.Refresh();
                x -= step;
            }
        }

Seemed to work fine at the time.

That doesn't seem like the correct language. The program wouldn't run, loads of code errors.

Link to comment
Share on other sites

  • 0
6 hours ago, Elliot B. said:

I'm a bit lost without code :(

 

That doesn't seem like the correct language. The program wouldn't run, loads of code errors.

It's valid C#. Here I converted it to VB for you:

Private Sub fadeOut()
	Const  steps As Integer = 175

	Dim x As Single = 100F
	Dim [step] As Single = x / steps
	For i As Integer = 0 To steps - 1
		Opacity = x / 100
		Me.Refresh()
		x -= [step]
	Next
End Sub

I actually used it on the PowerNotifier briefly during testing when I was considering having the Window automatically appear/disappear as the mouse entered/left the system tray. I decided against it in the end though.

 

 

Link to comment
Share on other sites

  • 0

I wasn't sure where to put that code so I put it at the top of form1 but it doesn't work :(

 

The top now reads:

 

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.CenterParent
        Me.Opacity = 0
        Timer1.Interval = 20
        Timer1.Enabled = True
        Timer2.Interval = 20
        Timer2.Enabled = False
    End Sub

    Private Sub fadeOut()
        Const steps As Integer = 175

        Dim x As Single = 100.0F
        Dim [step] As Single = x / steps
        For i As Integer = 0 To steps - 1
            Opacity = x / 100
            Me.Refresh()
            x -= [step]
        Next
    End Sub

    Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.Opacity = 1 Then
            Timer2.Enabled = True
        End If
    End Sub

 

Link to comment
Share on other sites

  • 0
1 minute ago, Elliot B. said:

I wasn't sure where to put that code so I put it at the top of form1 but it doesn't work :(

You need to call the subroutine at the point which you want to fade. Whether that's in the form closing event or not is up to you. So something like this:

Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
	fadeOut()
End Sub

 

Link to comment
Share on other sites

  • 0

1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable.

Link to comment
Share on other sites

  • 0
7 hours ago, Sledge said:

1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable.

Easy .. this is how people learn. By doing. We were all new to it once. Cut the OP a little slack?

Link to comment
Share on other sites

  • 0
17 hours ago, simplezz said:

You need to call the subroutine at the point which you want to fade. Whether that's in the form closing event or not is up to you. So something like this:


Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
	fadeOut()
End Sub

 

Absolute legend, thank you! (Y)

Link to comment
Share on other sites

  • 0
20 hours ago, Sledge said:

1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable.

For someone who wants control over how and when it fades, it's quite reasonable. The OS doesn't even support what I was using it for ;)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.