它可能对其他人有用:
Public Shared Sub SaveWindowPosition(ByVal formToSave As Form, ByVal userid As Integer)
Dim formPosLeft As Integer = formToSave.Location.X
Dim formPosTop As Integer = formToSave.Location.Y
Dim formDimWidth As Integer = formToSave.Width
Dim formDimHeight As Integer = formToSave.Height
Dim formState As Integer = formToSave.WindowState
Dim formName As String = formToSave.Name
' Apri il file dove memorizzo le posizioni delle varie form
'
Dim xdoc As XDocument
Try
xdoc = XDocument.Load(FILE_NAME)
Catch ex As Exception
' Non trovato, va creato uno nuovo
'
Dim settings As New XmlWriterSettings()
settings.Indent = True
Dim writer As XmlWriter = XmlWriter.Create(FILE_NAME, settings)
writer.WriteStartDocument()
writer.WriteStartElement("Root")
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
Try
xdoc = XDocument.Load(FILE_NAME)
Catch ex2 As Exception
' Nemmeno questa volta è riuscito ad aprire il file, basta
'
Exit Sub
End Try
End Try
Dim elementRoot As XElement = xdoc.Element("Root")
' Cerca l'utente corrente (per id)
'
Dim cercaUtente As IEnumerable(Of XElement) =
From c In elementRoot.Elements("Utente") Where c.Attribute("id").Value = CStr(userid)
Select c
If cercaUtente.Count = 0 Then
' Utente non trovato, crealo con tutti gli altri elementi
'
Dim xutente As XElement =
<Utente id=<%= userid %>>
<Form name=<%= formName %>>
<Position top=<%= formPosTop %> left=<%= formPosLeft %> width=<%= formDimWidth %> height=<%= formDimHeight %> state=<%= formState %>></Position>
</Form>
</Utente>
elementRoot.Add(xutente)
Else
' Utente trovato, cerca la form
'
Dim cercaForm As IEnumerable(Of XElement) =
From c In cercaUtente.Elements("Form") Where c.Attribute("name").Value = formName
Select c
If cercaForm.Count = 0 Then
' Form non trovata, creala
'
Dim formPos As XElement = _
<Form name=<%= formName %>>
<Position top=<%= formPosTop %> left=<%= formPosLeft %> width=<%= formDimWidth %> height=<%= formDimHeight %> state=<%= formState %>></Position>
</Form>
cercaUtente.ElementAt(0).Add(formPos)
Else
' Trovato tutto, sostituisci gli attributi
'
Dim position As XElement = cercaForm.ElementAt(0).Element("Position")
position.ReplaceAttributes(
{
New XAttribute("top", formPosTop),
New XAttribute("left", formPosLeft),
New XAttribute("width", formDimWidth),
New XAttribute("height", formDimHeight),
New XAttribute("state", formState)
})
End If
End If
' Salva il file
'
xdoc.Save(FILE_NAME)
End Sub
Public Shared Sub SetWindowPosition(ByVal formToPosition As Form, ByVal userid As Integer)
formToPosition.SuspendLayout()
Dim xdoc As XDocument
Try
xdoc = XDocument.Load(FILE_NAME)
Catch ex As Exception
' File non trovato, nulla da fare
'
Exit Sub
End Try
Dim elementRoot As XElement = xdoc.Element("Root")
' Cerca l'utente corrente (per id)
'
Dim cercaUtente As IEnumerable(Of XElement) =
From c In elementRoot.Elements("Utente") Where c.Attribute("id").Value = CStr(userid)
Select c
If cercaUtente.Count = 0 Then Exit Sub
Dim cercaForm As IEnumerable(Of XElement) =
From c In cercaUtente.Elements("Form") Where c.Attribute("name").Value = formToPosition.Name
Select c
If cercaForm.Count = 0 Then Exit Sub
' Preleva tutti gli attributi
'
Dim position As XElement = cercaForm.ElementAt(0).Element("Position")
Dim top As Integer = CInt(position.Attribute("top"))
Dim left As Integer = CInt(position.Attribute("left"))
Dim width As Integer = CInt(position.Attribute("width"))
Dim height As Integer = CInt(position.Attribute("height"))
Dim state As FormWindowState = CType([Enum].Parse(GetType(FormWindowState), CStr(position.Attribute("state"))), FormWindowState)
' Imposta posizione, dimensione e stato, ma solo se lo stato è normale, altrimenti ignora i valori
'
formToPosition.WindowState = state
If state = FormWindowState.Normal Then
formToPosition.Location = New Point(left, top)
formToPosition.Width = width
formToPosition.Height = height
formToPosition.StartPosition = FormStartPosition.Manual
End If
' TODO: controllare che posizione e dimensioni siano NORMALI cioè che la form non vada a finire fuori dallo schermo
formToPosition.ResumeLayout(False)
End Sub