<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Mono Winforms 中的繪圖 > 原文: [http://zetcode.com/gui/csharpwinforms/painting/](http://zetcode.com/gui/csharpwinforms/painting/) 在 Mono Winforms 教程的這一部分中,我們將進行繪圖。 當我們想要更改或增強現有控件時,將使用繪圖。 或者,如果我們要從頭開始創建自定義控件。 要進行繪圖,我們使用 Winforms 庫提供的繪圖 API。 繪圖是在一種方法中完成的,我們將其插入`Paint`事件。 `System.Drawing`名稱空間提供對`GDI+`基本圖形功能的訪問。 `System.Drawing.Drawing2D`,`System.Drawing.Imaging`和`System.Drawing.Text`命名空間中提供了更高級的功能。 `Graphics`類提供了在表單上繪圖的方法。 ## 直線 我們的第一個示例將在`Form`控件上繪制線條。 `lines.cs` ```cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Lines"; Size = new Size(280, 270); ResizeRedraw = true; Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new 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 = new float[] {6f, 8f, 1f, 1f, 1f, 1f, 1f, 1f }; g.DrawLine(pen, 20, 200, 250, 200); g.Dispose(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們在表格上畫了五行。 每行具有不同的`DashStyle`。 ```cs ResizeRedraw = true; ``` 調整表單大小時,它會自動重繪。 這不是默認行為。 ```cs Paint += new PaintEventHandler(OnPaint); ``` 繪圖事件將傳遞給`OnPaint()`方法。 ```cs void OnPaint(object sender, PaintEventArgs e) { ... } ``` 這是`OnPaint()`方法的簽名。 ```cs Graphics g = e.Graphics; ``` 為了在表單上繪圖,我們必須獲取`Graphics`對象。 在窗體上繪圖實際上是在調用`Graphics`對象的各種方法。 ```cs Pen pen = new Pen(Color.Black, 1); pen.DashStyle = DashStyle.Dot; g.DrawLine(pen, 20, 40, 250, 40); ``` 我們創建一個`Pen`對象。 該對象用于繪制形狀的輪廓。 比我們設置點劃線`DashStyle`。 最后,我們用`DrawLine()`方法畫線。 第一個參數是鋼筆對象。 接下來的四個值是線的起點和終點的 x 和 y 值。 ```cs pen.DashPattern = new float[] {6f, 8f, 1f, 1f, 1f, 1f, 1f, 1f }; ``` 有幾個內置的`DashStyle`值。 我們可以使用`DashPattern`屬性來創建自己的樣式。 乍一看可能很難。 但是模式只是填充和空值的數組。 ```cs g.Dispose(); ``` 我們知道 C# 語言使用垃圾回收。 那么,為什么我們要明確釋放資源? 這是為了提高效率。 我們正在幫助垃圾收集器。 ![lines](https://img.kancloud.cn/ab/b2/abb2982c65a9e074433a99f2b959b1d2_280x271.jpg) 圖:直線 ## 色彩 Winforms 庫中的顏色表示 ARGB(alpha,紅色,綠色,藍色)顏色。 它是 Alpha,紅色,綠色和藍色(RGB)強度值的組合。 還有一些可以在繪圖中使用的預定義顏色名稱。 `colors.cs` ```cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Colors"; Size = new Size(360, 300); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.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(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們用 9 種不同的顏色繪制 9 個矩形。 ```cs 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.cs` ```cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Hatches"; Size = new Size(360, 300); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.Black, this.BackColor); g.FillRectangle(hb, 10, 15, 90, 60); hb = new HatchBrush(HatchStyle.Percent05, Color.Black, this.BackColor); g.FillRectangle(hb, 130, 15, 90, 60); hb = new HatchBrush(HatchStyle.SolidDiamond, Color.Black, this.BackColor); g.FillRectangle(hb, 250, 15, 90, 60); hb = new HatchBrush(HatchStyle.DiagonalBrick, Color.Black, this.BackColor); g.FillRectangle(hb, 10, 105, 90, 60); hb = new HatchBrush(HatchStyle.Divot, Color.Black, this.BackColor); g.FillRectangle(hb, 130, 105, 90, 60); hb = new HatchBrush(HatchStyle.Wave, Color.Black, this.BackColor); g.FillRectangle(hb, 250, 105, 90, 60); hb = new HatchBrush(HatchStyle.ZigZag, Color.Black, this.BackColor); g.FillRectangle(hb, 10, 195, 90, 60); hb = new HatchBrush(HatchStyle.Sphere, Color.Black, this.BackColor); g.FillRectangle(hb, 130, 195, 90, 60); hb = new HatchBrush(HatchStyle.Shingle, Color.Black, this.BackColor); g.FillRectangle(hb, 250, 195, 90, 60); hb.Dispose(); g.Dispose(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 這次,我們用九種不同的圖案(稱為剖面線)填充了九個矩形。 ```cs HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.Black, this.BackColor); ``` 在這里,我們創建一個`HatchBrush`對象。 參數是圖案填充樣式以及前景色和背景色。 背景顏色設置為表單的顏色,因此看起來就像我們在表單上繪制的一樣。 ```cs g.FillRectangle(hb, 10, 15, 90, 60); ``` 我們使用指定的陰影刷填充矩形。 ![Hatches](https://img.kancloud.cn/8e/42/8e428f8e37e62428a7fa48b58da0f136_360x301.jpg) 圖:通口 ## 漸變 在計算機圖形學中,漸變是從淺到深或從一種顏色到另一種顏色的陰影的平滑混合。 在 2D 繪圖程序和繪圖程序中,漸變用于創建彩色背景和特殊效果以及模擬燈光和陰影。 (answers.com) `gradients.cs` ```cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "Gradients"; Size = new Size(350, 350); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Point pt1 = new Point(5, 5); Point pt2 = new Point(25, 25); Brush lg = new LinearGradientBrush(pt1, pt2, Color.Red, Color.Black); g.FillRectangle(lg, 20, 20, 300, 40); pt1 = new Point(5, 25); pt2 = new Point(20, 2); lg = new LinearGradientBrush(pt1, pt2, Color.Yellow, Color.Black); g.FillRectangle(lg, 20, 80, 300, 40); pt1 = new Point(5, 25); pt2 = new Point(2, 2); lg = new LinearGradientBrush(pt1, pt2, Color.Green, Color.Black); g.FillRectangle(lg, 20, 140, 300, 40); pt1 = new Point(25, 25); pt2 = new Point(15, 25); lg = new LinearGradientBrush(pt1, pt2, Color.Blue, Color.Black); g.FillRectangle(lg, 20, 200, 300, 40); pt1 = new Point(0, 10); pt2 = new Point(0, 20); lg = new LinearGradientBrush(pt1, pt2, Color.Orange, Color.Black); g.FillRectangle(lg, 20, 260, 300, 40); lg.Dispose(); g.Dispose(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們繪制五個矩形,這些矩形填充有不同的線性漸變。 ```cs Point pt1 = new Point(5, 5); Point pt2 = new Point(25, 25); ``` 這兩個是線性漸變畫筆的控制點。 ```cs Brush lg = new LinearGradientBrush(pt1, pt2, Color.Red, Color.Black); ``` 我們創建`LinearGradientBrush`對象。 我們使用兩個控制點和兩種混合顏色。 ![Gradients](https://img.kancloud.cn/8a/81/8a81df45c2ba86502f7cf44e9dac321a_350x351.jpg) 圖:漸變 ## 畫線 要在 Winforms `Form`上繪制字符串,我們使用`DrawString()`方法。 `lyrics.cs` ```cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class MForm : Form { public MForm() { Text = "You know I'm No Good"; Size = new Size(380, 450); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font ft = new Font("Purisa", 10); SolidBrush br = new SolidBrush(Color.Black); PointF pt = new PointF(20.0f, 20.0f); g.DrawString("Meet you downstairs in the bar and heard", ft, br, pt); pt = new PointF(20.0f, 50.0f); g.DrawString("Your rolled up sleeves and your skull t-shirt", ft, br, pt); pt = new PointF(20.0f, 80.0f); g.DrawString("You say why did you do it with him today?", ft, br, pt); pt = new PointF(20.0f, 110.0f); g.DrawString("And sniffed me out like I was tanqueray", ft, br, pt); pt = new PointF(20.0f, 160.0f); g.DrawString("Cause you’re my fella, my guy", ft, br, pt); pt = new PointF(20.0f, 190.0f); g.DrawString("Hand me your stella and fly", ft, br, pt); pt = new PointF(20.0f, 220.0f); g.DrawString("By the time I’m out the door", ft, br, pt); pt = new PointF(20.0f, 250.0f); g.DrawString("You tear me down like roger moore", ft, br, pt); pt = new PointF(20.0f, 300.0f); g.DrawString("I cheated myself", ft, br, pt); pt = new PointF(20.0f, 330.0f); g.DrawString("Like I knew I would", ft, br, pt); pt = new PointF(20.0f, 360.0f); g.DrawString("I told ya, I was trouble", ft, br, pt); pt = new PointF(20.0f, 390.0f); g.DrawString("You know that I’m no good", ft, br, pt); g.Dispose(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在我們的示例中,我們在 Winforms 窗體上繪制歌曲的歌詞。 ```cs Font ft = new Font("Purisa", 10); ``` 我們使用 10 磅高的 Purisa 字體。 ```cs PointF pt = new PointF(20.0f, 20.0f); ``` 要在表單上繪制字符串,我們必須使用浮點值。 ```cs g.DrawString("Meet you downstairs in the bar and heard", ft, br, pt); ``` `DrawString()`方法采用以下參數:要繪制的文本,字體,筆刷和`PointF`對象。 ![Lyrics](https://img.kancloud.cn/5a/26/5a266b5eb832a41650bd7cb964b475a4_380x451.jpg) 圖:歌詞 ## 繪制圖像 在最后一個示例中,我們將在`Form`控件上繪制圖像。 `redrock.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private Bitmap castle; public MForm() { Text = "Red Rock"; loadImage(); ClientSize = new Size(castle.Width, castle.Height); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void loadImage() { try { castle = new Bitmap("redrock.png"); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle r = new Rectangle(1, 1, castle.Width, castle.Height); g.DrawImage(castle, r); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 此代碼示例在窗體上繪制城堡的圖像。 ```cs try { castle = new Bitmap("redrock.png"); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } ``` 我們加載城堡的圖像。 ```cs Rectangle r = new Rectangle(1, 1, castle.Width, castle.Height); ``` 我們確定將要繪制的矩形。 ```cs g.DrawImage(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>

                              哎呀哎呀视频在线观看