用VB编写画图程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

相关文档
最新文档