AutoSaveSettings in WindowsForms on it exit

 














  1. Imports System.ComponentModel
  2. Imports System.Drawing.Text
  3. Imports System.Runtime.InteropServices
  4. Public Class Form1
  5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  6.         Width = My.Settings.width
  7.         Height = My.Settings.height
  8.         Location = New Drawing.Point(My.Settings.X, My.Settings.Y)
  9.     End Sub
  10.     Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
  11.         My.Settings.previous_datetime = $"{DateTime.Now:yyyy/MM/dd hh:mm:ss}"
  12.         My.Settings.visit_number += 1
  13.         My.Settings.X = Location.X
  14.         My.Settings.Y = Location.Y
  15.         My.Settings.width = Width
  16.         My.Settings.height = Height
  17.     End Sub
  18.     Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  19.         Try
  20.             Dim one_tenth As Single = ClientRectangle.Height / 10.0!
  21.             Dim nine_tenths As Single = one_tenth * 9.0!
  22.             Using Font2 As New Font(New FontFamily(GenericFontFamilies.Serif), one_tenth / 9),
  23.                   Font1 As New Font(New FontFamily(GenericFontFamilies.Serif), nine_tenths / 9),
  24.                   SolidBrush2 As New SolidBrush(Color.Red),
  25.                   SolidBrush1 As New SolidBrush(Color.Green)
  26.                 Dim Rectangle2 As New Rectangle(0, 0, Me.Width, one_tenth)
  27.                 Dim Rectangle1 As New Rectangle(0, one_tenth, Me.Width, nine_tenths)
  28.                 Dim g As Graphics = e.Graphics
  29.                 g.DrawString(My.Settings.previous_datetime, Font2, SolidBrush2, Rectangle2)
  30.                 g.DrawString(My.Settings.visit_number, Font1, SolidBrush1, Rectangle1)
  31.             End Using
  32.         Catch ex As Exception

  33.         End Try
  34.     End Sub
  35.     Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
  36.         Invalidate()
  37.     End Sub
  38.     Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  39.         If e.Control AndAlso e.KeyCode = Keys.Enter Then
  40.             Open_SettingsFile()
  41.         End If
  42.     End Sub
  43. End Class
  44. Module Module1
  45.     Sub Open_SettingsFile()
  46.         Try
  47.             Dim ExplorerProcess As New Process() With {
  48.             .StartInfo = New ProcessStartInfo(GetSettingsPath()) With {.Verb = "open",
  49.                            .UseShellExecute = True}}
  50.             ExplorerProcess.Start()
  51.         Catch ex As Exception
  52.             MsgBox(ex.Message)
  53.         End Try
  54.     End Sub
  55.     Function GetSettingsPath() As String

  56.         Dim LocalAppData As String = String.Empty
  57.         SHGetKnownFolderPath(
  58.                      New Guid(FolderId_LocalAppData),
  59.                      0,
  60.                      IntPtr.Zero,
  61.                      LocalAppData)
  62.         Dim SettingsPath As String
  63.         SettingsPath = IO.Path.Combine(LocalAppData, My.Application.Info.AssemblyName)
  64.         If Not String.IsNullOrEmpty(My.Application.Info.CompanyName) Then
  65.             SettingsPath = IO.Path.Combine(LocalAppData, My.Application.Info.CompanyName)
  66.         End If
  67.         Dim sp As New IO.DirectoryInfo(SettingsPath)
  68.         If sp IsNot Nothing AndAlso sp.Exists Then
  69.             Return sp.GetDirectories.Last().GetDirectories.Last().GetFiles.First.FullName
  70.         Else
  71.             Return LocalAppData
  72.         End If
  73.     End Function

  74.     Declare Unicode Function SHGetKnownFolderPath Lib "Shell32.dll" (
  75.         <InAttribute(), MarshalAs(UnmanagedType.LPStruct)> REF_KNOWN_FOLDER_ID_As_Guid As Guid,
  76.         KNOWN_FOLDER_FLAG As UInt32,
  77.         In_opt_hToken As IntPtr,
  78.         <OutAttribute()> ByRef Outptr_ppszPath As String) As Int32
  79.     Public Const FolderId_LocalAppData As String = "F1B32785-6FBA-4FCF-9D55-7B8E7F157091"
  80. End Module














  •         <System.Windows.Forms.ApplicationConfigurationSection>
  • <add key="DpiAwareness" value="PerMonitorV2" />
  • </System.Windows.Forms.ApplicationConfigurationSection>

























留言

這個網誌中的熱門文章

Marshalling

Calling a C# WPF library from C++

Marshalling II