📄 form1.vb
字号:
Imports System.Runtime.InteropServices
Public Class Form1
Private pointList As ArrayList
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'构造颜色对话框
Dim cdlg As New ColorDialog()
If cdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
'把选取的颜色设为按钮前景色
button1.ForeColor = cdlg.Color
'刷新窗口
Me.Invalidate(Me.ClientRectangle)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pointList = New ArrayList()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'用当前鼠标位置构造Point对象
Dim pt As New Point(e.X, e.Y)
'加入到数据中
pointList.Add(pt)
'刷新窗口
Me.Invalidate(Me.ClientRectangle)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
'使用指定颜色和宽度构造画笔对象
Dim pen As New Pen(button1.ForeColor, Single.Parse(textBox1.Text))
For i As Integer = 0 To pointList.Count - 2
'绘制直线
g.DrawLine(pen, DirectCast(pointList(i), Point), DirectCast(pointList(i + 1), Point))
Next
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'构造保存对话框
Dim sfdlg As New SaveFileDialog()
'设置对话框属性
sfdlg.Filter = "Point Connector File(*.ptc)|*.ptc|All Files(*.*)|*.*"
sfdlg.FilterIndex = 1
'数据流
Dim fs As IO.FileStream = Nothing
Dim bw As IO.BinaryWriter = Nothing
If sfdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
'使用文件名称创建新文件
fs = New IO.FileStream(sfdlg.FileName, IO.FileMode.Create, System.IO.FileAccess.Write)
'构造二进制写入类
bw = New IO.BinaryWriter(fs)
'写入线条宽度
bw.Write(Single.Parse(textBox1.Text))
'写入线条颜色
bw.Write(button1.ForeColor.ToArgb())
'写入点的个数
bw.Write(pointList.Count)
For i As Integer = 0 To pointList.Count - 1
'写入点的坐标
bw.Write(DirectCast(pointList(i), Point).X)
bw.Write(DirectCast(pointList(i), Point).Y)
Next
Catch excep As Exception
MessageBox.Show(excep.Message.ToString(), "出错", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Finally
'关闭流
bw.Close()
fs.Close()
End Try
End If
End Sub
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
'构造打开文件对话框
Dim ofdlg As New OpenFileDialog()
'设置对话框属性
ofdlg.Filter = "Point Connector File(*.ptc)|*.ptc|All Files(*.*)|*.*"
ofdlg.FilterIndex = 1
'数据流
Dim fs As IO.FileStream = Nothing
Dim br As IO.BinaryReader = Nothing
If ofdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
'使用用户指定名称构造FileStream对象
fs = New IO.FileStream(ofdlg.FileName, IO.FileMode.Create, System.IO.FileAccess.Write)
'构造二进制读取对象
br = New IO.BinaryReader(fs)
'读取线条宽度
textBox1.Text = br.ReadSingle().ToString()
'读取线条颜色
button1.ForeColor = Color.FromArgb(br.ReadInt32())
'清除内容
pointList.Clear()
'读取点的数目
Dim count As Integer = br.ReadInt32()
For i As Integer = 0 To count - 1
'读取点的坐标,构造新的Point对象,并加入到pointList中
pointList.Add(New Point(br.ReadInt32(), br.ReadInt32()))
Next
'刷新窗口
Me.Invalidate(Me.ClientRectangle)
Catch excep As Exception
MessageBox.Show(excep.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Finally
'关闭流
br.Close()
fs.Close()
End Try
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -