A Fade in Window

From IronPython Cookbook

Adapted from this Python Cookbook recipe by Brian Quinlan.

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.

This recipe demonstrates:

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


import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

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()


Back to Contents.

TOOLBOX
LANGUAGES