GDI+でVBで描画する

Graphicsオブジェクトを作成して、提供されているメソッドを使用して描画することで、VBでGDI+を使って描画できます。以下にVBでGDI+を使用して描画する方法の簡単な例を示します。

Imports System.Drawing
Public Class Form1
Inherits Form
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
' 创建Graphics对象
Dim g As Graphics = e.Graphics
' 创建画笔
Dim brush As New SolidBrush(Color.Red)
' 绘制矩形
g.FillRectangle(brush, New Rectangle(50, 50, 100, 100))
' 绘制椭圆
g.FillEllipse(brush, New Rectangle(200, 50, 100, 100))
' 绘制文本
g.DrawString("Hello, GDI+!", New Font("Arial", 12), brush, New PointF(50, 200))
End Sub
Public Sub New()
Me.Text = "GDI+绘图示例"
Me.ClientSize = New Size(400, 300)
End Sub
End Class
Public Shared Sub Main()
Application.Run(New Form1())
End Sub

先の例では、Formから継承したForm1というカスタムフォームクラスを作成しました。フォームのOnPaintメソッドでは、Graphicsオブジェクトを作成し、そのFillRectangle、FillEllipse、DrawStringメソッドを使用して矩形、楕円、文字列を描画しました。

Main メソッドにて Form1 を new したあとに Application.Run メソッドを実行することでプログラムが実行されます。これにより、フォームが作成され、描画された内容が表示されます。

コードを実行すると、ウィンドウが開き、赤い長方形、楕円、テキストが表示されます。

bannerAds