一、实验目的
掌握在.net环境下的绘图软件界面设计与交互技术。
二、实验准备
学习在.net环境下的界面设计的一般原理与交互技术等基本知识。
三、实验内容
将前7个实验内容集成到一个界面下,如直线段、圆、矩形与曲线的绘制填充,以及对图像的处理,并能利用交互技术实现对图元的选取、修改和交互。
四、实验过程及步骤
1、程序界面设计
2 、控件属性说明
添加mainmnue控件,属性如:左添加2个panel控件,设置panel1.name=“P”,panel2作为一个调色板
3 、程序代码
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Math
--------------------------------------------------------------------------------------------
Public Class Form1Dim f1 As Integer = 0Dim lp(100, 20) As PointDim lf As BooleanDim ln(100) As IntegerDim lleft As BooleanDim cp1(100) As PointDim cp2(100) As PointDim cr(100) As SingleDim cf As BooleanDim rp1(100) As PointDim rp2(100) As PointDim rf As BooleanDim l As IntegerDim c As Integer
Dim r As IntegerDim Pencolor As ColorDim lc(100) As ColorDim cc(100) As Color
Dim rc(100) As Color
--------------------------------------------------------------------------------------------Private Sub Init()Dim i, j As IntegerFor i = 0 To 100ln(i) = 0Nextlf = Truelleft = Truecf = Truerf = Truel = 0c = 0r = 0End Sub
--------------------------------------------------------------------------------------------Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadP.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3DP.Dock = DockStyle.FillP.BackColor = Color.WhiteGroupBox1.Dock = DockStyle.Right '该控件停靠在其包含控件的右边缘lRed.Text = "R"lRed.BackColor = Color.Red '获取控件的背景颜色为红色lGreen.Text = "G"lGreen.BackColor = Color.Green '获取控件的背景颜色为绿色lBlue.Text = "B"lBlue.BackColor = Color.Blue '获取控件的背景颜色为蓝色pColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSinglepColor.BackColor = Color.BlacktrRed.Maximum = 255trRed.Minimum = 0trRed.LargeChange = 17trRed.SmallChange = 1trRed.TickFrequency = 17trRed.TickStyle = TickStyle.BottomRight '刻度线位于水平控件的底部或者垂直控件的右部trRed.Value = 0 '刚开始设置为0trGreen.Maximum = 255trGreen.Minimum = 0trGreen.LargeChange = 17trGreen.SmallChange = 1trGreen.TickFrequency = 17trGreen.TickStyle = TickStyle.BottomRighttrGreen.Value = 0 '刚开始设置为0trBlue.Maximum = 255trBlue.Minimum = 0trBlue.LargeChange = 17trBlue.SmallChange = 1trBlue.TickFrequency = 17trBlue.TickStyle = TickStyle.BottomRighttrBlue.Value = 0 '刚开始设置为0Init()Pencolor = Color.BlackEnd Sub
--------------------------------------------------------------------------------------------Private Sub mLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mLine.Clickf1 = 1End Sub
--------------------------------------------------------------------------------------------Private Sub mCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCircle.Clickf1 = 2End Sub
--------------------------------------------------------------------------------------------Private Sub mRectangle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mRectangle.Clickf1 = 3End Sub
--------------------------------------------------------------------------------------------Private Sub mClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mClear.Clickf1 = 4P.Invalidate()Init()End Sub
--------------------------------------------------------------------------------------------Private Sub P_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles P.MouseDownSelect Case f1Case 1If e.Button = MouseButtons.Left ThenIf lf Thenlc(l) = Pencolorlp(l, ln(l)) = New Point(e.X, e.Y)lf = FalseElseIf Not lf Thenln(l) = ln(l) + 1End IfElseIf e.Button = MouseButtons.Right Thenlleft = FalseP.Invalidate()End IfCase 2If cf Thencc(c) = Pencolorcp1(c) = New Point(e.X, e.Y)cf = FalseElseIf Not cf Thencf = TrueP.Invalidate()End IfCase 3If rf Thenrc(r) = Pencolorrp1(r) = New Point(e.X, e.Y)rf = FalseElseIf Not rf Thenrf = TrueP.Invalidate()End IfEnd SelectEnd Sub
--------------------------------------------------------------------------------------------Private Sub P_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles P.MouseMoveSelect Case f1Case 1If lleft ThenIf Not lf Thenlp(l, ln(l) + 1) = New Point(e.X, e.Y)P.Invalidate()End IfEnd IfCase 2If Not cf Thencp2(c) = New Point(e.X, e.Y)P.Invalidate()End IfCase 3If Not rf Thenrp2(r) = New Point(e.X, e.Y)P.Invalidate()End IfEnd SelectEnd Sub
--------------------------------------------------------------------------------------------Private Sub P_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles P.PaintDim rect1 As RectangleDim rect2 As RectangleDim i, j As IntegerDim g As Graphics = e.Graphicsg.SmoothingMode = SmoothingMode.AntiAliasIf l > 0 ThenFor i = 0 To l - 1For j = 0 To ln(i) - 1g.DrawLine(Pens.White, lp(i, j), lp(i, j + 1))g.DrawLine(New Pen(lc(i), 3), lp(i, j), lp(i, j + 1))NextNextEnd IfIf c > 0 ThenFor i = 0 To c - 1rect1 = New Rectangle(cp1(i).X - cr(i), cp1(i).Y - cr(i), cr(i) * 2, cr(i) * 2)rect2 = New Rectangle(cp1(i).X - cr(i), cp1(i).Y - cr(i), cr(i) * 2, cr(i) * 2)g.DrawEllipse(Pens.White, rect1)g.DrawEllipse(New Pen(cc(i), 3), rect2)NextEnd IfIf r > 0 ThenFor i = 0 To r - 1rect1 = New Rectangle(Min(rp1(i).X, rp2(i).X), Min(rp1(i).Y, rp2(i).Y), Abs(rp1(i).X - rp2(i).X), Abs(rp1(i).Y - rp2(i).Y))rect2 = New Rectangle(Min(rp1(i).X, rp2(i).X), Min(rp1(i).Y, rp2(i).Y), Abs(rp1(i).X - rp2(i).X), Abs(rp1(i).Y - rp2(i).Y))g.DrawRectangle(Pens.White, rect1)g.DrawRectangle(New Pen(rc(i), 3), rect2)NextEnd IfSelect Case f1Case 1If ln(l) >= 1 ThenFor j = 0 To ln(l) - 1g.DrawLine(Pens.White, lp(i, j), lp(i, j + 1))g.DrawLine(New Pen(lc(l), 3), lp(i, j), lp(i, j + 1))NextEnd IfIf lleft ThenIf Not lf Theng.DrawLine(Pens.White, lp(l, ln(l)), lp(l, ln(l) + 1))g.DrawLine(New Pen(lc(l), 3), lp(l, ln(l)), lp(l, ln(l) + 1))End IfElseIf ln(l) > 0 Thenl = l + 1End Iflf = Truelleft = TrueEnd IfCase 2If Not cf Thencr(c) = Sqrt((cp2(c).X - cp1(c).X) * (cp2(c).X - cp1(c).X) + (cp2(c).Y - cp1(c).Y) * (cp2(c).Y - cp1(c).Y))rect1 = New Rectangle(cp1(c).X - cr(c), cp1(c).Y - cr(c), cr(c) * 2, cr(c) * 2)rect2 = New Rectangle(cp1(c).X - cr(c), cp1(c).Y - cr(c), cr(c) * 2, cr(c) * 2)g.DrawLine(Pens.White, cp1(c), cp2(c))g.DrawLine(New Pen(cc(c), 3), cp1(c), cp2(c))g.DrawLine(Pens.Black, cp1(c), cp2(c))g.DrawEllipse(Pens.White, rect1)g.DrawEllipse(New Pen(cc(c), 3), rect2)Elsecr(c) = Sqrt((cp2(c).X - cp1(c).X) * (cp2(c).X - cp1(c).X) + (cp2(c).Y - cp1(c).Y) * (cp2(c).Y - cp1(c).Y))rect1 = New Rectangle(cp1(c).X - cr(c), cp1(c).Y - cr(c), cr(c) * 2, cr(c) * 2)rect2 = New Rectangle(cp1(c).X - cr(c), cp1(c).Y - cr(c), cr(c) * 2, cr(c) * 2)g.DrawEllipse(Pens.White, rect1)g.DrawEllipse(New Pen(cc(c), 3), rect2)c = c + 1End IfCase 3If Not rf Theng.DrawLine(Pens.White, cp1(r), cp2(r))g.DrawLine(Pens.Black, cp1(r), cp2(r))rect1 = New Rectangle(Min(rp1(r).X, rp2(r).X), Min(rp1(r).Y, rp2(r).Y), Abs(rp1(r).X - rp2(r).X), Abs(rp1(r).Y - rp2(r).Y))rect2 = New Rectangle(Min(rp1(r).X, rp2(r).X), Min(rp1(r).Y, rp2(r).Y), Abs(rp1(r).X - rp2(r).X), Abs(rp1(r).Y - rp2(r).Y))g.DrawRectangle(Pens.White, rect1)g.DrawRectangle(New Pen(rc(r), 3), rect2)Elserect1 = New Rectangle(Min(rp1(r).X, rp2(r).X), Min(rp1(r).Y, rp2(r).Y), Abs(rp1(r).X - rp2(r).X), Abs(rp1(r).Y - rp2(r).Y))rect2 = New Rectangle(Min(rp1(r).X, rp2(r).X), Min(rp1(r).Y, rp2(r).Y), Abs(rp1(r).X - rp2(r).X), Abs(rp1(r).Y - rp2(r).Y))g.DrawRectangle(Pens.White, rect1)g.DrawRectangle(New Pen(rc(r), 3), rect2)r = r + 1End IfCase 4g.FillRectangle(Brushes.White, P.ClientRectangle)f1 = 0End Select
--------------------------------------------------------------------------------------------
Private Sub tr_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles trRed.Scroll, trGreen.Scroll, trBlue.ScrollPencolor = Color.FromArgb(trRed.Value, trGreen.Value, trBlue.Value)pColor.BackColor = PencolorEnd Sub
End Class
5 、程序运行
刘一哥GIS:专注测绘地理信息教育,探索地理奥秘,分享GIS价值!