当前位置:文档之家› vbnetgraphic简单的画图程序

vbnetgraphic简单的画图程序

graphic 简单的画图程序[vb]Public Class Form1Dim DrawState As BooleanDim PreX As SingleDim PreY As SingleDim eP As New Pen(Color.Black, 3) ' 构造黑色画笔,并将它赋给对象变量eP'Dim g As Graphics = Me.CreateGraphics ' 在窗体上构造一块画布,并将它赋给对象变量gPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' 将DrawState初始化为False,表示提笔DrawState = FalseEnd SubPrivate Sub Form1_MouseDown(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseDown' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线Dim g As Graphics = Me.CreateGraphicsIf e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = True ' 设置画图状态PreX = e.X ' PreX和PreY保存了线条的起点。

PreY = e.YEnd If' 当按住鼠标右键时,画一个直径为50的圆If e.Button = Windows.Forms.MouseButtons.Right Theng.DrawEllipse(eP, e.X - 25, e.Y - 25, 100, 100)End IfEnd SubPrivate 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.CreateGraphicsIf DrawState = True Theng.DrawLine(eP, PreX, PreY, e.X, e.Y)PreX = e.XPreY = e.YEnd IfEnd SubPrivate Sub Form1_MouseUp(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseUp' 当释放鼠标左键时,解除画线状态If e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = FalseEnd IfEnd SubEnd ClassPublic Class Form1Dim DrawState As BooleanDim PreX As SingleDim PreY As SingleDim eP As New Pen(Color.Black, 3) ' 构造黑色画笔,并将它赋给对象变量eP'Dim g As Graphics = Me.CreateGraphics ' 在窗体上构造一块画布,并将它赋给对象变量gPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' 将DrawState初始化为False,表示提笔DrawState = FalseEnd SubPrivate Sub Form1_MouseDown(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseDown' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线Dim g As Graphics = Me.CreateGraphicsIf e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = True ' 设置画图状态PreX = e.X ' PreX和PreY保存了线条的起点。

PreY = e.YEnd If' 当按住鼠标右键时,画一个直径为50的圆If e.Button = Windows.Forms.MouseButtons.Right Theng.DrawEllipse(eP, e.X - 25, e.Y - 25, 100, 100)End IfEnd SubPrivate Sub Form1_MouseMove(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseMove' 当鼠标移动时,如果处于画线状态,则在(PreX,PreY)与(X,Y)之间画一条直线Dim g As Graphics = Me.CreateGraphicsIf DrawState = True Theng.DrawLine(eP, PreX, PreY, e.X, e.Y)PreX = e.XPreY = e.YEnd IfEnd SubPrivate Sub Form1_MouseUp(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseUp' 当释放鼠标左键时,解除画线状态If e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = FalseEnd IfEnd SubEnd Class改法1[vb]Public Class Form1Dim DrawState As BooleanDim PreX As SingleDim PreY As SingleDim eP As New Pen(Color.Black, 3) ' 构造黑色画笔,并将它赋给对象变量ePDim g As Graphics ' 在窗体上构造一块画布,并将它赋给对象变量gPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' 将DrawState初始化为False,表示提笔DrawState = FalseEnd SubPrivate Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线'Dim g As Graphics = Me.CreateGraphicsIf e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = True ' 设置画图状态PreX = e.X ' PreX和PreY保存了线条的起点。

PreY = e.YEnd If' 当按住鼠标右键时,画一个直径为50的圆If e.Button = Windows.Forms.MouseButtons.Right Theng.DrawEllipse(eP, e.X - 25, e.Y - 25, 100, 100)End IfEnd SubPrivate 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.CreateGraphicsIf DrawState = True Theng.DrawLine(eP, PreX, PreY, e.X, e.Y)PreX = e.XPreY = e.YEnd IfEnd SubPrivate Sub Form1_MouseUp(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseUp' 当释放鼠标左键时,解除画线状态If e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = FalseEnd IfEnd SubPrivate Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resizeg = Me.CreateGraphicsEnd SubEnd ClassPublic Class Form1Dim DrawState As BooleanDim PreX As SingleDim PreY As SingleDim eP As New Pen(Color.Black, 3) ' 构造黑色画笔,并将它赋给对象变量ePDim g As Graphics ' 在窗体上构造一块画布,并将它赋给对象变量gPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' 将DrawState初始化为False,表示提笔DrawState = FalseEnd SubPrivate Sub Form1_MouseDown(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles Me.MouseDown' 当鼠标左键被按下时,把DrawState设为True,表示落笔开始画线'Dim g As Graphics = Me.CreateGraphicsIf e.Button = Windows.Forms.MouseButtons.Left ThenDrawState = True ' 设置画图状态PreX = e.X ' PreX和PreY保存了线条的起点。

相关主题