Public Class MainForm
    Inherits System.Windows.Forms.Form

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

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    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 MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem12 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.MenuItem6 = New System.Windows.Forms.MenuItem
        Me.MenuItem7 = New System.Windows.Forms.MenuItem
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.MenuItem8 = New System.Windows.Forms.MenuItem
        Me.MenuItem9 = New System.Windows.Forms.MenuItem
        Me.MenuItem10 = New System.Windows.Forms.MenuItem
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.MenuItem11 = New System.Windows.Forms.MenuItem
        Me.MenuItem12 = New System.Windows.Forms.MenuItem
        Me.MenuItem13 = New System.Windows.Forms.MenuItem
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem11, Me.MenuItem12, Me.MenuItem5, Me.MenuItem4, Me.MenuItem13})
        Me.MenuItem1.Text = "ファイル(&F)"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlN
        Me.MenuItem2.Text = "新規作成"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlO
        Me.MenuItem3.Text = "開く"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 4
        Me.MenuItem5.Text = "-"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 5
        Me.MenuItem4.Text = "終了(&X)"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 1
        Me.MenuItem6.MdiList = True
        Me.MenuItem6.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem7, Me.MenuItem8, Me.MenuItem9, Me.MenuItem10})
        Me.MenuItem6.Text = "ウィンドウ(&W)"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 0
        Me.MenuItem7.Text = "重ねて表示"
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem6})
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Filter = "テキストファイル（*.txt）|*.txt|JPEGファイル（*.jpg）|*.jpg"
        Me.OpenFileDialog1.InitialDirectory = "c:\"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 1
        Me.MenuItem8.Text = "上下に並べて表示"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 2
        Me.MenuItem9.Text = "左右に並べて表示"
        '
        'MenuItem10
        '
        Me.MenuItem10.Index = 3
        Me.MenuItem10.Text = "アイコンの整列"
        '
        'MenuItem11
        '
        Me.MenuItem11.Index = 2
        Me.MenuItem11.Shortcut = System.Windows.Forms.Shortcut.CtrlS
        Me.MenuItem11.Text = "上書き保存"
        '
        'MenuItem12
        '
        Me.MenuItem12.Index = 3
        Me.MenuItem12.Text = "名前を付けて保存(&A)..."
        '
        'MenuItem13
        '
        Me.MenuItem13.Index = 6
        Me.MenuItem13.Text = "-"
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(464, 366)
        Me.IsMdiContainer = True
        Me.Menu = Me.MainMenu1
        Me.Name = "MainForm"
        Me.Text = "MDIアプリケーション"

    End Sub

#End Region
    Dim MenuNum As Byte = 0
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim NewMDIChild As New Form1

        NewMDIChild.MdiParent = Me
        NewMDIChild.Show()

        Dim i As Byte
        Dim ItemName As String

        Try
            MenuNum = GetSetting("MyMDIApp", "menu", "num")
        Catch
            Exit Sub
        End Try
        If MenuNum <> 0 Then
            For i = 7 To MenuNum + 6
                ItemName = GetSetting("MyMDIApp", "menu", i)
                MenuItem1.MenuItems.Add(ItemName, New EventHandler(AddressOf addmenu_Click))
            Next
        End If
    End Sub

    Dim cnt As Byte = 1

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Dim NewForm As New Form
        Dim NewRtbox As New RichTextBox

        cnt += 1

        With NewRtbox
            .Dock = DockStyle.Fill
            .ShowSelectionMargin = True
        End With

        With NewForm
            .MdiParent = Me
            .Text = "新規文書" & cnt
            .Controls.Add(NewRtbox)
            .Show()
        End With
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        Dim Fname As String
        Dim Extname As String

        If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Fname = OpenFileDialog1.FileName

            Extname = Microsoft.VisualBasic.Mid(Fname, Len(Fname) - 2, 3)

            Select Case Extname
                Case "jpg", "JPG"
                    imageview(Fname)

                Case "txt", "TXT"
                    textview(Fname)
            End Select
            addmenu(Fname)
        End If
    End Sub

    Private Sub textview(ByVal Fname As String)
        Dim NewForm As New Form
        Dim NewRtbox As New RichTextBox

        With NewRtbox
            .Dock = DockStyle.Fill
            .ShowSelectionMargin = True
            .LoadFile(Fname, RichTextBoxStreamType.PlainText)
        End With

        With NewForm
            .MdiParent = Me
            .Text = Fname
            .Controls.Add(NewRtbox)
            .Show()
        End With
    End Sub

    Private Sub imageview(ByVal Fname As String)
        Dim NewForm As New Form
        Dim NewPicbox As New PictureBox

        With NewPicbox
            .Dock = DockStyle.Fill
            .SizeMode = PictureBoxSizeMode.StretchImage
            .Image = Image.FromFile(Fname)
        End With

        With NewForm
            .MdiParent = Me
            .Text = Fname
            .Controls.Add(NewPicbox)
            .Show()
        End With
    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
        End
    End Sub

    Private Sub MenuItem7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub MenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem10.Click
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub MenuItem11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem11.Click
        Dim fname As String

        fname = Me.ActiveMdiChild.Text
        CType(Me.ActiveMdiChild.ActiveControl, RichTextBox). _
              SaveFile(fname, RichTextBoxStreamType.PlainText)
    End Sub

    Private Sub MenuItem12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem12.Click
        Dim fname As String

        With Me.SaveFileDialog1
            .FileName = Me.ActiveMdiChild.Text
            .DefaultExt = "txt"
            .Filter = "テキストファイル（*.txt）|*.txt"
            .InitialDirectory = "c:\"
            If .ShowDialog() = DialogResult.OK Then
                fname = .FileName
                CType(Me.ActiveMdiChild.ActiveControl, RichTextBox). _
                       SaveFile(fname, RichTextBoxStreamType.PlainText)
            End If
        End With
    End Sub

    Private Sub addmenu(ByVal fname As String)
        MenuNum += 1
        With MenuItem1
            If MenuNum > 5 Then
                .MenuItems.RemoveAt(7)
                MenuNum = 5
            End If
            .MenuItems.Add(fname, New EventHandler(AddressOf addmenu_Click))
        End With
    End Sub

    Protected Sub addmenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim fname As String = sender.text
        Dim Extname As String

        Extname = Microsoft.VisualBasic.Mid(fname, Len(fname) - 2, 3)

        Select Case Extname
            Case "jpg", "JPG"
                imageview(fname)

            Case "txt", "TXT"
                textview(fname)
        End Select
    End Sub

    Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MenuNum = 0 Then
            SaveSetting("MyMDIApp", "menu", "num", MenuNum)
            Exit Sub
        End If

        Dim i As Integer
        For i = 7 To MenuNum + 6
            SaveSetting("MyMDIApp", "menu", i, MenuItem1.MenuItems(i).Text)
        Next
        SaveSetting("MyMDIApp", "menu", "num", MenuNum)
    End Sub

End Class
