Imports System.Xml
Imports System.IO

Public Class Form1
    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 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
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label1 = New System.Windows.Forms.Label
        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
        '
        '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
        '
        '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 = ">>"
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(240, 320)
        Me.Controls.Add(Me.Panel3)
        Me.Controls.Add(Me.Panel2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Panel1)
        Me.Text = "絵ｶｰﾄﾞ選択画面"

    End Sub

#End Region
    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 picNo As String
    Dim maxImage As Integer = 114
    Dim selectImageName As String

    Dim mImage As Image

    'Const card_selectWav As String = "\Program Files\RainmanSound\select.wav"

    '114枚の画像を左右ボタンでページングする
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '上部のメニューを消す
        Me.WindowState = FormWindowState.Maximized

        imageNo = 0

        '実機のフォルダーにおいたXMLファイルを読む
        Dim xReader As XmlTextReader = New XmlTextReader("\Program Files\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()
        PageNo = (ImageNo / imageCount) + 1
        ImageShowGo()
    End Sub

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

        no = 0

        If ImageNo < 0 Then Exit Sub

      
        If ImageNo >= imageArray.Count - imageCount + 1 Then
            beginNo = 0
            lastNo = imageCount - 1
            ImageNo = 0
            PageNo = 1
        End If

        Dim myxReader As XmlTextReader
        If File.Exists("\Program Files\data\rainmanInfo.xml") = True And topSelectCheck = 3434 Then
            allNo = 1
            myxReader = New XmlTextReader("\Program Files\data\rainmanInfo.xml")
            While myxReader.Read
                If myxReader.IsStartElement("画像名") = True Then
                    readImage = myxReader.ReadElementString
                End If

                If myxReader.IsStartElement("タイトル") = True Then
                    readTitle = myxReader.ReadElementString
                End If

                If myxReader.IsStartElement("ページ") = True Then
                    selectedImageNo = Integer.Parse(myxReader.ReadElementString)
                End If

                If myxReader.IsStartElement("開始") = True Then
                    begin = Integer.Parse(myxReader.ReadElementString)
                    beginNo = begin
                End If

                If myxReader.IsStartElement("最終") = True Then
                    last = Integer.Parse(myxReader.ReadElementString)
                    lastNo = last
                End If
            End While
            myxReader.Close()
        Else
            beginNo = ((PageNo) * imageCount) - imageCount
            lastNo = ((PageNo) * imageCount) - 1
        End If

        If allNo = 1 Then ImageNo = selectedImageNo
        PageNo = (ImageNo / imageCount) + 1

        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\image70\" & imageArray((no - 1) + ImageNo).ToString)
            selectedImageNo = ImageNo

            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
        allNo = 0
        topSelectCheck = 0
        Label1.Text = PageNo & "/" & pageCount

    End Sub

    '画像をクリックしたときの処理
    Private Sub Picture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim picName As PictureBox = CType(sender, PictureBox)
        picNo = picName.Text
        Dim selectImageNo As Integer = Integer.Parse(picNo)

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

        '前回の解説ではダイアログボックスを表示させていた
        'MsgBox("画像ファイル名=" & showImage & vbCrLf & "タイトル名は=" & selectTitle)

        Dim xWriter As XmlTextWriter = Nothing
        xWriter = New XmlTextWriter("\Program Files\data\rainmanInfo.xml", System.Text.Encoding.GetEncoding("Shift_JIS"))
        With xWriter
            .Formatting = Formatting.Indented
            .Indentation = 3
            .WriteProcessingInstruction("xml", "version='1.0' encoding='Shift_JIS'")
            .WriteStartElement("全体")
            .WriteStartElement("情報")

            .WriteStartElement("画像名")
            .WriteString(showImage)
            .WriteEndElement()

            .WriteStartElement("タイトル")
            .WriteString(selectTitle)
            .WriteEndElement()

            .WriteStartElement("ページ")
            .WriteString(selectedImageNo)
            .WriteEndElement()

            .WriteStartElement("開始")
            .WriteString(beginNo)
            .WriteEndElement()

            .WriteStartElement("最終")
            .WriteString(lastNo)
            .WriteEndElement()


            .WriteEndElement()
            .WriteEndElement()

            .Flush()
            .Close()
        End With

        '選択した画像ファイルとタイトルをForm2に渡す．
        '変数をモジュールで宣言
        Dim myForm2 As Form2 = New Form2
        myForm2.Show()
    End Sub

    '右カーソルボタン
    Sub NextButton()
        ImageNo = ImageNo + imageCount
        Panel1.Controls.Clear()
        PageNo = (ImageNo + imageCount) / imageCount
        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
        ImageShowGo()

    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


    'ボタンによる前移動
    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
End Class






