AutoSaveSettings in WindowsForms on it exit
- Imports System.ComponentModel
- Imports System.Drawing.Text
- Imports System.Runtime.InteropServices
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Width = My.Settings.width
- Height = My.Settings.height
- Location = New Drawing.Point(My.Settings.X, My.Settings.Y)
- End Sub
- Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
- My.Settings.previous_datetime = $"{DateTime.Now:yyyy/MM/dd hh:mm:ss}"
- My.Settings.visit_number += 1
- My.Settings.X = Location.X
- My.Settings.Y = Location.Y
- My.Settings.width = Width
- My.Settings.height = Height
- End Sub
- Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
- Try
- Dim one_tenth As Single = ClientRectangle.Height / 10.0!
- Dim nine_tenths As Single = one_tenth * 9.0!
- Using Font2 As New Font(New FontFamily(GenericFontFamilies.Serif), one_tenth / 9),
- Font1 As New Font(New FontFamily(GenericFontFamilies.Serif), nine_tenths / 9),
- SolidBrush2 As New SolidBrush(Color.Red),
- SolidBrush1 As New SolidBrush(Color.Green)
- Dim Rectangle2 As New Rectangle(0, 0, Me.Width, one_tenth)
- Dim Rectangle1 As New Rectangle(0, one_tenth, Me.Width, nine_tenths)
- Dim g As Graphics = e.Graphics
- g.DrawString(My.Settings.previous_datetime, Font2, SolidBrush2, Rectangle2)
- g.DrawString(My.Settings.visit_number, Font1, SolidBrush1, Rectangle1)
- End Using
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
- Invalidate()
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- If e.Control AndAlso e.KeyCode = Keys.Enter Then
- Open_SettingsFile()
- End If
- End Sub
- End Class
- Module Module1
- Sub Open_SettingsFile()
- Try
- Dim ExplorerProcess As New Process() With {
- .StartInfo = New ProcessStartInfo(GetSettingsPath()) With {.Verb = "open",
- .UseShellExecute = True}}
- ExplorerProcess.Start()
- Catch ex As Exception
- MsgBox(ex.Message)
- End Try
- End Sub
- Function GetSettingsPath() As String
- Dim LocalAppData As String = String.Empty
- SHGetKnownFolderPath(
- New Guid(FolderId_LocalAppData),
- 0,
- IntPtr.Zero,
- LocalAppData)
- Dim SettingsPath As String
- SettingsPath = IO.Path.Combine(LocalAppData, My.Application.Info.AssemblyName)
- If Not String.IsNullOrEmpty(My.Application.Info.CompanyName) Then
- SettingsPath = IO.Path.Combine(LocalAppData, My.Application.Info.CompanyName)
- End If
- Dim sp As New IO.DirectoryInfo(SettingsPath)
- If sp IsNot Nothing AndAlso sp.Exists Then
- Return sp.GetDirectories.Last().GetDirectories.Last().GetFiles.First.FullName
- Else
- Return LocalAppData
- End If
- End Function
- Declare Unicode Function SHGetKnownFolderPath Lib "Shell32.dll" (
- <InAttribute(), MarshalAs(UnmanagedType.LPStruct)> REF_KNOWN_FOLDER_ID_As_Guid As Guid,
- KNOWN_FOLDER_FLAG As UInt32,
- In_opt_hToken As IntPtr,
- <OutAttribute()> ByRef Outptr_ppszPath As String) As Int32
- Public Const FolderId_LocalAppData As String = "F1B32785-6FBA-4FCF-9D55-7B8E7F157091"
- End Module
- <System.Windows.Forms.ApplicationConfigurationSection>
- <add key="DpiAwareness" value="PerMonitorV2" />
- </System.Windows.Forms.ApplicationConfigurationSection>
留言
張貼留言