- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Painting II in IronPython Mono Winforms
In this part of the IronPython Mono Winforms tutorial, we will continue with painting.
Donut
In the following example we create an complex shape by rotating a bunch of ellipses.
donut.py
#!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Drawing import Size, Color, SolidBrush, Pen class IForm(Form): def __init__(self): self.Text = 'Donut' self.Size = Size(350, 300) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics pen = Pen(Color.Gray, 1) size = self.ClientSize g.TranslateTransform(size.Width/2, size.Height/2) g.DrawEllipse(pen, -125, -125, 250, 250) for i in range(0, 36): g.DrawEllipse(pen, 0, 0, 120, 50) g.RotateTransform(10) g.Dispose() Application.Run(IForm())
We draw five lines on the form. Each line has different DashStyle
.
size = self.ClientSize g.TranslateTransform(size.Width/2, size.Height/2) g.DrawEllipse(pen, -125, -125, 250, 250)
We draw a circle in the middle of the form.
for i in range(0, 36): g.DrawEllipse(pen, 0, 0, 120, 50) g.RotateTransform(10)
We draw 36 ellipses, each rotated by 10 degree after the last one. Thus getting the donut object.

Transparent rectangles
Transparency is the quality of being able to see through a material. The easiest way to understand transparency is to imagine a piece of glass or water. Technically, the rays of light can go through the glass and this way we can see objects behind the glass.
In computer graphics, we can achieve transparency effects using alpha compositing. Alpha compositing is the process of combining an image with a background to create the appearance of partial transparency. The composition process uses an alpha channel. (wikipedia.org, answers.com)
transparentrectangles.py
#!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Drawing import Size, Color, SolidBrush class IForm(Form): def __init__(self): self.Text = 'Transparent rectangles' self.Size = Size(590, 110) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics for i in range(1, 11): color = Color.FromArgb(i*25, 0, 0, 255) brush = SolidBrush(color) g.FillRectangle(brush, 50*i, 20, 40, 40) Application.Run(IForm())
In the example we will draw ten rectangles with different levels of transparency.
color = Color.FromArgb(i*25, 0, 0, 255)
This line creates a colour object. The first value is the alpha transparency.
brush = SolidBrush(color)
We create a brush from the colour.
g.FillRectangle(brush, 50*i, 20, 40, 40)
We draw a rectangle.

Grayscale image
The following example creates a grayscale image.
grayscale.py
#!/usr/bin/ipy import clr import sys clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Drawing import Size, Rectangle from System.Drawing import Bitmap, Color class IForm(Form): def __init__(self): self.Text = 'Grayscale' self.Size = Size(290, 150) self.Paint += self.OnPaint self.rotunda = self.loadImage() self.gs = self.grayScale(self.rotunda.Clone()) self.CenterToScreen() def loadImage(self): try: rotunda = Bitmap("rotunda.jpg") return rotunda except Exception, e: print e.msg sys.exit(1) def grayScale(self, image): w = image.Width h = image.Height for i in range(w): for j in range(h): c = image.GetPixel(i, j) lum = 0.299*c.R + 0.587*c.G + 0.114*c.B image.SetPixel(i, j, Color.FromArgb(lum, lum, lum)) return image def OnPaint(self, event): g = event.Graphics r1 = Rectangle(15, 15, self.rotunda.Width, self.rotunda.Height) g.DrawImage(self.rotunda, r1) r2 = Rectangle(150, 15, self.gs.Width, self.gs.Height) g.DrawImage(self.gs, r2) g.Dispose() Application.Run(IForm())
We have two images in our example. A colour and a grayscale one.
self.rotunda = self.loadImage()
The loadImage()
method loads a bitmap from the current working directory of the disk.
self.gs = self.grayScale(self.rotunda.Clone())
The grayScale()
method makes a grayscale image from a colour image. We give a copy of the rotunda image as a parameter to this method.
c = image.GetPixel(i, j)
We get all pixels from the image.
lum = 0.299*c.R + 0.587*c.G + 0.114*c.B
This equation calculates a luminocity for a grayscale image. If we scale the the red, green and blue parts of the colour with these factors, the human eye sees the image as gray.
image.SetPixel(i, j, Color.FromArgb(lum, lum, lum))
We modify the pixel.

Gradients
In computer graphics, gradient is a smooth blending of shades from light to dark or from one colour to another. In 2D drawing programs and paint programs, gradients are used to create colorful backgrounds and special effects as well as to simulate lights and shadows. (answers.com)
gradients.py
#!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Drawing import Size, Color, Point from System.Drawing.Drawing2D import LinearGradientBrush class IForm(Form): def __init__(self): self.Text = 'Gradients' self.Size = Size(350, 350) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics pt1 = Point(5, 5) pt2 = Point(25, 25) lg = LinearGradientBrush(pt1, pt2, Color.Red, Color.Black) g.FillRectangle(lg, 20, 20, 300, 40) pt1 = Point(5, 25) pt2 = Point(20, 2) lg = LinearGradientBrush(pt1, pt2, Color.Yellow, Color.Black) g.FillRectangle(lg, 20, 80, 300, 40) pt1 = Point(5, 25) pt2 = Point(2, 2) lg = LinearGradientBrush(pt1, pt2, Color.Green, Color.Black) g.FillRectangle(lg, 20, 140, 300, 40) pt1 = Point(25, 25) pt2 = Point(15, 25) lg = LinearGradientBrush(pt1, pt2, Color.Blue, Color.Black) g.FillRectangle(lg, 20, 200, 300, 40) pt1 = Point(0, 10) pt2 = Point(0, 20) lg = LinearGradientBrush(pt1, pt2, Color.Orange, Color.Black) g.FillRectangle(lg, 20, 260, 300, 40) lg.Dispose() g.Dispose() Application.Run(IForm())
We draw five rectangles which are filled with different linear gradients.
pt1 = Point(5, 5) pt2 = Point(25, 25)
These two are the controlling points of the linear gradient brush.
lg = LinearGradientBrush(pt1, pt2, Color.Red, Color.Black)
We create the LinearGradientBrush
object. We use two controlling points and two blending colours.

Waiting
In this examle, we use transparency effect to create a waiting demo. We will draw 8 lines that will gradually fade out creating an illusion, that a line is moving. Such effects are often used to inform users, that a lengthy task is going on behind the scenes. An example is streaming video over the Internet.
waiting.py
#!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, Timer from System.Drawing import Size, Color, SolidBrush, Pen from System.Drawing.Drawing2D import SmoothingMode, LineCap from System.ComponentModel import Container trs = ( ( 0, 35, 70, 100, 150, 180, 210, 250 ), ( 250, 0, 35, 70, 100, 150, 180, 210 ), ( 210, 250, 0, 35, 70, 100, 150, 180 ), ( 180, 210, 250, 0, 35, 70, 100, 150 ), ( 150, 180, 210, 250, 0, 35, 70, 100 ), ( 100, 150, 180, 210, 250, 0, 35, 70 ), ( 70, 100, 150, 180, 210, 250, 0, 35 ), ( 35, 70, 100, 150, 180, 210, 250, 0 ) ) class IForm(Form): def __init__(self): self.Text = 'Waiting' self.Size = Size(250, 150) self.Paint += self.OnPaint self.count = 0 self.timer = Timer(Container()) self.timer.Enabled = True self.timer.Interval = 80 self.timer.Tick += self.OnTick self.CenterToScreen() def OnTick(self, sender, event): self.count = self.count + 1 self.Refresh() def OnPaint(self, event): g = event.Graphics g.SmoothingMode = SmoothingMode.AntiAlias size = self.ClientSize g.TranslateTransform(size.Width/2, size.Height/2) for i in range(0, 8): color = Color.FromArgb(trs[self.count%8][i], 30, 30, 30) pen = Pen(color, 3) pen.StartCap = LineCap.Round pen.EndCap = LineCap.Round g.DrawLine(pen, 0, -10, 0, -40) g.RotateTransform(45) pen.Dispose() g.Dispose() Application.Run(IForm())
We draw eight lines with eight different alpha values.
self.timer = Timer(Container()) self.timer.Enabled = True self.timer.Interval = 80 self.timer.Tick += self.On
We use Timer
to create animation.
trs = ( ( 0, 35, 70, 100, 150, 180, 210, 250 ), ... )
This is a two dimensional collectino of transparency values used in this demo. There are 8 rows, each for one state. Each of the 8 lines will continuosly use these values.
pen = Pen(color, 3) pen.StartCap = LineCap.Round pen.EndCap = LineCap.Round
We make the lines a bit thicker, so that they are better visible. We draw the lines with rouded caps.
color = Color.FromArgb(trs[self.count%8][i], 30, 30, 30)
Here we define the transparency value for a line.
g.DrawLine(pen, 0, -10, 0, -40) g.RotateTransform(45)
We draw 8 lines. They are rotated clockwise.

In this chapter of the IronPython Winforms tutorial, we did some more advanced painting in Mono Winforms library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论