亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

圖形和 Windows 窗體

系統 1966 0

圖形和 Windows 窗體

介紹 GDI+

公共語言運行庫使用 Windows 圖形設備接口 (GDI) 的高級版本,其名稱為 GDI+。GDI+ 旨在提供良好的性能和易用性。它支持二維圖形、版式和圖像。

新的二維功能包括下列內容:

  • 對所有圖形基元的 Alpha 混合支持
  • 消除鋸齒
  • 漸變填充和紋理填充
  • 寬線
  • 基數樣條
  • 可縮放區域
  • 浮點坐標
  • 復合線
  • 嵌入鋼筆
  • 高質量的篩選和縮放
  • 大量的線型和筆尖選項

圖像支持包括下列內容:

  • 對圖像文件格式(如 .jpeg、.png、.gif、.bmp、.tiff、.exif 和 .icon)的本機支持
  • 用于編碼和解碼任意光柵圖像格式的公共接口
  • 用于動態添加新圖像文件格式的可擴展結構
  • 對通用點操作(如亮度、對比度、色彩平衡、模糊和溶解)的本機圖像處理支持。對通用轉換(如旋轉、裁切等)的支持。

顏色管理

對 sRGB、ICM2 和 sRGB64 的支持

版式支持包括下列內容:

  • 本機 ClearType 支持
  • 紋理填充和漸變填充的文本
  • 在所有平臺上對 Unicode 的完全支持
  • 對所有 Windows 2000 腳本的支持
  • Unicode 3.0 標準的更新
  • 文本行服務支持,可使文本更具可讀性

GDI+ 可與 Windows 窗體和 Web 窗體一起使用。例如,Web 窗體控件可使用基于用戶輸入的 GDI+ 動態生成 .jpeg 文件,并從 Web 頁引用它。

本節集中討論 Windows 窗體。

GDI 和 GDI+ 之間的差異

本節的目的是幫助您了解使用 GDI+ 的基礎知識。開始之前,有必要指出 GDI 和 GDI+ 之間的最大差異。GDI 具有有狀態的編程模型,而 GDI+ 具有無狀態的編程模型。使用 GDI,可在繪圖表面上設置屬性(如前景色和背景色),然后在它上面進行繪制。例如,若要繪制黑色文本字符串,代碼有點類似于下面的示例。

function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i <style type="text/css"> td.code { padding:0,10,0,10; border-style:solid; border-width:1; border-bottom:0; border-top:0; border-right:0; border-color:cccccc; background-color:ffffee } td.tab { text-align:center; font:8pt verdana; width:15%; padding:3,3,3,3; border-style:solid; border-width:1; border-right:0; border-color:black; background-color:eeeeee; cursor:hand } td.backtab { text-align:center; font: 8pt verdana; width:15%; padding:3,3,3,3; border-style:solid; border-width:1; border-right:0; border-color:black; background-color:cccccc; cursor:hand } td.space { width:70%; font: 8pt verdana; padding:0,0,0,0; border-style:solid; border-bottom:0; border-right:0; border-width:1; border-color:cccccc; border-left-color:black; background-color:white } </style>

            Graphics g ;
g.ForeColor = Color.Black;
g.BackColor = Color.White;
g.Font = new Font("Times New Roman", 26);
g.DrawString("Hello, World", 0, 0);

          
            Dim g as Graphics
g.ForeColor = Color.Black
g.BackColor = Color.White
g.Font = new Font("Times New Roman", 26)
g.DrawString("Hello, World", 0, 0)

          
C# VB

使用 GDI+ 時,始終將要使用的屬性作為繪圖命令的一部分傳遞。上面的代碼更改為如下所示。

            Graphics g ;
Color foreColor = Color.Black;
Color backColor = Color.White;
Font font = new Font("Times New Roman", 26);
g.FillRectangle(new SolidBrush(backColor), ClientRectangle);
g.DrawString("Hello World", font, new SolidBrush(foreColor), 15, 15);

          
            Dim g As Graphics;
Dim foreColor As Color = Color.Black;
Dim backColor As Color = Color.White;
Dim vbFont As New Font("Times New Roman", 26);
g.FillRectangle(New SolidBrush(backColor), ClientRectangle);
g.DrawString("Hello World", vbFont, New SolidBrush(foreColor), 15, 15);

          
C# VB
GDI+ 命名空間

GDI+ 類駐留于 System.Drawing 、 System.Drawing.Drawing2D System.Drawing.Imaging System.Drawing.Text 命名空間中。這些命名空間包含在程序集 System.Drawing.DLL 中。

創建圖形對象

GDI+ 繪圖表面由 Graphics 類表示。為了使用 GDI+,首先需要一個對圖形對象的引用。通常,在控件或窗體的 Paint 事件中或者在

可以通過為 Paint 事件創建事件處理程序來處理該事件。

            public class GdiPlusDemo : Form {

   public GdiPlusDemo () {
        //Hook the paint event of the form.
        this.Paint += new PaintEventHandler(form1_Paint);
    }

    private void form1_Paint(object sender, PaintEventArgs pe) {
        Graphics g = pe.Graphics;

        //Simply fill a rectangle with red.
        g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
    }
}

          
            Public Class GdiPlusDemo : Inherits Form

    Public Sub New()
        ' Hook the paint event of the form.
        AddHandler Me.Paint, AddressOf form1_Paint
    End Sub

    Private Sub form1_Paint(sender As Object, pe As PaintEventArgs)
        Dim g As Graphics = pe.Graphics

        ' Simply fill a rectangle with red.
        g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
    End Sub
End Class

          
C# VB

更常用的是,可以創建 OnPaint 方法的子類并重寫該方法。

            public class GdiPlusDemo : Form {

   public GdiPlusDemo () {
   }

   protected override void OnPaint(PaintEventArgs pe) {
        Graphics g = pe.Graphics;
        g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
    }
}

          
            Public Class GdiPlusDemo : Inherits Form

   Public Sub New()
   End Sub

   Protected Overrides Sub OnPaint(pe As PaintEventArgs)
        Dim g As Graphics = pe.Graphics
        g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
   End Sub
End Class

          
C# VB

還可以從 Image 的任何派生類創建 Graphics 對象實例。

            Bitmap newBitmap = new Bitmap(600,400,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
newBitmap.Save("c:\\temp\\TestImage.jpg", ImageFormat.Jpeg) ;

          
            Dim newBitmap As Bitmap = New Bitmap(600,400,PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(newBitmap)
g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
newBitmap.Save("c:\temp\TestImage.jpg", ImageFormat.Jpeg)

          
C# VB

創建 Graphics 對象后,可以使用它繪制線、填充形狀、繪制文本等。與 Graphics 對象一起使用的主要對象如下所示。

Brush

用于使用圖案、顏色或位圖填充封閉表面。

Pen

用于繪制線和多邊形,包括矩形、弧線和扇形。

Font

用于描述用來呈現文本的字體。

Color

用于描述用來呈現特定對象的顏色。在 GDI+ 中,顏色可以是 Alpha 混合效果的。

Alpha 混合
顏色可以是 Alpha 混合的,這使得易于創建基于顏色疊加的效果。例如,下面的示例繪制一個紅色矩形,然后在不遮掩底層的紅色矩形的情況下疊加一個黃色矩形。

            Graphics g = pe.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 650,
                               400, 100, 100);

          
            Dim g As Graphics = pe.Graphics
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100)
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 650, _
                               400, 100, 100)

          
C# VB
使用畫筆
使用畫筆填充形狀和路徑的內部。例如,若要創建紅色矩形,請參見下面的示例。

            Graphics g = pe.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100);

          
            Dim g As Graphics = pe.Graphics
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100)

          
C# VB

畫筆可以是純色的、帶陰影的、帶紋理的或漸變的。陰影畫筆只不過是使用圖案進行繪畫的畫筆。

            Graphics g = pe.Graphics;
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green,
                               Color.FromArgb(100, Color.Yellow));
g.FillEllipse(hb, 250, 10, 100, 100);

          
            Dim g As Graphics = pe.Graphics
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green, _
                               Color.FromArgb(100, Color.Yellow))
g.FillEllipse(hb, 250, 10, 100, 100)

          
C# VB

帶紋理的畫筆使用位圖繪畫。例如,下面的代碼使用紋理畫筆繪制窗體的背景,然后對它應用白色"沖洗"以沖淡位圖中顏色的色調。

            Graphics g = pe.Graphics;
Image colorbars = new Bitmap("colorbars.jpg");
Brush backgroundBrush = new TextureBrush(colorbars);
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

          
            Dim g As Graphics = pe.Graphics
Dim colorbars As Image = new Bitmap("colorbars.jpg")
Dim backgroundBrush As Brush = new TextureBrush(colorbars)
g.FillRectangle(backgroundBrush, ClientRectangle)
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

          
C# VB

LinearGradientBrush 使用兩種顏色繪畫。它根據在畫筆上設置的屬性填充給定的形狀,逐漸從一種顏色過渡到另一種顏色。例如,可以用下面所示代碼創建下列填充效果。



            Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,
                                            LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);

          
            Dim r As Rectangle = new Rectangle(500, 300, 100, 100);
Dim lb As LinearGradientBrush = new LinearGradientBrush(r, Color.Red, Color.Yellow, _
                                                       LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);

          
C# VB

PathGradient 畫筆允許創建更復雜的效果,如下列形狀。

它是用下面的代碼創建的。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

    GraphicsPath path = new GraphicsPath(new Point[] {
        new Point(40, 140),
        new Point(275, 200),
        new Point(105, 225),
        new Point(190, 300),
        new Point(50, 350),
        new Point(20, 180),
        }, new byte[] {
            (byte)PathPointType.Start,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Line,
            (byte)PathPointType.Line,
            });

    PathGradientBrush pgb = new PathGradientBrush(path);
    pgb.SurroundColors = new Color[] {
        Color.Green,
        Color.Yellow,
        Color.Red,
        Color.Blue,
        Color.Orange,
        Color.White,
    };

    e.Graphics.FillPath(pgb, path);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle)
    e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

    Dim path As New GraphicsPath(New Point() { _
        New Point(40, 140), _
        New Point(275, 200), _
        New Point(105, 225), _
        new Point(190, 300), _
        new Point(50, 350), _
        new Point(20, 180), _
        }, New Byte() { _
            CType(PathPointType.Start, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Line, Byte), _
            CType(PathPointType.Line, Byte), _
            })

    Dim pgb As New PathGradientBrush(path)
    pgb.SurroundColors = new Color() { _
        Color.Green, _
        Color.Yellow, _
        Color.Red, _
        Color.Blue, _
        Color.Orange, _
        Color.White, _
    }

    e.Graphics.FillPath(pgb, path)
End Sub

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 畫筆示例</td> </tr></tbody></table> <br>[<a target="_top">運行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

使用鋼筆

使用鋼筆繪制直線和曲線。可以設置屬性,如 PenType 、 DashStyle 、 Width Color EndCap ,以控制 Pen 如何進行繪制。

下面的代碼繪制一條曲線。

            Graphics g;
//Create a pen 20 pixels wide that is and purple and partially transparent. 
Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
//Make it a dashed pen.
Pen penExample.DashStyle = DashStyle.Dash;
//Make the ends round.
Pen penExample.StartCap = LineCap.Round;
Pen penExample.EndCap = LineCap.Round;

//Now draw a curve using the pen. 
g.DrawCurve(penExample, new Point[] {
            new Point(200, 140),
            new Point(700, 240),
            new Point(500, 340),
            new Point(140, 140),
            new Point(40, 340),
});

          
            Dim g As Graphics
' Create a pen 20 pixels wide that is and purple and partially transparent.
Dim penExample As New Pen(Color.FromArgb(150, Color.Purple), 20)
' Make it a dashed pen.
Dim penExample.DashStyle As Pen = DashStyle.Dash
' Make the ends round.
Dim penExample.StartCap As Pen = LineCap.Round
Dim penExample.EndCap As Pen= LineCap.Round

' Now draw a curve using the pen
g.DrawCurve(penExample, New Point() { _
            New Point(200, 140), _
            New Point(700, 240), _
            New Point(500, 340), _
            New Point(140, 140), _
            New Point(40, 340), _
})

          
C# VB

還可以使用帶紋理的畫筆將紋理用作鋼筆的填充效果。

            Graphics g;
Brush textureBrush = new TextureBrush(new Bitmap("Boiling Point.jpg"));
Pen penExample = new Pen(textureBrush, 25);
penExample.DashStyle = DashStyle.DashDotDot;
penExample.StartCap = LineCap.Triangle;
penExample.EndCap = LineCap.Round;
g.DrawLine(penExample, 10,450,550,400);

          
            Dim g As Graphics
Dim textureBrush As New TextureBrush(New Bitmap("Boiling Point.jpg"))
Dim penExample As New Pen(textureBrush, 25)
penExample.DashStyle = DashStyle.DashDotDot
penExample.StartCap = LineCap.Triangle
penExample.EndCap = LineCap.Round
g.DrawLine(penExample, 10,450,550,400)

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 筆示例</td> </tr></tbody></table> <br>[<a target="_top">運行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

繪制文本

Graphics 對象的 DrawString 方法在繪圖表面上呈現文本。將要使用的字體和顏色傳遞給 DrawString 方法。例如,下面的代碼使用窗體的字體和黑色畫筆顯示文本"Hello World"。

            protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
    g.DrawString("Hello World", this.Font, new SolidBrush(Color.Black), 10,10);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim g As Graphics = e.Graphics
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
    g.DrawString("Hello World", Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub

          
C# VB

因為 DrawString 采用畫筆,所以可以使用任何畫筆呈現文本。其中包括紋理畫筆。例如,下面的代碼呈現具有大理石效果和背景陰影的文本。

            protected override void OnPaint(PaintEventArgs e) {
    TextureBrush titleBrush = new TextureBrush(new Bitmap("marble.jpg"));
    TextureBrush backgroundBrush = new TextureBrush(new Bitmap("colorbars.jpg"));
    SolidBrush titleShadowBrush = new SolidBrush(Color.FromArgb(70, Color.Black));
    Font titleFont = new Font("Lucida Sans Unicode", 60);
    string titleText = "Graphics  Samples";

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

    e.Graphics.DrawString(titleText, titleFont, titleShadowBrush, 15, 15);
    e.Graphics.DrawString(titleText, titleFont, titleBrush, 10, 10);

}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim titleBrush As New TextureBrush(New Bitmap("marble.jpg"))
    Dim backgroundBrush As New TextureBrush(New Bitmap("colorbars.jpg"))
    Dim titleShadowBrush As New SolidBrush(Color.FromArgb(70, Color.Black))
    Dim titleFont As New Font("Lucida Sans Unicode", 60)
    Dim titleText As String = "Graphics  Samples"

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle)
    e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

    e.Graphics.DrawString(titleText, titleFont, titleShadowBrush, 15, 15)
    e.Graphics.DrawString(titleText, titleFont, titleBrush, 10, 10)

End Sub

          
C# VB

如果提供帶有 Rectangle DrawString ,則文本將換行以適合該矩形。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 12);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 12)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)
End Sub

          
C# VB

可以使用 StringFormat 對象控制如何繪制文本。例如,下面的代碼使您得以繪制在特定區域內居中的文本。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 8);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
    StringFormat format = new StringFormat();
    format.Alignment=StringAlignment.Center;
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle,format);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 8)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
    Dim format As New StringFormat()
    format.Alignment = StringAlignment.Center
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle,format)
End Sub

          
C# VB

如果要在繪制字符串時確定該字符串的長度,可使用 MeasureString 。例如,若要將某個字符串在窗體上居中顯示,請使用下面的代碼。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    string textToDraw="Hello Symetrical World";

    Font textFont = new Font("Lucida Sans Unicode", 8);
    float windowCenter=this.DisplayRectangle.Width/2;
    SizeF stringSize=e.Graphics.MeasureString(textToDraw, textFont);
    float startPos=windowCenter-(stringSize.Width/2);

    e.Graphics.DrawString(textToDraw, textFont, new SolidBrush(Color.Blue), startPos, 40);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Dim textToDraw As String ="Hello Symetrical World"

    Dim textFont As New Font("Lucida Sans Unicode", 8)
    Dim windowCenter As Double = this.DisplayRectangle.Width/2
    Dim stringSize As SizeF = e.Graphics.MeasureString(textToDraw, textFont)
    Dim startPos As Double = windowCenter-(stringSize.Width/2)

    e.Graphics.DrawString(textToDraw, textFont, new SolidBrush(Color.Blue), startPos, 40)
End Sub

          
C# VB

MeasureString 還可用于確定呈現多少行和多少個字符。例如,我們可以確定在上一個討論過的文本示例中呈現多少行和多少個字符。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 12);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    int lines;
    int characters;
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    string whatRenderedText;

    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);

    e.Graphics.MeasureString(flowedText, textFont, rectangle.Size,
                    new StringFormat(), out characters, out lines);
    whatRenderedText="We printed " + characters + " characters and " + lines + " lines";

    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);

    e.Graphics.DrawString(whatRenderedText, this.Font, new SolidBrush(Color.Black), 10,10);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 12)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    Dim lines As Integer
    Dim characters As Integer
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    Dim whatRenderedText As String

    e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)

    e.Graphics.MeasureString(flowedText, textFont, rectangle.Size, _
                    new StringFormat(), characters, lines)
    whatRenderedText = "We printed " & characters & " characters and " & lines & " lines"

    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)

    e.Graphics.DrawString(whatRenderedText, Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub

          
C# VB

GDI+ 完全支持 Unicode。這意味著可以用任何語言呈現文本。例如,下面的代碼用日語繪制字符串(必須安裝有日語語言包)。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    try {
        Font japaneseFont = new Font("MS Mincho", 32);
        string japaneseText = new string(new char[] {(char)31169, (char)12398, (char)21517,
                    (char)21069, (char)12399, (char)12463, (char)12522, (char)12473,
                    (char)12391, (char)12377, (char)12290});

        e.Graphics.DrawString(japaneseText, japaneseFont, new SolidBrush(Color.Blue), 20, 40);
    } catch (Exception ex) {
         MessageBox.Show("You need to install the Japanese language pack to run this sample");
         Application.Exit();
    }
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Try
        Dim japaneseFont As New Font("MS Mincho", 32)
        Dim japaneseText As New String(new char() {CType(31169, Char), CType(12398, Char), _
                    CType(21517, Char), CType(21069, Char), CType(12399, Char), _
                    CType(12463, Char), CType(12522, Char), CType(12473, Char), _
                    CType(12391, Char), CType(12377, Char), CType(12290, Char)})
        e.Graphics.DrawString(japaneseText, japaneseFont, new SolidBrush(Color.Blue), 20, 40)
    Catch ex As Exception
         MessageBox.Show("You need to install the Japanese language pack to run this sample")
         Application.Exit()
    End Try
End Sub

          
C# VB


<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 文本示例</td> </tr></tbody></table> <br>[<a target="_top">運行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

使用圖像

GDI+ 完全支持各種圖像格式,包括 .jpeg、.png、.gif、.bmp、.tiff、.exif 和 .icon 文件。

呈現圖像非常簡單。例如,下面的代碼呈現一個 .jpeg 圖像。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
    e.Graphics.DrawImage(new Bitmap("sample.jpg"), 29, 20, 283, 212);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
    e.Graphics.DrawImage(New Bitmap("sample.jpg"), 29, 20, 283, 212)
End Sub

          
C# VB

使用位圖作為呈現圖面也非常簡單。下面的代碼將一些文本和線呈現到位圖上,然后將該位圖作為 .png 文件保存到磁盤中。

            Bitmap newBitmap = new Bitmap(800,600,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,800,600));

Font textFont = new Font("Lucida Sans Unicode", 12);
RectangleF rectangle = new RectangleF(100, 100, 250, 350);
int lines;
int characters;
string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
    + "developing Windows applications that combines the simplicity of the Visual\n"
    + "Basic 6.0 programming model with the power and flexibility of the common\n"
    + "language runtime.\n"
    + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
    + "deployment features of the common language runtime to offer reduced deployment\n"
    + "costs and higher application robustness over time. This significantly lowers the\n"
    + "maintenance costs (TCO) for applications\n"
    + "written in Windows Forms.\n"
    + "Architecture for controls: Windows Forms offers an architecture for\n"
    + "controls and control containers that is based on concrete implementation of the\n"
    + "control and container classes. This significantly reduces\n"
    + "control-container interoperability issues.\n";

g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
penExample.DashStyle = DashStyle.Dash;
penExample.StartCap = LineCap.Round;
penExample.EndCap = LineCap.Round;
g.DrawCurve(penExample, new Point[] {
            new Point(200, 140),
            new Point(700, 240),
            new Point(500, 340),
            new Point(140, 140),
            new Point(40, 340),
});

newBitmap.Save("TestImage.png", ImageFormat.Png) ;

          
            Dim newBitmap As New Bitmap(800,600,PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(newBitmap)
g.FillRectangle(New SolidBrush(Color.White), new Rectangle(0,0,800,600))

Dim textFont As New Font("Lucida Sans Unicode", 12)
Dim rectangle As New RectangleF(100, 100, 250, 350)
Dim Lines As Integer
Dim Characters As Integer
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
    & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
    & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
    & "language runtime." & ControlChars.CrLf _
    & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
    & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
    & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
    & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
    & "written in Windows Forms." & ControlChars.CrLf _
    & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
    & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
    & "control and container classes. This significantly reduces" & ControlChars.CrLf _
    & "control-container interoperability issues." & ControlChars.CrLf

g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle)
Dim penExample As New Pen(Color.FromArgb(150, Color.Purple), 20)
penExample.DashStyle = DashStyle.Dash
penExample.StartCap = LineCap.Round
penExample.EndCap = LineCap.Round
g.DrawCurve(penExample, new Point() { _
            new Point(200, 140), _
            new Point(700, 240), _
            new Point(500, 340), _
            new Point(140, 140), _
            new Point(40, 340), _
})

newBitmap.Save("TestImage.png", ImageFormat.Png)

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 圖像示例</td> </tr></tbody></table> <br>[<a target="_top">運行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

其他信息
標準鋼筆和畫筆

Pen Brush 類包括一組用于所有已知顏色的標準純色鋼筆和畫筆。

消除鋸齒
Graphics.SmoothingMode 設置為 SmoothingMode.AntiAlias 將導致更銳化的文本和圖形。
圖形對象的范圍
Paint 事件的參數 ( PaintEventArgs ) 中包含的圖形對象根據 Paint 事件處理程序的返回結果進行處置。因此,不應保持對范圍超出 Paint 事件之外的此 Graphics 對象的引用。嘗試在 Paint 事件之外使用此 Graphics 對象將出現不可預知的結果。
處理調整大小

默認情況下,當調整控件或窗體大小時不引發 Paint 事件。如果希望在調整窗體大小時引發 Paint 事件,則需要相應地設置控件樣式。

            public MyForm() {
    SetStyle(ControlStyles.ResizeRedraw,true);
}

          
            Public Sub MyForm()
    SetStyle(ControlStyles.ResizeRedraw,True)
End Sub

          
C# VB

轉自 http://www.gotdotnet.com/

PrintDocument PrintPage 事件中獲取對圖形對象的引用。

圖形和 Windows 窗體


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中国一级特黄 | 97欧美精品一区二区三区 | 激情综合五月亚洲婷婷 | 99视频在线永久免费观看 | 久久最近最新中文字幕大全 | 久久久91精品国产一区二区三区 | 97影院理伦在线观看 | 99re久久在热线播放最新地址 | 亚洲精品日韩在线一区 | 色综合久久中文色婷婷 | 久久精品成人免费看 | 高清黄色直接看 | 91视频最新地址 | 九九视频热| 神马影院午夜我不卡 | 国产欧美日韩在线播放 | 色久综合 | 久久精热 | 中国美女hdxxxxx | 超碰最新上传 | 国产视频最新 | 日本高清视频一区二区三区 | 亚洲综合在线一区 | 波多野结衣一区二区三区四区 | 日本免费不卡 | 中文线码中文高清播放中 | 国产精品亚洲欧美日韩区 | 全黄大全大色全免费大片 | 日韩精品欧美精品中文精品 | 久久精品国产eeuss | 一级片在线免费观看 | 久久香蕉国产线看观看网站 | 伊人狼人综合网 | 精品国产一区二区 | 真实国产乱弄免费视频 | 欧美成人免费夜夜黄啪啪 | 日本精品a在线 | 国产午夜精品一二区理论影院 | 亚洲欧美日韩在线不卡中文 | 97影院九七影院理论片 | 粗大猛烈进出呻吟声的视频 |