本入门教程中的所有案例均已测试通过,具体参考入门1中的使用方法。
09 基本控件2
14 对话框1
15 对话框2
16 拖拽功能
17 绘图1
18 绘图2
19 绘图3
20 绘图4
21 进度条
22 游戏——贪吃蛇
功能:打开文件夹对话框
本案例中创建按钮,其功能用于触发事件,弹出选择文件夹窗口。 对应的本文框会显示路径地址。
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
import System.Windows.Forms
from System.Windows.Forms import *
class IForm(System.Windows.Forms.Form):
def __init__(self):
self._textBox1 = System.Windows.Forms.TextBox()
self._button1 = System.Windows.Forms.Button()
self._textBox1.Location = System.Drawing.Point(20, 50)
self._textBox1.Size = System.Drawing.Size(120, 20)
self._button1.Location = System.Drawing.Point(150, 50)
self._button1.Size = System.Drawing.Size(50, 20)
self._button1.Text = "button"
self._button1.Click += self.OnClicked
self.Controls.Add(self._button1)
self.Controls.Add(self._textBox1)
self.Text = "FolderBrowserDialog"
self.CenterToScreen()
def OnClicked(self, sender, e):
dialog = FolderBrowserDialog()
if (dialog.ShowDialog(self) == DialogResult.OK):
self._textBox1.Text = dialog.SelectedPath
System.Windows.Forms.Application.Run(IForm())
效果展示:
功能:打开颜色选择夹对话框
本案例中创建按钮,其功能用于触发事件,弹出颜色选择窗口。 对应的色块会改变颜色。
# encoding: utf-8
import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import Point, Size, Color, SolidBrush, Rectangle
RECT_WIDTH = 200
RECT_HEIGHT = 100
class IForm(Form):
def __init__(self):
self.Text = "ColorDialog"
self.color = Color.Blue
self._button1 = Button()
self._button1.Location = Point(150, 50)
self._button1.Size = Size(50, 20)
self._button1.Text = "button"
self.Controls.Add(self._button1)
self._button1.Click += self.OnClicked
self.SetStyle(ControlStyles.ResizeRedraw, True)
self.Paint += self.OnPaint
self.CenterToScreen()
def OnPaint(self, event):
g = event.Graphics
self.LocateRect()
brush = SolidBrush(self.color)
g.FillRectangle(brush, self.r)
def OnClicked(self, sender, events):
dialog = ColorDialog()
if (dialog.ShowDialog(self) == DialogResult.OK):
self.color = dialog.Color
self.Invalidate()
def LocateRect(self):
x = (self.ClientSize.Width - RECT_WIDTH) / 2
y = (self.ClientSize.Height - RECT_HEIGHT) / 2
self.r = Rectangle(x, y, RECT_WIDTH, RECT_HEIGHT)
IForm().ShowDialog()
效果展示:
完整的本地电子版本参阅入门1的其他。