一、實驗目的
掌握在.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價值!
?