Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources]

David Ascher, Alex Martelli, Anna Ravenscroft

نسخه متنی -صفحه : 394/ 245
نمايش فراداده

Recipe 11.19. Implementing Fade-in Windows with IronPython

Credit: Brian Quinlan

Problem

You're developing an application with IronPython (using Windows Forms on Microsoft .NET), and you want to use fade-in windows to display temporary data.

Solution

Fading in can best be accomplished using the Form.Opacity property and a Timer. Fade-in windows, being a form of pop-up window, should also set the topmost window style:

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 window disappear
opacity_tracker.Enabled = True
timer.Stop( )
else:
opacity_tracker.Value = val
timer.Tick += tick
timer.Start( )
form.ShowDialog( )

Discussion

While IronPython, at the time of this writing, is not yet entirely mature, and it therefore cannot be recommended for use to develop Windows Forms applications intended for production deployment, any .NET (or Mono) developer should already download IronPython and start playing with it; when it matures, it promises to provide a nonpareil high-productivity avenue for .NET application development.

This recipe shows that IronPython can already do, with elegance and ease, a number of interesting things with Windows Forms. Specifically, the recipe demonstrates several techniques of Windows Forms programming:

  • How to create a form.

  • How to draw in an off-screen image.

  • How to create a control, add it to a form, and manage its events.

  • How to create a timer and add a delegate to get periodic events.

More specifically, this recipe shows how to create a fade-in window using IronPython. Several applications use fade-in windows for temporary data; look, for example, at Microsoft's new Outlook XP. It displays mail messages through a fade-in/fade-out pop-up window. It looks cool, it's also quite useful, and IronPython makes it a snap!

See Also

IronPython is at http://ironpython.com/.