用VB编写画图程序.docx

上传人:b****4 文档编号:1196755 上传时间:2022-10-18 格式:DOCX 页数:11 大小:105.89KB
下载 相关 举报
用VB编写画图程序.docx_第1页
第1页 / 共11页
用VB编写画图程序.docx_第2页
第2页 / 共11页
用VB编写画图程序.docx_第3页
第3页 / 共11页
用VB编写画图程序.docx_第4页
第4页 / 共11页
用VB编写画图程序.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

用VB编写画图程序.docx

《用VB编写画图程序.docx》由会员分享,可在线阅读,更多相关《用VB编写画图程序.docx(11页珍藏版)》请在冰豆网上搜索。

用VB编写画图程序.docx

用VB编写画图程序

11.6.2 编写一个仿Windows画图程序

(1)

本例将实现11.1节中的一个仿Windows的简单的画图软件的任务。

1.新建项目

创建一个项目类型为VisualBasic,模板为Windows应用程序的项目,命名为Ch11_5。

2.界面设计

在窗体上添加:

一个MenuStrip菜单控件、一个ToolStrip工具栏控件,一个用作绘图板的PictureBox图片框控件、两个让用户选择颜色和线条宽标志的Lable控件、5个让用户选择线条宽度的button控件,一个Panel控件上、一个ImageList控件和一个作为状态信息栏的StatusStripr。

调整窗体上各控件的大小及位置,如图11-18所示。

 

图11-18 画图程序主界面

3.设置各控件的相关属性

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

表11-9 画图程序各控件属性设置

 

 

4.代码设计

(1)引入命名空间:

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

(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.        '如果选择的是图标,则打开Open选取图标 

7.        Dim dlgOpen As New Open 

8.        dlgOpen.Filter = "图标文件|*.ico" 

9.        If dlgOpen.ShowDialog = Windows.Forms.DialogResult.OK Then 

10.            mIcon = New Icon(dlgOpen.) 

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1