加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net 教程 5-10 Graphics类 1

发布时间:2020-12-17 07:35:06 所属栏目:百科 来源:网络整理
导读:个人觉得Graphics就像是画布,要作画,就必须先建立Graphics对象,在这上面实现图像的操作。 能够在表面作图的控件,都支持CreateGraphics()方法 例如: PictureBox1.CreateGraphics() Me.CreateGraphics() 等等 如果是绘制图形,那么要用的工具主要是Pen类
个人觉得Graphics就像是画布,要作画,就必须先建立Graphics对象,在这上面实现图像的操作。
能够在表面作图的控件,都支持CreateGraphics()方法
例如:
PictureBox1.CreateGraphics()
Me.CreateGraphics()
等等

如果是绘制图形,那么要用的工具主要是Pen类和Brush类
Pen主要用于线条图形类的,Brush主要用于填充图形类
声明一个Pen对象:
Dim myPen As New Pen(Color.Blue,1)

声明一个Brush对象:
Dim myBrush As New SolidBrush(Color.Red)

Brush类是一个抽象类,实际使用中需要用Brush的派生类:
SolidBrush、HatchBrush、PathGrdientBrush、TextureBrush和LinearGradientBrush。

画 线:Graphics.DrawLine
画矩形:Graphics.DrawRectangles
画椭圆:Graphics.DrawEllipse
画弧线:Graphics.DrawArc
画多边形:Graphics.DrawPolygon
。。。。

填充椭圆:Graphics.FillEllipse
填充扇形:Graphics.FillPie
填充多边形:Graphics.FillPolygon
填充矩形:Graphics.FillRectangle

一个简单的例子:
代码如下:
Public Class Form1

    Dim gCanvas As Graphics

    Private Sub btnLine_Click(sender As Object,e As EventArgs) Handles btnLine.Click
        Dim myPen As New Pen(Color.Red,2)  '红色,宽度2的画笔
        gCanvas.DrawLine(myPen,New Point(10,10),200)) '画线
    End Sub

    Private Sub btnRectangle_Click(sender As Object,e As EventArgs) Handles btnRectangle.Click
        Dim myPen As New Pen(Color.Blue,2)  '蓝色,宽度2的画笔
        gCanvas.DrawRectangle(myPen,New Rectangle(20,10,150,200))  '画矩形
    End Sub

    Private Sub btnArc_Click(sender As Object,e As EventArgs) Handles btnArc.Click
        Dim myPen As New Pen(Color.Green,2)  '绿色,宽度2的画笔
        gCanvas.DrawArc(myPen,New Rectangle(240,200),180)  '画弧线
    End Sub

    Private Sub btnFillRectangle_Click(sender As Object,e As EventArgs) Handles btnFillRectangle.Click
        Dim myBrush As New SolidBrush(Color.Red) '红色画刷
        gCanvas.FillRectangle(myBrush,New Rectangle(21,11,148,198))  '填充矩形
    End Sub

    Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load
        gCanvas = Me.CreateGraphics()
    End Sub

    Private Sub btnFillEllipse_Click(sender As Object,e As EventArgs) Handles btnFillEllipse.Click
        Dim myBrush As New SolidBrush(Color.Red)  '红色画刷
        gCanvas.FillEllipse(myBrush,New Rectangle(241,198))  '填充椭圆
    End Sub
End Class

代码中创建了一个窗体级别变量 gCanvas,在窗口载入的时候,将gCanvas 绑到窗体上。
运行时如下图:


由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读