Option Explicit On 
Imports System.Xml
Imports System.IO

Public Class yotei_Form2
    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)
        MyBase.Dispose(disposing)
    End Sub

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents Panel3 As System.Windows.Forms.Panel
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button3 = New System.Windows.Forms.Button
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.Label2 = New System.Windows.Forms.Label
        Me.Panel3 = New System.Windows.Forms.Panel
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.White
        Me.Panel1.Location = New System.Drawing.Point(8, 24)
        Me.Panel1.Size = New System.Drawing.Size(224, 264)
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold)
        Me.Label1.Location = New System.Drawing.Point(184, 0)
        Me.Label1.Size = New System.Drawing.Size(48, 20)
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Button3
        '
        Me.Button3.Font = New System.Drawing.Font("MS UI Gothic", 8.0!, System.Drawing.FontStyle.Bold)
        Me.Button3.Location = New System.Drawing.Point(80, 288)
        Me.Button3.Size = New System.Drawing.Size(40, 20)
        Me.Button3.Text = "現在"
        '
        'Panel2
        '
        Me.Panel2.BackColor = System.Drawing.Color.Gainsboro
        Me.Panel2.Controls.Add(Me.Label2)
        Me.Panel2.Location = New System.Drawing.Point(32, 288)
        Me.Panel2.Size = New System.Drawing.Size(40, 20)
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold)
        Me.Label2.Location = New System.Drawing.Point(8, 2)
        Me.Label2.Size = New System.Drawing.Size(40, 20)
        Me.Label2.Text = "<<"
        '
        'Panel3
        '
        Me.Panel3.BackColor = System.Drawing.Color.Gainsboro
        Me.Panel3.Controls.Add(Me.Label3)
        Me.Panel3.Location = New System.Drawing.Point(168, 288)
        Me.Panel3.Size = New System.Drawing.Size(40, 20)
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold)
        Me.Label3.Location = New System.Drawing.Point(8, 2)
        Me.Label3.Size = New System.Drawing.Size(40, 20)
        Me.Label3.Text = ">>"
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold)
        Me.Label4.Location = New System.Drawing.Point(8, 0)
        Me.Label4.Size = New System.Drawing.Size(128, 20)
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("MS UI Gothic", 8.0!, System.Drawing.FontStyle.Bold)
        Me.Button1.Location = New System.Drawing.Point(120, 288)
        Me.Button1.Size = New System.Drawing.Size(40, 20)
        Me.Button1.Text = "戻る"
        '
        'yotei_Form2
        '
        Me.ClientSize = New System.Drawing.Size(240, 376)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Panel3)
        Me.Controls.Add(Me.Panel2)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Panel1)
        Me.Text = "ｶｰﾄﾞ選択"

    End Sub

#End Region
    Dim xReader As XmlTextReader
    Dim imageArray As ArrayList
    Dim altArray As ArrayList
    Dim sortArray As ArrayList
    Dim no As Integer
    Dim myX, myY As Integer
    Dim nowPage As Integer = 1

    '1ページに表示させる画像数
    Dim imageCount As Integer = 6

    Dim pageCount As Integer = 0
    Dim maxImage As Integer = 114
    Dim dummyPageNo As Integer
    Dim picNo As String

    Dim selectImageName As String

    Dim mImage As Image
    Dim xmldoc As XmlDocument


    '114枚の画像を左右ボタンでページングする
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '上部のメニューを消す
        Me.WindowState = FormWindowState.Maximized
        Label4.Text = myYear & "年" & selectMonth & "月" & selectDate & "日"
        ImageNo = 0

        '実機のフォルダーにXMLファイルを置く
        xReader = New XmlTextReader("\Program Files\Rainman_calendar\calendar_image\image70\RainmanCard.xml")
        imageArray = New ArrayList
        altArray = New ArrayList
        sortArray = New ArrayList

        no = 0
        xReader.Read()
        xReader.Read()
        xReader.Read()
        xReader.Read()
        While xReader.Read
            xReader.MoveToFirstAttribute()
            altArray.Add(xReader.Value)
            xReader.MoveToNextAttribute()
            sortArray.Add(xReader.Value)

            If xReader.IsStartElement("image") = True Then
                imageArray.Add(xReader.ReadElementString)
            End If
        End While

        '総ページ数を求める
        pageCount = imageArray.Count / imageCount

        xReader.Close()

        'フォーム2より戻った場合の判定
        If allNo = 1 Then ImageNo = selectedImageNo

        pageNo = (ImageNo / imageCount) + 1

        Label1.Text = PageNo & "/" & pageCount

        ImageShowGo()
    End Sub

    '左右ボタンで呼び出される共通のプロシージャ
    Sub ImageShowGo()
        no = 0

        If ImageNo < 0 Then Exit Sub

        'フォーム2より戻った場合の判定
        If allNo = 1 Then
            beginNo = begin
            lastNo = last
            ImageNo = selectedImageNo
        Else
            beginNo = ((PageNo) * imageCount) - imageCount
            lastNo = ((PageNo) * imageCount) - 1
            allNo = 0
        End If

        If ImageNo >= imageArray.Count - imageCount + 1 Then
            beginNo = 0
            lastNo = imageCount - 1
            ImageNo = 0
            PageNo = 1
        End If
     
        For i As Integer = beginNo To lastNo
            no = no + 1
            If no = 1 Then
                myX = 0
                myY = 0
            ElseIf no = 2 Then
                myX = 98
                myY = 0
            ElseIf no = 3 Then
                myX = 0
                myY = 87
            ElseIf no = 4 Then
                myX = 98
                myY = 87

            ElseIf no = 5 Then
                myX = 0
                myY = 174
            ElseIf no = 6 Then
                myX = 98
                myY = 174
            Else
                Exit For
            End If

            Dim myTop As Integer = 1
            Dim myLeft As Integer = 28


            'カードのインデックス番号
            mImage = New Bitmap("\Program Files\Rainman_calendar\calendar_image\image70\" & imageArray((no - 1) + ImageNo).ToString)
            selectedImageNo = ImageNo

            allNo = 0

            Dim pictureBox As PictureBox = New PictureBox
            With pictureBox
                .Text = i.ToString
                .Width = 70
                .Height = 70
                .Location = New Point(myX + myLeft, myY + myTop)
                .Image = mImage
            End With

            Dim myLabel As Label = New Label
            With myLabel
                .Font = New Font("ＭＳ Ｐゴシック", "9", FontStyle.Regular)
                .Size = New Size(110, 16)
                .TextAlign = ContentAlignment.TopCenter
                .ForeColor = Color.Black
                .Text = altArray((no - 1) + ImageNo)
                .Location = New Point(myX + myLeft - 22, myY + myTop + 71)
            End With

            AddHandler pictureBox.Click, AddressOf Me.Picture_Click
            With Panel1
                .Controls.Add(pictureBox)
                .Controls.Add(myLabel)
            End With
        Next

        Label1.Text = PageNo & "/" & pageCount

    End Sub

    '画像をクリックしたときの処理
    Private Sub Picture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'WAVを鳴らす()
        'Dim mySound As New CardSelectSound
        'mySound.Owari_Play("\Program Files\Rainman_calendar\sound\" & card_selectWav)


        formNo = 2
        Dim picName As PictureBox = CType(sender, PictureBox)
        picNo = picName.Text

        picName.BackColor = Color.Red
        '画像の連番のインデックス
        Dim selectImageNo As Integer = Integer.Parse(picNo)

        selectImageName = imageArray(selectImageNo)
        selectTitle = altArray(selectImageNo)

        showImage = selectImageName

        begin = beginNo
        last = lastNo

        allNo = 1
        mImage.Dispose()

        '選択された画像とタイトルをXMLファイルで保存する
        xmldoc = New XmlDocument
        If File.Exists("\Program Files\Rainman_calendar\data\calendarInfo.xml") = False Then
            xmldoc.LoadXml("<?xml version='1.0'?><予定 />")
        Else
            xmldoc.Load("\Program Files\Rainman_calendar\data\calendarInfo.xml")
        End If

        Dim root As XmlNode = xmldoc.DocumentElement
        Dim infoElement As XmlElement = xmldoc.CreateElement("メイン")

        Dim imageElement As XmlElement = xmldoc.CreateElement("画像名")
        Dim imageText As XmlText = xmldoc.CreateTextNode(showImage)
        imageElement.AppendChild(imageText)

        Dim nenElement As XmlElement = xmldoc.CreateElement("年")
        Dim nenText As XmlText = xmldoc.CreateTextNode(myYear)
        nenElement.appendChild(nenText)

        Dim tukiElement As XmlElement = xmldoc.CreateElement("月")
        Dim tukiText As XmlText = xmldoc.CreateTextNode(selectMonth)
        tukiElement.AppendChild(tukiText)


        Dim hiElement As XmlElement = xmldoc.CreateElement("日")
        Dim hiText As XmlText = xmldoc.CreateTextNode(selectDate)
        hiElement.AppendChild(hiText)

        Dim titleElement As XmlElement = xmldoc.CreateElement("タイトル")
        Dim titleText As XmlText = xmldoc.CreateTextNode(selectTitle)
        titleElement.AppendChild(titleText)

        With infoElement
            .AppendChild(imageElement)
            .AppendChild(nenElement)
            .AppendChild(tukiElement)
            .AppendChild(hiElement)
            .AppendChild(titleElement)
        End With

        root.AppendChild(infoElement)
        xmldoc.Save("\Program Files\Rainman_calendar\data\calendarInfo.xml")
        xmldoc = Nothing
        readImage = String.Empty
        readTitle = String.Empty

        MsgBox(myYear & "/" & selectMonth & "/" & selectDate & "に" & selectTitle & "の予定を記録しました。")
    End Sub

    '右カーソルボタン
    Sub NextButton()
        ImageNo = ImageNo + imageCount
        Panel1.Controls.Clear()
        PageNo = (ImageNo + imageCount) / imageCount
        allNo = 0
        ImageShowGo()

    End Sub


    '左カーソルボタン
    Sub PreviousButton()
        If ImageNo = 0 Then
            ImageNo = imageArray.Count - imageCount
        Else
            ImageNo = ImageNo - imageCount
        End If

        Panel1.Controls.Clear()
        pageNo = (ImageNo / imageCount) + 1
        allNo = 0
        ImageShowGo()

    End Sub
    'メニュー画面に戻る
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        nenTukiCheck = 0
        Dim myForm1 As Form1 = New Form1
        myForm1.Show()
    End Sub

    '左右カーソルボタンのイベント発生
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode
            Case Keys.Right
                NextButton()
            Case Keys.Left
                PreviousButton()
            Case Else
                Exit Sub
        End Select
    End Sub

    'アイコン選択画面を閉じる
    Sub form1Close()
        Dim myForm1 As Form1 = New Form1
        myForm1.Close()
        myForm1 = Nothing
    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
        Me.Dispose()
    End Sub

    'ボタンによる前移動
    Private Sub Panel2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel2.Click
        PreviousButton()
    End Sub

    'ボタンによる次移動
    Private Sub Panel3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel3.Click
        NextButton()
    End Sub


    Private Sub Panel2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown
        Select Case e.Button
            Case MouseButtons.Left
                Panel2.BackColor = Color.Gray
        End Select
    End Sub

    Private Sub Panel2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseUp
        Select Case e.Button
            Case MouseButtons.Left
                Panel2.BackColor = Color.Gainsboro
        End Select
    End Sub

    Private Sub Panel3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown
        Select Case e.Button
            Case MouseButtons.Left
                Panel3.BackColor = Color.Gray
        End Select
    End Sub

    Private Sub Panel3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseUp
        Select Case e.Button
            Case MouseButtons.Left
                Panel3.BackColor = Color.Gainsboro
        End Select
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        nenTukiCheck = 2525
        nen = myYear
        tuki = myMonth
        Dim myForm1 As Form1 = New Form1
        myForm1.Show()
    End Sub
End Class

'Public Class CardSelectSound
'    Inherits SoundHelpers
'    Sub Owari_Play(ByVal strFileName As String)
'        PlaySound(strFileName, IntPtr.Zero, SoundHelpers.PlaySoundFlags.SND_FILENAME Or SoundHelpers.PlaySoundFlags.SND_ASYNC)
'    End Sub

'End Class



