文档库 最新最全的文档下载
当前位置:文档库 › 用VB编写画图程序

用VB编写画图程序

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.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

23.End If

24.End Sub

(5)为工具栏ItemClick事件编写代码:

1.Private Sub tsPaint_ItemClicked(ByVal sender As System.Object, _

2.ByVal e As System.Windows.Forms.ToolStripItem

ClickedEventArgs) Handles tsPaint.ItemClicked

3.'获取发生事件的索引号

4.Me.mChoice = Me.tsPaint.Items.IndexOf(e.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 Icon(dlgOpen.FileName)

11.End If

12.End If

(6)为btnSetColor控件的Click事件,编写代码,选择画笔颜色,代码如下:

1.Private Sub btnSetColor_Click(ByVal 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_Click(ByVal 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

9.Me.btnLine5.BackColor = Color.White

10.

11.'用户选中的按钮背景色为Blue

12.CType(sender, Button).BackColor = Color.Black

13.

14.'把画笔宽度设为用户选择按钮的Tag值

15. mWidth = CType(sender, Button).Tag

16.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.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_MouseUp(ByVal 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 '用户在工具栏中选择的是铅笔

11.Dim myPen As New Pen(Me.btnSetColor.BackColor, mWidt

h)

12. g.DrawLine(myPen, pstart, pend) '根据起点和终点绘制直

线

13.

14.Case mSelect.Rec '用户在工具栏中选择的是空心矩形

15. Convert_Point() '转换矩形的起点为其左上点

16.Dim myPen As New Pen(Me.btnSetColor.BackColor, mWidt

h)

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 Select

56.End If

57.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.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 Pen(btnSetColor.BackColor, mWidth)

8. pend.X = e.X

9. pend.Y = e.Y

10. 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.X

15. pend.Y = e.Y

16. g.DrawLine(myPen, pstart, pend) '将已

经绘制的终点作为下一次绘制的起点

17. pstart = pend '将已经绘制的终点作为

下一次绘制的起点

18.End Select

19.End If

20.End Sub

(11)为PictrueBox的Mouse的Click(鼠标单击)事件编写代码:

1.Private Sub picPaint_Click(ByVal sender As System.Object, _

2.ByVal e As System.EventArgs) Handles picPaint.Click

3.If mChoice = mSelect.Icon Then

4.'画图标

5. g.DrawIcon(mIcon, pstart.X, pstart.Y)

6.End If

7.End Sub

(12)为"新建"miNew的Click事件编写代码:

1.Private Sub miNew_Click(ByVal 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_Click(ByVal sender As System.Object, _

2.ByVal e As System.EventArgs) Handles miExit.Click

3. Application.Exit()

4.End Sub

相关文档