<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 繪圖 > 原文: [http://zetcode.com/tutorials/ironpythontutorial/painting/](http://zetcode.com/tutorials/ironpythontutorial/painting/) 在 IronPython Mono Winforms 教程的這一部分中,我們將進行繪圖。 當我們想要更改或增強現有控件時,將使用繪圖。 或者,如果我們要從頭開始創建自定義控件。 要進行繪圖,我們使用 Winforms 庫提供的繪圖 API。 繪圖是在一種方法中完成的,我們將其插入`Paint`事件。 `System.Drawing`名稱空間提供對`GDI+`基本圖形功能的訪問。 `System.Drawing.Drawing2D`,`System.Drawing.Imaging`和`System.Drawing.Text`名稱空間提供了更高級的功能。 `Graphics`類提供了在表單上進行繪制的方法。 ## 直線 我們的第一個示例將在`Form`控件上繪制線條。 `lines.py` ```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, Pen, Color from System.Drawing.Drawing2D import DashStyle class IForm(Form): def __init__(self): self.Text = 'Lines' self.Size = Size(280, 270) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics pen = Pen(Color.Black, 1) pen.DashStyle = DashStyle.Dot g.DrawLine(pen, 20, 40, 250, 40) pen.DashStyle = DashStyle.DashDot g.DrawLine(pen, 20, 80, 250, 80) pen.DashStyle = DashStyle.Dash g.DrawLine(pen, 20, 120, 250, 120) pen.DashStyle = DashStyle.DashDotDot g.DrawLine(pen, 20, 160, 250, 160) pen.DashPattern = (6, 8, 1, 1, 1, 1, 1, 1) g.DrawLine(pen, 20, 200, 250, 200) pen.Dispose() g.Dispose() Application.Run(IForm()) ``` 我們在表格上畫了五行。 每行具有不同的`DashStyle`。 ```py self.Paint += self.OnPaint ``` 繪圖事件將傳遞給`OnPaint()`方法。 ```py def OnPaint(self, event): ``` 這是`OnPaint()`方法的簽名。 ```py g = event.Graphics ``` 為了在表單上繪圖,我們必須獲取`Graphics`對象。 在窗體上繪圖實際上是在調用`Graphics`對象的各種方法。 ```py pen = Pen(Color.Black, 1) pen.DashStyle = DashStyle.Dot g.DrawLine(pen, 20, 40, 250, 40) ``` 我們創建一個`Pen`對象。 該對象用于繪制形狀的輪廓。 比我們設置點劃線`DashStyle`。 最后,我們用`DrawLine()`方法畫線。 第一個參數是鋼筆對象。 接下來的四個值是線的起點和終點的 x 和 y 值。 ```py pen.DashPattern = (6, 8, 1, 1, 1, 1, 1, 1) ``` 有幾個內置的`DashStyle`值。 我們可以使用`DashPattern`屬性來創建自己的樣式。 乍一看可能很難。 但是模式只是填充和空值的元組。 ```py pen.Dispose() g.Dispose() ``` 我們釋放資源。 ![lines](https://img.kancloud.cn/ab/b2/abb2982c65a9e074433a99f2b959b1d2_280x271.jpg) 圖:直線 ## 色彩 Winforms 庫中的顏色表示 ARGB(alpha,紅色,綠色,藍色)顏色。 它是 Alpha,紅色,綠色和藍色(RGB)強度值的組合。 還有一些可以在繪圖中使用的預定義顏色名稱。 `colors.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, ControlStyles from System.Drawing import Size, Brushes class IForm(Form): def __init__(self): self.Text = 'Colors' self.Size = Size(360, 300) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics g.FillRectangle(Brushes.Sienna, 10, 15, 90, 60) g.FillRectangle(Brushes.Green, 130, 15, 90, 60) g.FillRectangle(Brushes.Maroon, 250, 15, 90, 60) g.FillRectangle(Brushes.Chocolate, 10, 105, 90, 60) g.FillRectangle(Brushes.Gray, 130, 105, 90, 60) g.FillRectangle(Brushes.Coral, 250, 105, 90, 60) g.FillRectangle(Brushes.Brown, 10, 195, 90, 60) g.FillRectangle(Brushes.Teal, 130, 195, 90, 60) g.FillRectangle(Brushes.Goldenrod, 250, 195, 90, 60) g.Dispose() Application.Run(IForm()) ``` 我們用 9 種不同的顏色繪制 9 個矩形。 ```py g.FillRectangle(Brushes.Sienna, 10, 15, 90, 60) ``` `FillRectagle()`方法用畫筆填充指定的矩形。 畫筆可以是顏色或圖案。 有一些預定義的顏色可用。 我們可以從`Brushes`對象獲得它們。 最后四個值是左上角點的 x,y 值以及矩形的寬度和高度。 ![Colors](https://img.kancloud.cn/14/84/1484d0903ead923acde088ff9bf8ade4_360x301.jpg) 圖:顏色 ## `HatchBrush` `HatchBrush`對象用于填充形狀的內部。 我們可以使用幾種內置模式。 `hatches.py` ```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 from System.Drawing.Drawing2D import HatchBrush, HatchStyle class IForm(Form): def __init__(self): self.Text = 'Hatches' self.Size = Size(360, 300) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics hb = HatchBrush(HatchStyle.Cross, Color.Black, self.BackColor) g.FillRectangle(hb, 10, 15, 90, 60) hb = HatchBrush(HatchStyle.Percent05, Color.Black, self.BackColor) g.FillRectangle(hb, 130, 15, 90, 60) hb = HatchBrush(HatchStyle.SolidDiamond, Color.Black, self.BackColor) g.FillRectangle(hb, 250, 15, 90, 60) hb = HatchBrush(HatchStyle.DiagonalBrick, Color.Black, self.BackColor) g.FillRectangle(hb, 10, 105, 90, 60) hb = HatchBrush(HatchStyle.Divot, Color.Black, self.BackColor) g.FillRectangle(hb, 130, 105, 90, 60) hb = HatchBrush(HatchStyle.Wave, Color.Black, self.BackColor) g.FillRectangle(hb, 250, 105, 90, 60) hb = HatchBrush(HatchStyle.ZigZag, Color.Black, self.BackColor) g.FillRectangle(hb, 10, 195, 90, 60) hb = HatchBrush(HatchStyle.Sphere, Color.Black, self.BackColor) g.FillRectangle(hb, 130, 195, 90, 60) hb = HatchBrush(HatchStyle.Shingle, Color.Black, self.BackColor) g.FillRectangle(hb, 250, 195, 90, 60) hb.Dispose() g.Dispose() Application.Run(IForm()) ``` 這次,我們用九種不同的圖案(稱為剖面線)填充了九個矩形。 ```py hb = HatchBrush(HatchStyle.Cross, Color.Black, self.BackColor) ``` 在這里,我們創建一個`HatchBrush`對象。 參數是圖案填充樣式以及前景色和背景色。 背景顏色設置為表單的顏色,因此看起來就像我們在表單上繪制的一樣。 ```py g.FillRectangle(hb, 10, 15, 90, 60) ``` 我們使用指定的陰影刷填充矩形。 ![Hatches](https://img.kancloud.cn/8e/42/8e428f8e37e62428a7fa48b58da0f136_360x301.jpg) 圖:通口 ## 基本對象 下面的示例在窗體控件上繪制一些基本形狀。 `basicshapes.py` ```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, Rectangle, Brushes, Pens, Point from System.Drawing.Drawing2D import SmoothingMode from System import Array class IForm(Form): def __init__(self): self.Text = 'Basic shapes' self.Size = Size(420, 280) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics g.SmoothingMode = SmoothingMode.AntiAlias g.FillRectangle(Brushes.Gray, 20, 20, 120, 80) g.FillRectangle(Brushes.Gray, 180, 20, 80, 80) g.FillEllipse(Brushes.Gray, 30, 120, 100, 100) g.FillEllipse(Brushes.Gray, 160, 130, 100, 70) p1 = Point(300, 40) p2 = Point(340, 15) p3 = Point(380, 40) p4 = Point(380, 80) p5 = Point(340, 105) p6 = Point(300, 80) g.FillPolygon(Brushes.Gray, Array[Point]([p1, p2, p3, p4, p5, p6])) g.FillPie(Brushes.Gray, Rectangle(290, 130, 90, 90), 0, 315) g.Dispose() Application.Run(IForm()) ``` 該代碼示例在表單上繪制六個形狀。 矩形,正方形,圓形,橢圓形,多邊形和扇形。 ```py g.SmoothingMode = SmoothingMode.AntiAlias ``` 這使繪圖更平滑。 ```py g.FillRectangle(Brushes.Gray, 20, 20, 120, 80) ``` 這條線用灰色填充矩形。 參數是畫筆顏色,矩形左上角的 x,y 坐標以及矩形的寬度和高度。 ```py g.FillPolygon(Brushes.Gray, Array[Point]([p1, p2, p3, p4, p5, p6])) ``` 這條線繪制了一個包含六個單點的多邊形。 ```py g.FillPie(Brushes.Gray, Rectangle(290, 130, 90, 90), 0, 315) ``` 這條線畫了一個餡餅。 最后兩個參數是起始角度和后掠角度。 以度為單位。 ![Basic shapes](https://img.kancloud.cn/44/4e/444e954fbcd5d7bd92925d4da5524aa1_420x281.jpg) 圖:基本形狀 ## 繪制直線 要在 Winforms `Form`上繪制字符串,我們使用`DrawString()`方法。 `lyrics.py` ```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, Font, SolidBrush from System.Drawing import PointF, Color class IForm(Form): def __init__(self): self.Text = "You know I'm No Good" self.Size = Size(380, 450) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics ft = Font("Purisa", 10) br = SolidBrush(Color.Black) pt = PointF(20.0, 20.0) g.DrawString("Meet you downstairs in the bar and heard", ft, br, pt) pt = PointF(20.0, 50.0) g.DrawString("Your rolled up sleeves and your skull t-shirt", ft, br, pt) pt = PointF(20.0, 80.0) g.DrawString("You say why did you do it with him today?", ft, br, pt) pt = PointF(20.0, 110.0) g.DrawString("And sniffed me out like I was tanqueray", ft, br, pt) pt = PointF(20.0, 160.0) g.DrawString("Cause you're my fella, my guy", ft, br, pt) pt = PointF(20.0, 190.0) g.DrawString("Hand me your stella and fly", ft, br, pt) pt = PointF(20.0, 220.0) g.DrawString("By the time I'm out the door", ft, br, pt) pt = PointF(20.0, 250.0) g.DrawString("You tear me down like roger moore", ft, br, pt) pt = PointF(20.0, 300.0) g.DrawString("I cheated myself", ft, br, pt) pt = PointF(20.0, 330.0) g.DrawString("Like I knew I would", ft, br, pt) pt = PointF(20.0, 360.0) g.DrawString("I told ya, I was trouble", ft, br, pt) pt = PointF(20.0, 390.0) g.DrawString("You know that I'm no good", ft, br, pt) g.Dispose() Application.Run(IForm()) ``` 在我們的示例中,我們在 Winforms 窗體上繪制歌曲的歌詞。 ```py ft = Font("Purisa", 10) ``` 我們使用 10 磅高的 Purisa 字體。 ```py pt = PointF(20.0, 20.0) ``` 要在表單上繪制字符串,我們必須使用浮點值。 ```py g.DrawString("Meet you downstairs in the bar and heard", ft, br, pt) ``` `DrawString()`方法采用以下參數:要繪制的文本,字體,筆刷和`PointF`對象。 ![Lyrics](https://img.kancloud.cn/e3/cc/e3cc91b2d1a76b590f019ba3607dc8c9_380x451.jpg) 圖:歌詞 ## 繪制圖像 在最后一個示例中,我們將在`Form`控件上繪制圖像。 `redrock.py` ```py #!/usr/bin/ipy import sys import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form from System.Drawing import Size, Bitmap, Rectangle class IForm(Form): def __init__(self): self.Text = 'Red Rock' self.Size = Size(200, 150) self.loadImage() self.Size = Size(self.castle.Width, self.castle.Height) self.Paint += self.OnPaint self.CenterToScreen() def loadImage(self): try: self.castle = Bitmap("redrock.png") except Exception, e: print e.msg sys.exit(1) def OnPaint(self, event): g = event.Graphics r = Rectangle(1, 1, self.castle.Width, self.castle.Height) g.DrawImage(self.castle, r) g.Dispose() Application.Run(IForm()) ``` 此代碼示例在窗體上繪制城堡的圖像。 ```py def loadImage(self): try: self.castle = Bitmap("redrock.png") except Exception, e: print e.msg sys.exit(1) ``` 我們加載城堡的圖像。 ```py r = Rectangle(1, 1, self.castle.Width, self.castle.Height) ``` 我們確定將要繪制的矩形。 ```py g.DrawImage(self.castle, r) ``` 這條線實際上繪制圖像。 ![Image](https://img.kancloud.cn/17/34/1734df1bd7937808d95546914a360ebd_458x281.jpg) 圖:圖像 在本章中,我們在 Mono Winforms 庫中做了一些繪圖。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看