当前位置:文档之家› 用VB编写画图程序

用VB编写画图程序

3.设置各控件的相关属性设置各控件的相关属性,如表11-9所示。

表11-9 画图程序各控件属性设置4.代码设计(1)引入命名空间:Imports System.Drawing.Drawing2D(2)在FrmPaint类里定义全局变量和mSelect枚举结构:1.Private g As Graphics '绘图句柄2.Private pstart As Point, pend As Point '定义画图的起始点,终点3.Private mChoice As Integer'选择图形枚举4.Private mWidth As Integer'画笔宽度5.Private mIcon As Icon '用户选择图标6.Private Enum mSelect '选择图形类别枚举7. Pencil '铅笔8. Line '直线9. Ellipse '椭圆10. FillEllipse '填充椭圆11. StyleEllipse '风格椭圆12. Rec '矩形13. FillRec '填充矩形14. StyleRec '风格矩形15. Icon '图标16. Eraser '橡皮17.End Enum(3)在FrmPaint的Load事件中初始化全局变量和Graphics对象:1.Private Sub FrmPaint_Load(ByVal sender As System.Object, _2.ByVal e As System.EventArgs) Handles MyBase.Load3.4. g = Me.picPaint.CreateGraphics '获取PictureBox的绘图句柄5. mChoice = mSelect.Pencil '默认选择选铅笔作为绘图工具6. mWidth = 1 '初始化画笔宽度7.End Sub(4)定义转换坐标起点和终点的过程Convert_Point()。

转换坐标起始点和终点,确保起始点始终在终点的左上方,代码如下:1.'确保起始点坐标位于左上角2.'结束点坐标位于右下角3.Private Sub Convert_Point()4.Dim ptemp As Point '用于交换的临时点5.If pstart.X < pend.X Then6.If pstart.Y > pend.Y Then7. ptemp.Y = pstart.Y8. pstart.Y = pend.Y9. pend.Y = ptemp.Y10.End If11.End If12.If pstart.X > pend.X Then13.If pstart.Y < pend.Y Then14. ptemp.X = pstart.X15. pstart.X = pend.X16. pend.X = ptemp.X17.End If18.If pstart.Y > pend.Y Then19. ptemp = pstart20. pstart = pend21. pend = ptemp22.End If23.End If24.End Sub(5)为工具栏ItemClick事件编写代码:1.Private Sub tsPaint_ItemClicked(ByVal sender As System.Object, _2.ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsPaint.ItemClicked3.'获取发生事件的索引号4.Me.mChoice = Me.tsPaint.Items.IndexOf(e.ClickedItem)5.If mChoice = mSelect.Icon Then6.'如果选择的是图标,则打开OpenFileDialog选取图标7.Dim dlgOpen As New OpenFileDialog8. dlgOpen.Filter = "图标文件|*.ico"9.If dlgOpen.ShowDialog = Windows.Forms.DialogResult.OK Then10. mIcon = New Icon(dlgOpen.FileName)11.End If12.End If(6)为btnSetColor控件的Click事件,编写代码,选择画笔颜色,代码如下:1.Private Sub btnSetColor_Click(ByVal sender As System.Object, _2.ByVal e As System.EventArgs) Handles btnSetColor.Click3.'打开"颜色"对话框4.Dim dlgColor As New ColorDialog5.If dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then6.Me.btnSetColor.BackColor = dlgColor.Color7.End If8.End Sub(7)编写选择线条宽度的共享事件过程btnLines_Click()代码:1.Private Sub btnLine_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) _2.Handles btnLine1.Click, btnLine2.Click,btnLine3.Click, btnLine4.Click, btnLine5.Click3.4.'把所有按钮的背景色都设为Black5.Me.btnLine1.BackColor = Color.White6.Me.btnLine2.BackColor = Color.White7.Me.btnLine3.BackColor = Color.White8.Me.btnLine4.BackColor = Color.White9.Me.btnLine5.BackColor = Color.White10.11.'用户选中的按钮背景色为Blue12.CType(sender, Button).BackColor = Color.Black13.14.'把画笔宽度设为用户选择按钮的Tag值15. mWidth = CType(sender, Button).Tag16.End Sub(8)为PictrueBox的MouseDown(鼠标按下)事件编写代码。

在FrmPaint的代码窗口顶部的"对象"下拉列表框中选择picPaint,然后在右侧的"事件"下拉列表框中选择MouseDown,此时代码编辑器中已经自动生成了picPaint_MouseUp的事件代码,并把鼠标定位于事件过程内部的第一行,在该过程中编写如下代码:1.Private Sub picPaint_MouseDown(ByVal sender As System.Object, _2.ByVal e As System.Windows.Forms.MouseEventArgs)Handles picPaint.MouseDown3.If e.Button = Windows.Forms.MouseButtons.Left Then4.5.'如果用户按下的是鼠标左键,则将当前点坐标赋给起始点6. pstart.X = e.X7. pstart.Y = e.Y8.End If9.End Sub11.6.2 编写一个仿Windows画图程序(3)(9)为PictrueBox的MouseUp(鼠标释放)事件编写代码:1.Private Sub picPaint_MouseUp(ByVal sender As System.Object, _2.ByVal e As System.Windows.Forms.MouseEventArgs)Handles picPaint.MouseUp3.4.If e.Button = Windows.Forms.MouseButtons.Left Then5.'如果用户按下的是鼠标左键,记录终点坐标6. pend.X = e.X7. pend.Y = e.Y8.'根据保存的mChoice绘制图形9.Select Case mChoice10.Case mSelect.Line '用户在工具栏中选择的是铅笔11.Dim myPen As New Pen(Me.btnSetColor.BackColor, mWidth)12. g.DrawLine(myPen, pstart, pend) '根据起点和终点绘制直线13.14.Case mSelect.Rec '用户在工具栏中选择的是空心矩形15. Convert_Point() '转换矩形的起点为其左上点16.Dim myPen As New Pen(Me.btnSetColor.BackColor, mWidth)17. g.DrawRectangle(myPen, pstart.X, pstart.Y, _18. pend.X - pstart.X, pend.Y - pstart.Y) '根据起点和终点绘制空心矩形19.20.Case mSelect.FillRec '用户在工具栏中选择的是填充矩形21. Convert_Point() '转换矩形的起点为其左上点22.Dim rec As New Rectangle(pstart.X, pstart.Y, _23. pend.X - pstart.X, pend.Y - pstart.Y)'根据起点和终点定义矩形24.Dim sbr As New SolidBrush(btnSetColor.BackColor) '定义画刷颜色为用户选择的颜色25. g.FillRectangle(sbr, rec) '绘制填充矩形26.27.Case mSelect.StyleRec '用户在工具栏中选择的是风格矩形28. Convert_Point() '转换矩形的起点为其左上点29.Dim rec As New Rectangle(pstart.X, pstart.Y, _30. pend.X - pstart.X, pend.Y - pstart.Y)'根据起点和终点定义矩形31.' 定义画刷风格为Cross型,前景色为白色,背景色为用户选择32.Dim hbr As New HatchBrush(HatchStyle.Cross,Color.White, btnSetColor.BackColor)33. g.FillRectangle(hbr, rec) '用画刷填充矩形34.35.Case mSelect.Ellipse '用户在工具栏中选择的是空心椭圆36. Convert_Point() '转换椭圆外接矩形的起点为其左上点37.Dim pen1 As New Pen(btnSetColor.BackColor, mWidth)38. g.DrawEllipse(pen1, pstart.X, pstart.Y, _39. pend.X - pstart.X, pend.Y - pstart.Y) '根据椭圆外接矩形的起点和终点绘制椭圆40.41.Case mSelect.FillEllipse '用户在工具栏中选择的是填充椭圆42. Convert_Point() '转换椭圆外接矩形的起点为其左上点43.Dim rec As New Rectangle(pstart.X, pstart.Y, _44. pend.X - pstart.X, pend.Y - pstart.Y) '定义椭圆的外接矩形45.Dim sbr As New SolidBrush(btnSetColor.BackColor) '定义画刷颜色为用户选择的颜色46. g.FillEllipse(sbr, rec) '用画刷填充矩形47.48.Case mSelect.StyleEllipse '用户在工具栏中选择的是风格椭圆49. Convert_Point() '转换椭圆外接矩形的起点为其左上点50.Dim rec As New Rectangle(pstart.X, pstart.Y, _51. pend.X - pstart.X, pend.Y - pstart.Y) '定义椭圆的外接矩形52.' 定义画刷风格为Cross型,前景色为白色,背景色为用户选择53.Dim hbr As New HatchBrush(HatchStyle.Cross,Color.White, btnSetColor.BackColor)54. g.FillEllipse(hbr, rec) '用画刷填充矩形55.End Select56.End If57.End Sub(10)为PictrueBox的MouseMove(鼠标移动)事件编写代码:1.Private Sub picPaint_MouseMove(ByVal sender As System.Object, _2.ByVal e As System.Windows.Forms.MouseEventArgs)Handles picPaint.MouseMove3.If e.Button = Windows.Forms.MouseButtons.Left Then4.'如果用户按下的是鼠标左键,根据保存的mChoice绘制图形5.Select Case mChoice6.Case mSelect.Pencil '用户在工具栏中选择的是铅笔7.Dim pen1 As New Pen(btnSetColor.BackColor, mWidth)8. pend.X = e.X9. pend.Y = e.Y10. g.DrawLine(pen1, pstart, pend)11. pstart = pend '将已经绘制的终点作为下一次的绘制的起点12.Case mSelect.Eraser '用户在工具栏中选择的是橡皮13.Dim myPen As New Pen(Color.White, mWidth)'定义白色画笔作为擦除效果14. pend.X = e.X15. pend.Y = e.Y16. g.DrawLine(myPen, pstart, pend) '将已经绘制的终点作为下一次绘制的起点17. pstart = pend '将已经绘制的终点作为下一次绘制的起点18.End Select19.End If20.End Sub(11)为PictrueBox的Mouse的Click(鼠标单击)事件编写代码:1.Private Sub picPaint_Click(ByVal sender As System.Object, _2.ByVal e As System.EventArgs) Handles picPaint.Click3.If mChoice = mSelect.Icon Then4.'画图标5. g.DrawIcon(mIcon, pstart.X, pstart.Y)6.End If7.End Sub(12)为"新建"miNew的Click事件编写代码:1.Private Sub miNew_Click(ByVal sender As System.Object, _2.ByVal e As System.EventArgs) Handles miNew.Click3.4.Me.picPaint.Refresh()5.End Sub(13)为"退出"菜单miExit的Click事件编写代码:1.Private Sub miExit_Click(ByVal sender As System.Object, _2.ByVal e As System.EventArgs) Handles miExit.Click3. Application.Exit()4.End Sub(14)调试运行程序。

相关主题