今天继续讲功能
2.功能
2.9开机自启
设置程序随windows系统启动,其实就是就是将程序加载到注册表
Public Sub StartRunRegHKLM()
REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Microsoft \ Windows \ CurrentVersion \ Run
'Dim strName As String = Application.StartupPath + "\" + Application.ProductName + ".exe"
Dim strName As String = "E:\2024\工作助手WPF\工作助手WPF\bin\Debug\net8.0-windows\工作助手WPF.exe"
Dim strNewName As String = System.IO.Path.GetFileNameWithoutExtension(strName)
If Not System.IO.File.Exists(strName) Then Return
Try
Dim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)
If IsNothing(Rkey) Then
Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")
Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注册表,使程序开机时自动执行.
Else
Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注册表,使程序开机时自动执行。
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);
End Try
End Sub
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run
如果发现注册表中有此程序,说明注册成功,重启电脑,开机自启成功
考虑到用户会想要关闭开机自启,引入一个checkbox,同时该控件在弹窗中呈现,弹窗也要时圆角,所以xmal中在button下方添加
<Popup x:Name="myPopup" Placement="Center" AllowsTransparency="True" StaysOpen="False">
<Border x:Name="PopupBorder" Background="White" BorderThickness="1" CornerRadius="10" Margin="5">
<StackPanel Margin="20,0,0,0" Orientation="Vertical">
<TextBlock Text="This is a popup window." Margin="2"/>
<!--Add more content here if needed-->
<CheckBox x:Name="CheckBox1" Content="开机自启" HorizontalAlignment="Left" Margin="2,2,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked" Unchecked="CheckBox_unChecked"/>
</StackPanel>
</Border>
</Popup>
在vb.net中添加唤起popup弹窗
Sub btn_close_Click()
Debug.WriteLine("hello")
myPopup.IsOpen = True ' 设置Popup的是否打开
myPopup.VerticalOffset = -20 ' 设置垂直偏移量
myPopup.HorizontalOffset = -20 ' 设置水平偏移量
End Sub
添加checkbox对应的功能
Private Sub CheckBox_Checked(sender As Object, e As RoutedEventArgs)
'Debug.WriteLine("checked")
Dim strName As String = "E:\2024\工作助手WPF\工作助手WPF\bin\Debug\net8.0-windows\工作助手WPF.exe"
Dim strNewName As String = System.IO.Path.GetFileNameWithoutExtension(strName)
If Not System.IO.File.Exists(strName) Then Return
Try
Dim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)
If IsNothing(Rkey) Then
Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")
End If
Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注册表,使程序开机时自动执行.
Rkey.Close() '后加的,关闭注册表
Catch ex As Exception
MessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);
End Try
End Sub
Private Sub CheckBox_unChecked(sender As Object, e As RoutedEventArgs)
'Debug.WriteLine("unchecked")
Dim strName As String = "E:\2024\工作助手WPF\工作助手WPF\bin\Debug\net8.0-windows\工作助手WPF.exe"
Dim strNewName As String = System.IO.Path.GetFileNameWithoutExtension(strName)
If Not System.IO.File.Exists(strName) Then Return
Try
Dim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)
If IsNothing(Rkey) Then
Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")
End If
Rkey.DeleteValue(strNewName) '删除注册表,使程序开机时自动执行.
Debug.WriteLine(strNewName)
Rkey.Close() '后加的,关闭注册表
Catch ex As Exception
MessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);
End Try
End Sub
popup默认以button的底部为起点,偏移以此为基础,下面链接介绍popup的放置行为:
Popup 放置行为 - WPF .NET Framework | Microsoft Learn
对样式进行以下修改
如果margin只有一个值,表示上右下左的margin同为这个值。例如:margin:10px; 就等于 margin:10px 10px 10px 10px;
如果 margin 只有两个值,第一个值表示上下margin值,第二个值为左右margin的值。例如:margin:10px 20px; 就等于 margin:10px 20px 10px 20px;
如果margin有三个值,第一个值表示上margin值,第二个值表示左右margin的值,第三个值表示下margin的值。例如:margin:10px 20px 30px; 就等于 margin:10px 20px 30px 20px;
如果margin有四个值,那这四个值分别对应左上右下这四个margin值。例如:margin:10px 20px 30px 40px;
2.10由于由于要保存checkbox的状态,所以引入数据库,由于以前用过access数据库
添加引用Imports System.Data.OleDb
Sub Main()
' 连接字符串
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\2024\工作助手WPF\工作助手WPF\自动BOM数据库.accdb;"
' 创建连接对象
Dim connection As New OleDbConnection(connectionString)
Try
' 打开数据库连接
connection.Open()
' 连接成功,可以执行其他操作
' ...
Catch ex As Exception
' 连接失败,处理异常
Console.WriteLine("连接数据库失败:" + ex.Message)
Finally
' 关闭数据库连接
connection.Close()
End Try
End Sub
Dim connection As New OleDbConnection(connectionString)此处一直报错:未定义类型“OleDbConnection”
改为Dim connection As OleDbConnection后VS提示安装System.Data.OleDb,原来时这个包没有安装,安装后错误解除。以下是操作数据库的
Sub InsertAccess()
' 定义连接字符串,指向你的Access数据库文件
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.accdb"
Dim query As String = "INSERT INTO 参数设置 (参数名, 值) VALUES (@value1, @value2)"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(query, connection)
' 添加参数到命令
command.Parameters.AddWithValue("@value1", "YourValue1")
command.Parameters.AddWithValue("@value2", "YourValue2")
' 打开连接,执行命令,关闭连接
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Using
End Sub
Public Sub ModifyDataInAccess()
' 定义连接字符串,指向你的Access数据库文件
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.accdb"
Using connection As New OleDbConnection(connectionString) ' 创建连接对象
connection.Open() ' 打开连接
Dim commandText As String = "UPDATE yourTable SET ColumnName = @value WHERE ConditionColumn = @conditionValue" ' 创建SQL修改命令
Using command As New OleDbCommand(commandText, connection) ' 添加参数到命令
command.Parameters.AddWithValue("@value", "newValue") ' 新值
command.Parameters.AddWithValue("@conditionValue", "conditionValue") ' 条件值
Dim rowsAffected As Integer = command.ExecuteNonQuery() ' 执行命令
Console.WriteLine("Rows Affected: " & rowsAffected) ' 输出影响的行数
End Using
connection.Close() ' 关闭连接
End Using
End Sub
Public Sub ReadDataFromAccess()
'C:\database.accdb
'./database.accdb
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.accdb"
Dim query As String = "SELECT * FROM 参数设置"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(query, connection)
connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
' 处理每一行数据
' 例如:
MsgBox(reader(1).ToString())
End While
End Using
connection.Close()
End Using
End Sub
3.exe打包,如何把数据库打包进exe,先参考第三节VS 程序打包成一个独立的exe - Enigma Virtual Box-CSDN博客
想要把数据库打包进exe,参考以下设置
至此,一个exe文件就能实现:无锯齿无边框窗口,贴边隐藏,开机自启,自带数据库。在下面一段时间里,将做一些设置中的一些功能,比如开机自启后,打开客户自定义的一些文件,登录网页,代办事项等等。