在 .NET 4 winforms 中,我有一个包含多个项目的解决方案。在其中两个项目中,我想记住每个表单的表单设置,所以我让每个表单都继承自“CustomForm”,它保存和加载位置、大小等设置类,使用生成表单的名称作为参考。
作为参考,CustomForm
代码如下所示:
Public Class CustomForm : Inherits Form
Private ReadOnly Property SuidenSettings As My.MySettings
Get
Return My.MySettings.Default
End Get
End Property
''' <summary>
''' Restore the window settings from the settings file
''' </summary>
''' <remarks></remarks>
Public Sub RestoreSettings() Handles MyClass.Load
Dim state As FormWindowState
Dim settings As List(Of String) = SuidenSettings.Properties.Cast(Of SettingsProperty).Where(Function(p) p.Name.StartsWith(Me.Name)).Select(Function(p) p.Name).ToList()
Dim primaryWorkArea As Rectangle = Screen.PrimaryScreen.WorkingArea
' Make sure each setting exists before accessing or exceptions will fly
If settings.Any() Then
If settings.Contains(Me.Name & "_Location") Then
Me.Location = CType(SuidenSettings(Me.Name & "_Location"), Point)
End If
If settings.Contains(Me.Name & "_Size") Then
Me.Size = CType(SuidenSettings(Me.Name & "_Size"), Size)
End If
If settings.Contains(Me.Name & "_State") Then
If [Enum].TryParse(SuidenSettings(Me.Name & "_State").ToString(), state) Then
Me.WindowState = state
Else
Me.WindowState = FormWindowState.Normal
End If
End If
End If
' Verify that the form is actually positioned on screen, otherwise position it on the primary screen
If Not IsOnScreen() Then
Me.Location = New Point(Me.Location.X Mod primaryWorkArea.Width, Me.Location.Y Mod primaryWorkArea.Height)
If Not primaryWorkArea.Contains(New Rectangle(Me.Location, Me.Size)) Then
Me.Location = New Point(primaryWorkArea.Top, primaryWorkArea.Left)
End If
End If
End Sub
''' <summary>
''' Save the form settings to the settings file
''' </summary>
Public Overridable Sub SaveSettings() Handles MyClass.FormClosed
Dim settings As List(Of String) = SuidenSettings.Properties.Cast(Of SettingsProperty).Where(Function(p) p.Name.StartsWith(Me.Name)).Select(Function(p) p.Name).ToList()
' Make sure each setting exists before accessing or exceptions will fly
If settings.Any() Then
If settings.Contains(Me.Name & "_Location") Then
SuidenSettings(Me.Name & "_Location") = Me.Location
End If
If settings.Contains(Me.Name & "_Size") Then
SuidenSettings(Me.Name & "_Size") = Me.Size
End If
If settings.Contains(Me.Name & "_State") Then
SuidenSettings(Me.Name & "_State") = Me.WindowState
End If
Call SuidenSettings.Save()
End If
End Sub
''' <summary>
''' Check if the form is at least partially visible on screen
''' </summary>
''' <returns>True if the form is partially visible on screen, else false</returns>
Private Function IsOnScreen() As Boolean
Dim formRectangle As Rectangle = New Rectangle(Me.Location, Me.Size)
Return Screen.AllScreens.Any(Function(s) s.WorkingArea.IntersectsWith(formRectangle))
End Function
End Class
当我仍然有一个项目并且效果很好时,我使用了这种方法。现在,我不得不复制该项目来运行两个(并排略有不同的程序 - 请不要问我为什么必须这样做,我只是不得不这样做)。因此,我取出了该CustomForm
对象并将其放在一个单独的项目中,我在两个应用程序项目中都引用了该项目。
我现在遇到的问题是我不知道如何修改CustomForm
代码,以便它引用一个设置文件,该文件是使用表单的项目的本地文件,而不是共享项目中的设置文件.
由于使用表单名称检索表单设置并且两个项目实际上相同,因此所有表单都具有相同的名称。我想避免将项目中的所有表单重命名为稍微不同的名称,而是从项目本地的设置文件中加载设置。那可能吗?
C# 或 VB.NET 中的任何答案都可以。