今天繼續講功能
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 ReturnTryDim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)If IsNothing(Rkey) ThenRkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注冊表,使程序開機時自動執行.ElseRkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注冊表,使程序開機時自動執行。 End IfCatch ex As ExceptionMessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);End TryEnd 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 ReturnTryDim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)If IsNothing(Rkey) ThenRkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")End IfRkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注冊表,使程序開機時自動執行.Rkey.Close() '后加的,關閉注冊表Catch ex As ExceptionMessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);End TryEnd SubPrivate 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 ReturnTryDim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True)If IsNothing(Rkey) ThenRkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")End IfRkey.DeleteValue(strNewName) '刪除注冊表,使程序開機時自動執行.Debug.WriteLine(strNewName)Rkey.Close() '后加的,關閉注冊表Catch ex As ExceptionMessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message);End TryEnd 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 TryEnd 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 UsingEnd SubPublic 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 Usingconnection.Close() ' 關閉連接End UsingEnd SubPublic Sub ReadDataFromAccess()'C:\database.accdb'./database.accdbDim 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 WhileEnd Usingconnection.Close()End UsingEnd Sub
3.exe打包,如何把數據庫打包進exe,先參考第三節VS 程序打包成一個獨立的exe - Enigma Virtual Box-CSDN博客
?想要把數據庫打包進exe,參考以下設置
至此,一個exe文件就能實現:無鋸齒無邊框窗口,貼邊隱藏,開機自啟,自帶數據庫。在下面一段時間里,將做一些設置中的一些功能,比如開機自啟后,打開客戶自定義的一些文件,登錄網頁,代辦事項等等。