📄 form1.vb
字号:
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents btnShow As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnSave = New System.Windows.Forms.Button()
Me.btnClear = New System.Windows.Forms.Button()
Me.btnShow = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnSave
'
Me.btnSave.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
Me.btnSave.Location = New System.Drawing.Point(40, 184)
Me.btnSave.Name = "btnSave"
Me.btnSave.TabIndex = 0
Me.btnSave.Text = "保 存"
'
'btnClear
'
Me.btnClear.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
Me.btnClear.Location = New System.Drawing.Point(144, 184)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 1
Me.btnClear.Text = "清 除"
'
'btnShow
'
Me.btnShow.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
Me.btnShow.Location = New System.Drawing.Point(256, 184)
Me.btnShow.Name = "btnShow"
Me.btnShow.TabIndex = 2
Me.btnShow.Text = "显 示"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(376, 230)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnShow, Me.btnClear, Me.btnSave})
Me.Name = "Form1"
Me.Text = "序列化和反序列化"
Me.ResumeLayout(False)
End Sub
#End Region
Private points As New ArrayList()
Private FileName As String = Application.StartupPath & "\Points.Pnt"
Private Sub AddPoints()
Dim i As Integer
Dim ValueX As Single, ValueY As Single
For i = 0 To 49
ValueX = CInt(Int((500 * Rnd()) + 1))
ValueY = CInt(Int((150 * Rnd()) + 1))
Console.WriteLine(ValueX)
Dim p As New CPosition(ValueX, ValueY)
points.Add(p)
Next
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Draw(g)
End Sub
Private Sub Draw(ByVal g As Graphics)
Dim i As Integer
For i = 0 To points.Count - 1
points(i).Draw(g)
Next
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim stream As _
New System.IO.FileStream(FileName, System.IO.FileMode.Create)
Dim binary As New BinaryFormatter()
binary.Serialize(stream, points)
stream.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Color.White)
End Sub
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Dim stream As New System.IO.FileStream(FileName, _
System.IO.FileMode.Open)
Dim binary As New BinaryFormatter()
points = CType(binary.Deserialize(stream), ArrayList)
stream.Close()
Dim g As Graphics = Me.CreateGraphics
Draw(g)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddPoints()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -