一、实验目的
掌握在.net环境下的绘图软件界面设计与交互技术。
二、实验准备学习在.net环境下的界面设计的一般原理与交互技术等基本知识。
三、实验内容将前7个实验内容集成到一个界面下,如直线段、圆、矩形与曲线的绘制填充,以及对图像的处理,并能利用交互技术实现对图元的选取、修改和交互。
四、实验过程及步骤 1、程序界面设计添加mainmnue控件,属性如:左添加2个panel控件,设置panel1.name=“P”,panel2作为一个调色板
3 、程序代码Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Math
--------------------------------------------------------------------------------------------
Public Class Form1
Dim f1 As Integer = 0
Dim lp(100, 20) As Point
Dim lf As Boolean
Dim ln(100) As Integer
Dim lleft As Boolean
Dim cp1(100) As Point
Dim cp2(100) As Point
Dim cr(100) As Single
Dim cf As Boolean
Dim rp1(100) As Point
Dim rp2(100) As Point
Dim rf As Boolean
Dim l As Integer
Dim c As Integer
Dim r As Integer
Dim Pencolor As Color
Dim lc(100) As Color
Dim cc(100) As Color
Dim rc(100) As Color
--------------------------------------------------------------------------------------------
Private Sub Init()
Dim i, j As Integer
For i = 0 To 100
ln(i) = 0
Next
lf = True
lleft = True
cf = True
rf = True
l = 0
c = 0
r = 0
End Sub
--------------------------------------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
P.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
P.Dock = DockStyle.Fill
P.BackColor = Color.White
GroupBox1.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.FixedSingle
pColor.BackColor = Color.Black
trRed.Maximum = 255
trRed.Minimum = 0
trRed.LargeChange = 17
trRed.SmallChange = 1
trRed.TickFrequency = 17
trRed.TickStyle = TickStyle.BottomRight '刻度线位于水平控件的底部或者垂直控件的右部
trRed.Value = 0 '刚开始设置为0
trGreen.Maximum = 255
trGreen.Minimum = 0
trGreen.LargeChange = 17
trGreen.SmallChange = 1
trGreen.TickFrequency = 17
trGreen.TickStyle = TickStyle.BottomRight
trGreen.Value = 0 '刚开始设置为0
trBlue.Maximum = 255
trBlue.Minimum = 0
trBlue.LargeChange = 17
trBlue.SmallChange = 1
trBlue.TickFrequency = 17
trBlue.TickStyle = TickStyle.BottomRight
trBlue.Value = 0 '刚开始设置为0
Init()
Pencolor = Color.Black
End Sub
--------------------------------------------------------------------------------------------
Private Sub mLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mLine.Click
f1 = 1
End Sub
--------------------------------------------------------------------------------------------
Private Sub mCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCircle.Click
f1 = 2
End Sub
--------------------------------------------------------------------------------------------
Private Sub mRectangle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mRectangle.Click
f1 = 3
End Sub
--------------------------------------------------------------------------------------------
Private Sub mClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mClear.Click
f1 = 4
P.Invalidate()
Init()
End Sub
--------------------------------------------------------------------------------------------
Private Sub P_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles P.MouseDown
Select Case f1
Case 1
If e.Button = MouseButtons.Left Then
If lf Then
lc(l) = Pencolor
lp(l, ln(l)) = New Point(e.X, e.Y)
lf = False
ElseIf Not lf Then
ln(l) = ln(l) + 1
End If
ElseIf e.Button = MouseButtons.Right Then
lleft = False
P.Invalidate()
End If
Case 2
If cf Then
cc(c) = Pencolor
cp1(c) = New Point(e.X, e.Y)
cf = False
ElseIf Not cf Then
cf = True
P.Invalidate()
End If
Case 3
If rf Then
rc(r) = Pencolor
rp1(r) = New Point(e.X, e.Y)
rf = False
ElseIf Not rf Then
rf = True
P.Invalidate()
End If
End Select
End Sub
--------------------------------------------------------------------------------------------
Private Sub P_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles P.MouseMove
Select Case f1
Case 1
If lleft Then
If Not lf Then
lp(l, ln(l) + 1) = New Point(e.X, e.Y)
P.Invalidate()
End If
End If
Case 2
If Not cf Then
cp2(c) = New Point(e.X, e.Y)
P.Invalidate()
End If
Case 3
If Not rf Then
rp2(r) = New Point(e.X, e.Y)
P.Invalidate()
End If
End Select
End Sub
--------------------------------------------------------------------------------------------
Private Sub P_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles P.Paint
Dim rect1 As Rectangle
Dim rect2 As Rectangle
Dim i, j As Integer
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
If l > 0 Then
For i = 0 To l - 1
For j = 0 To ln(i) - 1
g.DrawLine(Pens.White, lp(i, j), lp(i, j + 1))
g.DrawLine(New Pen(lc(i), 3), lp(i, j), lp(i, j + 1))
Next
Next
End If
If c > 0 Then
For i = 0 To c - 1
rect1 = 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)
Next
End If
If r > 0 Then
For i = 0 To r - 1
rect1 = 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)
Next
End If
Select Case f1
Case 1
If ln(l) >= 1 Then
For j = 0 To ln(l) - 1
g.DrawLine(Pens.White, lp(i, j), lp(i, j + 1))
g.DrawLine(New Pen(lc(l), 3), lp(i, j), lp(i, j + 1))
Next
End If
If lleft Then
If Not lf Then
g.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 If
Else
If ln(l) > 0 Then
l = l + 1
End If
lf = True
lleft = True
End If
Case 2
If Not cf Then
cr(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)
Else
cr(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 + 1
End If
Case 3
If Not rf Then
g.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)
Else
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)
r = r + 1
End If
Case 4
g.FillRectangle(Brushes.White, P.ClientRectangle)
f1 = 0
End Select
--------------------------------------------------------------------------------------------
Private Sub tr_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles trRed.Scroll, trGreen.Scroll, trBlue.Scroll
Pencolor = Color.FromArgb(trRed.Value, trGreen.Value, trBlue.Value)
pColor.BackColor = Pencolor
End Sub
End Class
5 、程序运行