| ' (続き) 2/4
'派生クラスの一例(Explorer関連の処理を記述)
Public Class SnakeExplorer
Inherits ContentSnakeBase
Private fileNumber As Integer = 0
Private FileList As List(Of FileSystemInfo)
Private directoryDepth As Integer = 0
Private Sub initializeFileList(ByVal path As String)
If FileList Is Nothing Then
FileList = New List(Of FileSystemInfo)
Else
FileList.Clear()
End If
Dim ssd() As String = Directory.GetDirectories(path)
Dim ssf() As String = Directory.GetFiles(path)
For Each ss As String In ssd
FileList.Add(New DirectoryInfo(ss))
Next
For Each ss As String In ssf
FileList.Add(New FileInfo(ss))
Next
fileNumber = 0
End Sub
Protected Overrides Function OnCreateNewContent(ByVal cx As Integer, ByVal cy As Integer, _
ByVal dr As Direction) As System.Windows.Forms.Control
'cx、cyはContent(ここではControlと同義)を格子に見立てたときの座標です。
'フォーム起動時に最初に表示されるContentの(cx,cy)は(100,100)、
'その右隣のContentは(101,100)、下隣のContentは(100,101)です。
'ここで(100,100)の「100」は、基本クラスのInitialCX、InitialCYで定義されています。
'
'drは、Contentの展開方向です(例えば、下に展開した場合はDirection.Down)
'
'Contentを表示する場合は、Controlインスタンスを返します。
'Contentを作成しない場合はNothingを返してください。
'各Contentのサイズが同一でない場合も多分動作しますが、画面表示が変になる場合があります。
'(例えば、Contentが重なって表示される、Formの端でContentの一部が描画されない等)
'
If Me.FileList Is Nothing Then
' ↓StartupInformationがNothingになることはない
Dim path As String = Me.StartupInformation.ToString
If Directory.Exists(path) = False Then
Throw New DirectoryNotFoundException("ディレクトリ " + path + " が見つかりません。")
End If
initializeFileList(path)
End If
If FileList.Count < fileNumber + 1 Then
'表示するファイルがない
Return Nothing
End If
Dim ofe As New OneFileExplorer(FileList.Item(fileNumber), directoryDepth)
fileNumber += 1
AddHandler ofe.DoubleClick, AddressOf Me.OneFileExplorer_DoubleClick
Return ofe
End Function
Protected Overrides Sub OnClosingContent(ByVal cw As ContentWrapper)
'Content消去前にイベントハンドラを削除
Dim ofe As OneFileExplorer = DirectCast(cw.Content, OneFileExplorer)
RemoveHandler ofe.DoubleClick, AddressOf Me.OneFileExplorer_DoubleClick
If fileNumber >= 1 Then fileNumber -= 1
End Sub
Protected Overrides Sub OnHotContentChanging(ByVal cw As ContentSnakeBase.ContentWrapper)
'今までHotContentだったContentの背景色を元に戻す
Dim ofe As OneFileExplorer = DirectCast(cw.Content, OneFileExplorer)
If ofe.BackColor = ofe.colorHot Then
ofe.BackColor = ofe.colorDefault
End If
End Sub
Protected Overrides Sub OnHotContentChanged(ByVal cw As ContentSnakeBase.ContentWrapper)
'HotContentの背景色を変更
Dim ofe As OneFileExplorer = DirectCast(cw.Content, OneFileExplorer)
If ofe.BackColor = ofe.colorDefault Then
ofe.BackColor = ofe.colorHot
End If
End Sub
Protected Overrides Sub OnPaintContent(ByVal cw As ContentSnakeBase.ContentWrapper, _
ByVal e As System.Windows.Forms.PaintEventArgs)
'
'引数cwには、Contentの連接方向や座標データが格納されているので、
'これらの情報を用いて描画したい場合にはここに処理を書いてください。
'
End Sub
Private Sub OneFileExplorer_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim mc As OneFileExplorer = DirectCast(sender, OneFileExplorer)
Dim ty As Type = mc.FSI.GetType
Dim ss As String = mc.FSI.FullName
If ty Is GetType(DirectoryInfo) Then
'フォルダをダブルクリック
Me.TruncateSnake(mc)
mc.BackColor = mc.colorDirectory
initializeFileList(ss)
Me.directoryDepth = mc.directoryDepth + 1
ElseIf ty Is GetType(FileInfo) Then
'ファイルをダブルクリック
Process.Start(ss)
Me.Close()
End If
End Sub
'「Snakeのパーツ」を定義したクラス
Private Class OneFileExplorer
Inherits System.Windows.Forms.Control
'↓ディレクトリ階層の深さに応じてBackColorが変化します。
Private Allcolor() As Color = {Color.Azure, Color.AntiqueWhite, Color.LemonChiffon, Color.Wheat}
'↓環境に応じて適宜変更してください。Controlのサイズはこれに連動して決定されます。
Private IconSize As New Size(40, 40)
Public colorDefault As Color = Color.White
Public colorDirectory As Color = Color.LightSalmon
Public colorHot As Color = Color.Aqua
Public ReadOnly FSI As FileSystemInfo
Public ReadOnly IconImage As Icon
Public directoryDepth As Integer = 0
Private m_iconRectangle As Rectangle
Private m_textRectangle As Rectangle
Private m_strFormat As New StringFormat
Public Sub New(ByVal fsi As FileSystemInfo, ByVal directorydepth As Integer)
Me.FSI = fsi
Me.directoryDepth = directorydepth
Dim i As Integer = directorydepth Mod Allcolor.Length
Me.colorDefault = Allcolor(i)
Me.BackColor = Me.colorDefault
Me.IconImage = Shell32.Get32x32Icon(fsi.FullName)
Dim sz As Size = Me.IconSize
Me.Size = New Size(CInt(sz.Width * 2), CInt(sz.Height * 2.3))
m_iconRectangle = New Rectangle(CInt(sz.Width * 0.5), CInt(sz.Height * 0.25), _
sz.Width, sz.Height)
m_textRectangle = New Rectangle(CInt(sz.Width * 0.1), CInt(sz.Height * 1.25) + 3, _
CInt(sz.Width * 1.8), 40)
m_strFormat.Alignment = StringAlignment.Center
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'アイコンとファイル名を直接描画
e.Graphics.DrawIcon(Me.IconImage, m_iconRectangle)
e.Graphics.DrawString(Me.FSI.Name, SystemFonts.MessageBoxFont, Brushes.Black, _
m_textRectangle, m_strFormat)
MyBase.OnPaint(e)
End Sub
End Class
End Class
' (続く) 2/4 |