Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

 

Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

 

© Microsoft Corporation. All rights reserved.

 

Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#,  and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

 

Other product and company names herein may be the trademarks of their respective owners.


 

This source code is subject to terms and conditions of the Apache License, Version 2.0. A copy of the license can be found in the License.html file at the root of this distribution. If you cannot locate the Apache License, Version 2.0, please send an email to ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound by the terms of the Apache License, Version 2.0.

 

 

Bad Paint

Bad paint is a sample paint program built upon the Windows Presentation Foundation framework.  What distinguishes this paint application from others is it's use of Microsoft .NET 4.0's Dynamic Language Runtime (DLR) feature.  The DLR is utilized to dynamically and programmatically modify visual objects within the paint canvas.  Such a feat is extremely difficult to accomplish with statically typed languages, and the DLR makes this a breeze to implement for Python.

 

Prerequisites

The prerequisites to successfully run the Bad Paint demo are:

Getting Started

Please make sure the sample folder, BadPaint, (where this readme.htm is located) contains at least the following:

.\App.config
.\App.xaml
.\App.xaml.cs
.\BadPaint.csproj
.\BadPaint.sln
.\demo_snippets.py
.\MainWindow.xaml
.\MainWindow.xaml.cs
.\Properties\

                    AssemblyInfo.cs
                   Resources.Designer.cs
                   Resources.resx
                   Settings.Designer.cs
                   Settings.settings


IMPORTANT NOTE: if the folder containing the IronPython distribution is not in the default MSI install location, "%ProgramFiles%\IronPython 2.6 for .NET 4.0", you'll need to modify BadPaint.csproj to point to the correct installation location.  This means you'll need to adjust the HintPath elements within BadPaint.csproj which can be done either with notepad.exe or Visual Studio 2010.


Building Bad Paint

We recommend building the "Release" configuration of  BadPaint.sln directly from Visual Studio, although you can also build the demo from the command prompt by navigating to the BadPaint directory and typing the following:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild BadPaint.sln /p:configuration=Release

 

Running this command or building directly from Visual Studio will yield bin\Release\Demo.exe.


Playing with Demo.exe

From the command prompt, navigate to the BadPaint directory and type the following:

> bin\Release\Demo.exe

 

An application similar to the screenshot below will then start:

BadPaint Start Screenshot

 

Try dragging a square onto the black canvas and follow this up by dragging the color green into the square:

Paint Screenshot with green square

 

At this point let's try to dynamically modify the canvas.  First, click on the green square to select it.  Now type "A" into the "Selected Item" textbox and strike the Enter key once.  Behind the scenes, this sequence of actions has associated our green square as the "A" attribute of the Application object from IronPython. 

Now click the "Language" drop-down menu and select the IronPython 2.6 option.  This allows us to input some Python code into the textbox immediately below the "Language" drop-down menu.  Type:

Application.A.Width = 100

into this box:

Demo with Python text

 

Finally, click the "Run Code" button to actually run your Python code against the canvas contents:

Demo after the Python snippet has run

As you can imagine, at this point we've only touched the surface so to speak with respect to what's possible from the scripting code textbox.  This sample includes a demo_snippets.py file which as its name might imply, includes a few less trivial snippets of Python code intended to be copied and pasted into this sample's textbox and run.


This concludes our tutorial. Have fun playing with this sample!