📄 form1.vb
字号:
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports Microsoft.Win32
Public Class Form1
Public Sub New()
InitializeComponent()
Try
'读取保存在注册表中相应的键值,
'并判断注册表中是否有相应键值
If ReadSettings() = False Then
listBoxMessages.Items.Add("注册表中没有注册信息")
Else
listBoxMessages.Items.Add("从注册表中读到的信息")
End If
Me.StartPosition = FormStartPosition.Manual
Catch e As Exception
listBoxMessages.Items.Add("读注册表错误:")
listBoxMessages.Items.Add(e.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub SaveSettings()
'打开子键Software
Dim softwareKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
'在Software键下创建一个子键"读写注册表"
Dim wroxKey As RegistryKey = softwareKey.CreateSubKey("读写注册表")
'在子键"读写注册表"下创建一个
Dim selfPlacingWindowKey As RegistryKey = wroxKey.CreateSubKey("SelfPlacingWindow")
'把窗体的状态信息保存在子键SelfPlacingWindow中
selfPlacingWindowKey.SetValue("BackColor", DirectCast(BackColor.ToKnownColor(), Object))
selfPlacingWindowKey.SetValue("Red", DirectCast(CInt(BackColor.R), Object))
selfPlacingWindowKey.SetValue("Green", DirectCast(CInt(BackColor.G), Object))
selfPlacingWindowKey.SetValue("Blue", DirectCast(CInt(BackColor.B), Object))
selfPlacingWindowKey.SetValue("Width", DirectCast(Width, Object))
selfPlacingWindowKey.SetValue("Height", DirectCast(Height, Object))
selfPlacingWindowKey.SetValue("X", DirectCast(DesktopLocation.X, Object))
selfPlacingWindowKey.SetValue("Y", DirectCast(DesktopLocation.Y, Object))
selfPlacingWindowKey.SetValue("WindowState", DirectCast(WindowState.ToString(), Object))
End Sub
Private Function ReadSettings() As Boolean
'打开子键Software
Dim softwareKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software")
'打开子键"读写注册表"
Dim wroxKey As RegistryKey = softwareKey.OpenSubKey("读写注册表")
'子键“读写注册表”若为空,则返回false
If wroxKey Is Nothing Then
Return False
End If
'打开子键SelfPlacingWindow
Dim selfPlacingWindowKey As RegistryKey = wroxKey.OpenSubKey("SelfPlacingWindow")
'子键selfPlacingWindowKey若为空,返回false
'否则在列表框中列出信息
If selfPlacingWindowKey Is Nothing Then
Return False
Else
listBoxMessages.Items.Add("成功地打开了键:")
listBoxMessages.Items.Add(selfPlacingWindowKey.ToString())
End If
'读取窗体的背景颜色,并显示在列表中
Dim redComponent As Integer = CInt(selfPlacingWindowKey.GetValue("Red"))
Dim greenComponent As Integer = CInt(selfPlacingWindowKey.GetValue("Green"))
Dim blueComponent As Integer = CInt(selfPlacingWindowKey.GetValue("Blue"))
Me.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent)
listBoxMessages.Items.Add("Background color: " + BackColor.Name)
'读取窗体在屏幕上的坐标,并显示在列表中
Dim X As Integer = CInt(selfPlacingWindowKey.GetValue("X"))
Dim Y As Integer = CInt(selfPlacingWindowKey.GetValue("Y"))
Me.DesktopLocation = New Point(X, Y)
listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString())
'读取窗体的尺寸,并显示在列表中
Me.Height = CInt(selfPlacingWindowKey.GetValue("Height"))
Me.Width = CInt(selfPlacingWindowKey.GetValue("Width"))
listBoxMessages.Items.Add("Size: " + New Size(Width, Height).ToString())
'读取窗体的可视化状态,并显示在列表中
Dim initialWindowState As String = DirectCast(selfPlacingWindowKey.GetValue("WindowState"), String)
Me.WindowState = DirectCast(FormWindowState.Parse(WindowState.[GetType](), initialWindowState), FormWindowState)
listBoxMessages.Items.Add("Window State: " + initialWindowState)
'返回true
Return True
End Function
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
'把窗体的各项修改保存在注册表中
SaveSettings()
MyBase.Dispose(disposing)
End Sub
Private Sub btnChooseBColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseBColor.Click
'构造一个颜色对话框
Dim chooseColorDialog As New ColorDialog()
If chooseColorDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.BackColor = chooseColorDialog.Color
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -