我是靠谱客的博主 饱满鸡翅,最近开发中收集的这篇文章主要介绍vb.net graphic 简单的画图程序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Dim g As Graphics = Me.CreateGraphics

需要局部定义  每次实用需要定义--不能定义成全局

 

Public Class Form1
    Dim DrawState As Boolean
    Dim PreX As Single
    Dim PreY As Single
    Dim eP As New Pen(Color.Black, 3)          ' 构造黑色画笔,并将它赋给对象变量eP
    'Dim g As Graphics = Me.CreateGraphics     ' 在窗体上构造一块画布,并将它赋给对象变量g

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 将DrawState初始化为False,表示提笔
        DrawState = False
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        ' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线
        Dim g As Graphics = Me.CreateGraphics
        If e.Button = Windows.Forms.MouseButtons.Left Then
            DrawState = True     ' 设置画图状态
            PreX = e.X          ' PreX和PreY保存了线条的起点。
            PreY = e.Y
        End If
        ' 当按住鼠标右键时,画一个直径为50的圆
        If e.Button = Windows.Forms.MouseButtons.Right Then
            g.DrawEllipse(eP, e.X - 25, e.Y - 25, 100, 100)
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        ' 当鼠标移动时,如果处于画线状态,则在(PreX,PreY)与(X,Y)之间画一条直线
        Dim g As Graphics = Me.CreateGraphics
        If DrawState = True Then
            g.DrawLine(eP, PreX, PreY, e.X, e.Y)
            PreX = e.X
            PreY = e.Y
        End If

    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        ' 当释放鼠标左键时,解除画线状态
        If e.Button = Windows.Forms.MouseButtons.Left Then
            DrawState = False
        End If
    End Sub
End Class

 

改法1
 

Public Class Form1
    Dim DrawState As Boolean
    Dim PreX As Single
    Dim PreY As Single
    Dim eP As New Pen(Color.Black, 3)          ' 构造黑色画笔,并将它赋给对象变量eP
    Dim g As Graphics     ' 在窗体上构造一块画布,并将它赋给对象变量g

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 将DrawState初始化为False,表示提笔
        DrawState = False
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        ' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线
        'Dim g As Graphics = Me.CreateGraphics
        If e.Button = Windows.Forms.MouseButtons.Left Then
            DrawState = True     ' 设置画图状态
            PreX = e.X          ' PreX和PreY保存了线条的起点。
            PreY = e.Y
        End If
        ' 当按住鼠标右键时,画一个直径为50的圆
        If e.Button = Windows.Forms.MouseButtons.Right Then
            g.DrawEllipse(eP, e.X - 25, e.Y - 25, 100, 100)
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        ' 当鼠标移动时,如果处于画线状态,则在(PreX,PreY)与(X,Y)之间画一条直线
        'Dim g As Graphics = Me.CreateGraphics
        If DrawState = True Then
            g.DrawLine(eP, PreX, PreY, e.X, e.Y)
            PreX = e.X
            PreY = e.Y
        End If

    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        ' 当释放鼠标左键时,解除画线状态
        If e.Button = Windows.Forms.MouseButtons.Left Then
            DrawState = False
        End If
    End Sub


Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        g = Me.CreateGraphics
    End Sub
End Class


 

改法2

 

将声明定义到模块中

 

 

 

最后

以上就是饱满鸡翅为你收集整理的vb.net graphic 简单的画图程序的全部内容,希望文章能够帮你解决vb.net graphic 简单的画图程序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部