'//----------------------------------------------------------------------
'//【WeakReference デモ】- WeakRef
'//
'//
'//                                                       by Masato Hirai         
'//----------------------------------------------------------------------
Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading

Public Class MainForm : Inherits Form


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

    Public Sub New()
        MyBase.New()
        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 ListBox1 As System.Windows.Forms.ListBox
  Friend WithEvents RichLabel1 As GeniusHirai.ColorfulControls.RichLabel
  Friend WithEvents Button1 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents chkUseWeak As GeniusHirai.ColorfulControls.BigCheckBox
  Friend WithEvents Button2 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents Button3 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents NumericLabel1 As GeniusHirai.ColorfulControls.NumericLabel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.RichLabel1 = New GeniusHirai.ColorfulControls.RichLabel
Me.chkUseWeak = New GeniusHirai.ColorfulControls.BigCheckBox
Me.Button1 = New GeniusHirai.ColorfulControls.OvalButton
Me.Button2 = New GeniusHirai.ColorfulControls.OvalButton
Me.Button3 = New GeniusHirai.ColorfulControls.OvalButton
Me.NumericLabel1 = New GeniusHirai.ColorfulControls.NumericLabel
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.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.ListBox1.ItemHeight = 33
Me.ListBox1.Location = New System.Drawing.Point(8, 64)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(408, 400)
Me.ListBox1.TabIndex = 0
'
'RichLabel1
'
Me.RichLabel1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(128, Byte))
Me.RichLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.RichLabel1.Dock = System.Windows.Forms.DockStyle.Top
Me.RichLabel1.Font = New System.Drawing.Font("MS UI Gothic", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.RichLabel1.ForeColor = System.Drawing.Color.OrangeRed
Me.RichLabel1.Location = New System.Drawing.Point(0, 0)
Me.RichLabel1.Name = "RichLabel1"
Me.RichLabel1.Size = New System.Drawing.Size(424, 56)
Me.RichLabel1.TabIndex = 5
Me.RichLabel1.Text = "WeakReference デモ"
Me.RichLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'chkUseWeak
'
Me.chkUseWeak.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.chkUseWeak.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.chkUseWeak.Location = New System.Drawing.Point(16, 485)
Me.chkUseWeak.Name = "chkUseWeak"
Me.chkUseWeak.Size = New System.Drawing.Size(200, 32)
Me.chkUseWeak.TabIndex = 6
Me.chkUseWeak.Text = "WeakReference"
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Button1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Green
Me.Button1.Font = New System.Drawing.Font("Arial", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(16, 522)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 56)
Me.Button1.TabIndex = 7
Me.Button1.Text = "Show"
'
'Button2
'
Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button2.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Green
Me.Button2.Font = New System.Drawing.Font("Arial", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button2.Location = New System.Drawing.Point(144, 523)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(88, 56)
Me.Button2.TabIndex = 8
Me.Button2.Text = "TEST"
'
'Button3
'
Me.Button3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button3.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Blue
Me.Button3.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button3.Location = New System.Drawing.Point(352, 480)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(56, 24)
Me.Button3.TabIndex = 9
Me.Button3.Text = "GC"
'
'NumericLabel1
'
Me.NumericLabel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.NumericLabel1.BackColor = System.Drawing.Color.Black
Me.NumericLabel1.ForeColor = System.Drawing.Color.Yellow
Me.NumericLabel1.Location = New System.Drawing.Point(248, 520)
Me.NumericLabel1.Name = "NumericLabel1"
Me.NumericLabel1.Size = New System.Drawing.Size(172, 63)
Me.NumericLabel1.TabIndex = 10
Me.NumericLabel1.Text = "NumericLabel1"
Me.NumericLabel1.UpperPoint = 5
Me.NumericLabel1.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.Mini
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(15, 32)
Me.ClientSize = New System.Drawing.Size(424, 590)
Me.Controls.Add(Me.NumericLabel1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.chkUseWeak)
Me.Controls.Add(Me.RichLabel1)
Me.Controls.Add(Me.ListBox1)
Me.Font = New System.Drawing.Font("MS UI Gothic", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "MainForm"
Me.ResumeLayout(False)

    End Sub

#End Region

Private w As New WeakReference(Nothing)
Const PATH As String = "..\Images\"
'//
'// MainForm_Loadイベント
'//
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim f() As String = Directory.GetFiles(PATH)
  For k As Integer = 0 To f.Length - 1
    f(k) = f(k).Substring(f(k).LastIndexOf("\"c) + 1)
  Next
  ListBox1.Items.AddRange(f)
  ListBox1.SelectedIndex = 0
End Sub
'//
'// 単独フォームのShow
'//
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
  Button1.PerformClick()
End Sub
'//
'// 単独フォームのShow
'//
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  If chkUseWeak.Checked Then
    If Not w.IsAlive Then
      w.Target = New SubForm
    End If
    Dim f As SubForm = CType(w.Target, SubForm)
    f.PictureBox1.Image = Image.FromFile(PATH & ListBox1.SelectedItem)
    f.ShowDialog()
  Else
    Dim f As New SubForm
    f.PictureBox1.Image = Image.FromFile(PATH & ListBox1.SelectedItem)
    f.ShowDialog()
    f.Dispose()
  End If
End Sub
'//
'// [テスト]ボタン（連続でサブフォームを表示）
'//
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  Me.NumericLabel1.Value = 0
  Dim t As Integer = Environment.TickCount
  For k As Integer = 0 To ListBox1.Items.Count - 1
    ListBox1.SelectedIndex = k

    If chkUseWeak.Checked Then
      'WeakReference の場合 
      If Not w.IsAlive Then
        w.Target = New SubForm
      End If
      Dim f As SubForm = CType(w.Target, SubForm)
      f.PictureBox1.Image = Image.FromFile(PATH & ListBox1.SelectedItem)
      f.Show()
      f.Refresh()
      Application.DoEvents()
      Thread.Sleep(150)
      f.Close()
    Else
      'Show/Dispose の場合 
      Dim f As New SubForm
      f.PictureBox1.Image = Image.FromFile(PATH & ListBox1.SelectedItem)
      f.Show()
      f.Refresh()
      Application.DoEvents()
      Thread.Sleep(150)
      f.Close()
      f.Dispose()
    End If

    Application.DoEvents()
  Next
  Me.NumericLabel1.Value = Environment.TickCount - t
End Sub
'//
'// 強制ガベージコレクション
'//
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  GC.Collect(2)
End Sub

End Class
