Welcome, guest | Sign In | My Account | Store | Cart

This recipe describes how to create a fade-in window using IronPython. Several applications use fade-in windows for temporary data e.g. new Outlook XP mail messages are shown through a fade-in/fade-out pop-up window.

Fading in can be best accomplished using the Form.Opacity property and a Timer. Pop-up windows should also set the "topmost" window style.

Python, 57 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from System.Windows.Forms import *
from System.Drawing import *
from System.Drawing.Imaging import *

form = Form(Text="Window Fade-ins with IronPython",
         HelpButton=False,
         MinimizeBox=True, MaximizeBox=True,
         WindowState=FormWindowState.Maximized,
         FormBorderStyle = FormBorderStyle.Sizable,
         StartPosition = FormStartPosition.CenterScreen,
         Opacity = 0)

# create a checker background pattern image
box_size = 25
image = Bitmap(box_size * 2, box_size * 2)
graphics = Graphics.FromImage(image);
graphics.FillRectangle(Brushes.Black, 0, 0, box_size, box_size);
graphics.FillRectangle(Brushes.White, box_size, 0, box_size, 50);
graphics.FillRectangle(Brushes.White, 0, box_size,box_size, box_size);
graphics.FillRectangle(Brushes.Black, box_size, box_size, box_size, box_size);
form.BackgroundImage = image

# create a control to allow the opacity to be adjusted
opacity_tracker = TrackBar(Text="Transparency",
              Height = 20,
              Dock = DockStyle.Bottom,
              Minimum = 0, Maximum = 100, Value = 0,
              TickFrequency = 10,
              Enabled = False)

def track_opacity_change(sender, event):
    form.Opacity = opacity_tracker.Value / 100.0
opacity_tracker.ValueChanged += track_opacity_change

form.Controls.Add(opacity_tracker)

# create a timer to animate the initial appearance of the window
timer = Timer()
timer.Interval = 15

def tick(sender, event):
    val = opacity_tracker.Value + 1
    if val >= opacity_tracker.Maximum:
        # ok, we're done, set the opacity to maximum, stop the 
        # animation, and let the user play with the opacity manually
        opacity_tracker.Value = opacity_tracker.Maximum
        opacity_tracker.Minimum = 20 # don't let the user hurt themselves
        opacity_tracker.Enabled = True

        timer.Stop()
    else:
        opacity_tracker.Value = val
        
timer.Tick += tick
timer.Start()

form.ShowDialog()

IronPython is not currently at a stage where robust Windows Forms applications can be written but this recipe demonstrates several techniques of Windows Forms programming:

o how to create a form o how to draw in an offscreen image o how to create a control, add it to a form and manage it's events o how to create a timer and add a delegate to get periodic events