Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Win32
Imports System.IO
Imports System.Runtime.Serialization.Formatters

Public Class SubForm : Inherits Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
      MyBase.New()
      System.Threading.Thread.Sleep(300)
      InitializeComponent()
      Me.BackgroundImage = Image.FromFile("..\vs7.bmp")
    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
  Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  Friend WithEvents OvalButton1 As GeniusHirai.ColorfulControls.OvalButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.OvalButton1 = New GeniusHirai.ColorfulControls.OvalButton
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(16, 16)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(288, 206)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 1
Me.PictureBox1.TabStop = False
'
'OvalButton1
'
Me.OvalButton1.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.OvalButton1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Blue
Me.OvalButton1.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.OvalButton1.ForeColor = System.Drawing.Color.White
Me.OvalButton1.Location = New System.Drawing.Point(96, 232)
Me.OvalButton1.Name = "OvalButton1"
Me.OvalButton1.Size = New System.Drawing.Size(128, 56)
Me.OvalButton1.TabIndex = 2
Me.OvalButton1.Text = "Close"
'
'SubForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(320, 302)
Me.Controls.Add(Me.OvalButton1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "SubForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "SubForm"
Me.ResumeLayout(False)

    End Sub

#End Region

  Private obj As New AppSetting      'ウィンドウの位置・サイズ管理用クラス

  Private Sub SubForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '規定値の設定（プライマリースクリーンの60%でセンタリング）
    Dim W, H As Integer
    obj.myPosition = New Point( _
      (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) \ 2, _
      (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) \ 2)
    obj.mySize = Me.Size
    '規定値をシリアライズ
    Dim arr() As Byte
    Dim mf As New MemoryStream(0)
    Dim bf As New Binary.BinaryFormatter
    bf.Serialize(mf, obj)         '← シリアライズ
    arr = mf.ToArray()            '← BYTE配列へ格納
    'レジストリからの読み込み
    Dim reg As RegistryKey
    reg = Registry.CurrentUser.CreateSubKey("MyKey")
    arr = CType(reg.GetValue("SizeAndPos", arr), Byte())
    mf.Capacity = arr.Length
    mf.Seek(0, SeekOrigin.Begin)
    mf.Write(arr, 0, arr.Length)
    mf.Seek(0, SeekOrigin.Begin)
    obj = CType(bf.Deserialize(mf), AppSetting)  '← デシリアライズ
    mf.Close()
    'ウィンドウのサイズと位置を復元
    Me.Size = obj.mySize
    Me.Location = obj.myPosition
  End Sub

  Private Sub SubForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    obj.mySize = Me.Size
    obj.myPosition = Me.Location
    Dim mf As New MemoryStream(0)
    Dim bf As New Binary.BinaryFormatter
    bf.Serialize(mf, obj)
    Dim reg As RegistryKey
    reg = Registry.CurrentUser.CreateSubKey("MyKEY")
    reg.SetValue("SizeAndPos", mf.ToArray())
    reg.Close()
    mf.Close()
    e.Cancel = True
    Me.Hide()
  End Sub

  Private Sub OvalButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OvalButton1.Click
    Me.Close()
  End Sub

End Class

'//
'// AppSettingクラス（シリアル化可能）
'//
<Serializable()> _
Public Class AppSetting
  Public myPosition As Point 'ウィンドウの位置
  Public mySize As Size      'ウィンドウのサイズ
End Class
