【VB开源代码栏目提醒】:网学会员VB开源代码为您提供用VB编写画图程序 - 综合课件参考,解决您在用VB编写画图程序 - 综合课件学习中工作中的难题,参考学习。
1 11.6.2 编写一个仿Windows画图程序1 本例将实现11.1节中的一个仿Windows的简单的画图软件的任务。
1新建项目 创建一个项目类型为Visual Basic模板为Windows应用程序的项目命名为Ch11_5。
2界面设计 在窗体上添加一个MenuStrip菜单控件、一个ToolStrip工具栏控件一个用作绘图板的PictureBox图片框控件、两个让用户选择颜色和线条宽标志的Lable控件、5个让用户选择线条宽度的button控件一个Panel控件上、一个ImageList控件和一个作为状态信息栏的StatusStripr。
调整窗体上各控件的大小及位置如图11-18所示。
图11-18 画图
程序主界面 3设置各控件的相关属性 设置各控件的相关属性如表11-9所示。
表11-9 画图程序各控件属性设置 2 3 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 填充矩形 4 14. StyleRec 风格矩形 15. Icon 图标 16. Eraser 橡皮 17. End Enum 3在FrmPaint的Load事件中初始化全局变量和Graphics对象 1. Private Sub FrmPaint_LoadByVal sender As System.Object _ 2. ByVal e As System.EventArgs Handles MyBase.Load 3. 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 Then 6. If pstart.Y pend.Y Then 7. ptemp.Y pstart.Y 8. pstart.Y pend.Y 9. pend.Y ptemp.Y 10. End If 11. End If 12. If pstart.X pend.X Then 13. If pstart.Y pend.Y Then 14. ptemp.X pstart.X 15. pstart.X pend.X 16. pend.X ptemp.X 17. End If 18. If pstart.Y pend.Y Then 19. ptemp pstart 20. pstart pend 21. pend ptemp 22. End If 5 23. End If 24. End Sub 5为工具栏ItemClick事件编写
代码 1. Private Sub tsPaint_ItemClickedByVal sender As System.Object _ 2. ByVal e As System.Windows.Forms.ToolStripItem ClickedEventArgs Handles tsPaint.ItemClicked 3. 获取发生事件的索引号 4. Me.mChoice Me.tsPaint.Items.IndexOfe.ClickedItem 5. If mChoice mSelect.Icon Then 6. 如果选择的是图标则打开OpenFileDialog选取图标 7. Dim dlgOpen As New OpenFileDialog 8. dlgOpen.Filter 图标文件.ico 9. If dlgOpen.ShowDialog Windows.Forms.DialogResult.OK Then 10. mIcon New IcondlgOpen.FileName 11. End If 12. End If 6为btnSetColor控件的Click事件编写
代码选择画笔颜色
代码如下 1. Private Sub btnSetColor_ClickByVal sender As System.Object _ 2. ByVal e As System.EventArgs Handles btnSetColor.Click 3. 打开颜色对话框 4. Dim dlgColor As New ColorDialog 5. If dlgColor.ShowDialog Windows.Forms.DialogResult.OK Then 6. Me.btnSetColor.BackColor dlgColor.Color 7. End If 8. End Sub 7编写选择线条宽度的共享事件过程btnLines_Click
代码 1. Private Sub btnLine_ClickByVal sender As System.Object ByVal e As System.EventArgs _ 2. Handles btnLine1.Click btnLine2.Click btnLine3.Click btnLine4.Click btnLine5.Click 3. 4. 把所有按钮的背景色都设为Black 5. Me.btnLine1.BackColor Color.White 6. Me.btnLine2.BackColor Color.White 7. Me.btnLine3.BackColor Color.White 8. Me.btnLine4.BackColor Color.White 6 9. Me.btnLine5.BackColor Color.White 10. 11. 用户选中的按钮背景色为Blue 12. CTypesender Button.BackColor Color.Black 13. 14. 把画笔宽度设为用户选择按钮的Tag值 15. mWidth CTypesender Button.Tag 16. End Sub 8为PictrueBox的MouseDown鼠标按下事件编写
代码。
在FrmPaint的
代码窗口顶部的对象下拉列表框中选择picPaint然后在右侧的事件下拉
列表框中选择MouseDown此时
代码编辑器中已经自动生成了picPaint_MouseUp的事件
代码并把鼠标定位于事件过程内部的第一行在该过程中编写如下
代码 1. Private Sub picPaint_MouseDownByVal sender As System.Object _ 2. ByVal e As System.Windows.Forms.MouseEventArgs Handles picPaint.MouseDown 3. If e.Button Windows.Forms.MouseButtons.Left Then 4. 5. 如果用户按下的是鼠标左键则将当前点坐标赋给起始点 6. pstart.X e.X 7. pstart.Y e.Y 8. End If 9. End Sub 11.6.2 编写一个仿Windows画图程序3 9为PictrueBox的MouseUp鼠标释放事件编写
代码 1. Private Sub picPaint_MouseUpByVal sender As System.Object _ 2. ByVal e As System.Windows.Forms.MouseEventArgs Handles picPaint.MouseUp 3. 4. If e.Button Windows.Forms.MouseButtons.Left Then 5. 如果用户按下的是鼠标左键记录终点坐标 6. pend.X e.X 7. pend.Y e.Y 8. 根据保存的mChoice绘制图形 9. Select Case mChoice 10. Case mSelect.Line 用户在工具栏中选择的是铅笔 7 11. Dim myPen As New PenMe.btnSetColor.BackColor mWidth 12. g.DrawLinemyPen pstart pend 根据起点和终点绘制直线 13. 14. Case mSelect.Rec 用户在工具栏中选择的是空心矩形 15. Convert_Point 转换矩形的起点为其左上点 16. Dim myPen As New PenMe.btnSetColor.BackColor mWidth 17. g.DrawRectanglemyPen 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 Rectanglepstart.X pstart.Y _ 23. pend.X - pstart.X pend.Y - pstart.Y 根据起点和终点定义矩形 24. Dim sbr As New SolidBrushbtnSetColor. BackColor 定义画刷颜色为用户选择的颜色 25. g.FillRectanglesbr rec 绘制填充矩形 26. 27. Case mSelect.StyleRec 用户在工具栏中选择的是风格矩形 28. Convert_Point 转换矩形的起点为其左上点 29. Dim rec As New Rectanglepstart.X pstart.Y _ 30. pend.X - pstart.X pend.Y - pstart.Y 根据起点和终点定义矩形 31. 定义画刷风格为Cross型前景色为白色背景色为用户选择 32. Dim hbr As New HatchBrushHatchStyle.Cross Color.White btnSetColor.BackColor 33. g.FillRectanglehbr rec 用画刷填充矩形 34. 35. Case mSelect.Ellipse 用户在工具栏中选择的是空心椭圆 36. Convert_Point 转换椭圆外接矩形的起点为其左上点 37. Dim pen1 As New PenbtnSetColor.BackColor mWidth 38. g.DrawEllipsepen1 pstart.X pstart.Y _ 39. pend.X - pstart.X pend.Y - pstart.Y 根据椭圆外接矩形的起点和终点绘制椭圆 8 40. 41. Case mSelect.FillEllipse 用户在工具栏中选择的是填充椭圆 42. Convert_Point 转换椭圆外接矩形的起点为其左上点 43. Dim rec As New Rectanglepstart.X pstart.Y _ 44. pend.X - pstart.X pend.Y - pstart.Y 定义椭圆的外接矩形 45. Dim sbr As New SolidBrushbtnSetColor. BackColor 定义画刷颜色为用户选择的颜色 46. g.FillEllipsesbr rec 用画刷填充矩形 47. 48. Case mSelect.StyleEllipse 用户在工具栏中选择的是风格椭圆 49. Convert_Point 转换椭圆外接矩形的起点为其左上点 50. Dim rec As New Rectanglepstart.X pstart.Y _ 51. pend.X - pstart.X pend.Y - pstart.Y 定义椭圆的外接矩形 52. 定义画刷风格为Cross型前景色为白色背景色为用户选择 53. Dim hbr As New HatchBrushHatchStyle.Cross Color.White btnSetColor.BackColor 54. g.FillEllipsehbr rec 用画刷填充矩形 55. End Select 56. End If 57. End Sub 10为PictrueBox的MouseMove鼠标移动事件编写
代码 1. Private Sub picPaint_MouseMoveByVal sender As System.Object _ 2. ByVal e As System.Windows.Forms.MouseEventArgs Handles picPaint.MouseMove 3. If e.Button Windows.Forms.MouseButtons.Left Then 4. 如果用户按下的是鼠标左键根据保存的mChoice绘制图形 5. Select Case mChoice 6. Case mSelect.Pencil 用户在工具栏中选择的是铅笔 7. Dim pen1 As New PenbtnSetColor.BackColor mWidth 8. pend.X e.X 9. pend.Y e.Y 10. g.DrawLinepen1 pstart pend 11. pstart pend 将已经绘制的终点作为下一次的绘制的起点 12. Case mSelect.Eraser 用户在工具栏中选择的是橡皮 9 13. Dim myPen As New PenColor.White mWidth 定义白色画笔作为擦除效果 14. pend.X e.X 15. pend.Y e.Y 16. g.DrawLinemyPen pstart pend 将已 经绘制的终点作为下一次绘制的起点 17. pstart pend 将已经绘制的终点作为 下一次绘制的起点 18. End Select 19. End If 20. End Sub 11为PictrueBox的Mouse的Click鼠标单击事件编写
代码 1. Private Sub picPaint_ClickByVal sender As System.Object _ 2. ByVal e As System.EventArgs Handles picPaint.Click 3. If mChoice mSelect.Icon Then 4. 画图标 5. g.DrawIconmIcon pstart.X pstart.Y 6. End If 7. End Sub 12为新建miNew的Click事件编写
代码 1. Private Sub miNew_ClickByVal sender As System.Object _ 2. ByVal e As System.EventArgs Handles miNew.Click 3. 4. Me.picPaint.Refresh 5. End Sub 13为退出菜单miExit的Click事件编写
代码 1. Private Sub miExit_ClickByVal sender As System.Object _ 2. ByVal e As System.EventArgs Handles miExit.Click 3. Application.Exit 4. End Sub 14调试运行程序。
至此一个简单的绘图
软件的
代码编写完成按F5键或工具栏上的运行按钮即可运行程序绘制图形后结果如图11-19所示。
10 点击查看大图图11-19 画笔软件运行结果