Imports System.Xml
Imports System.Deployment.Application

Public Class MyUt
    Shared Sub CheckSel(ByVal lv As ListView, ByVal i As Integer, ByVal ik As String)
        For Each lvi As ListViewItem In lv.Items
            lvi.ImageKey = IIf(lvi.Index = i, ik, Nothing)
        Next
        lv.Items(i).EnsureVisible()
    End Sub

    Shared Function CheckHit(ByVal s As Microsoft.VisualBasic.PowerPacks.RectangleShape, ByVal x As Integer, ByVal y As Integer) As Boolean
        Dim rc As Rectangle = s.RectangleToScreen(New Rectangle(Point.Empty, s.Size))
        CheckHit = rc.X <= x AndAlso rc.Y <= y AndAlso x < rc.Right AndAlso y < rc.Bottom
    End Function
End Class

Public Class MyPicUt
    ''' <summary>
    ''' Load picture without read-locking target file.
    ''' </summary>
    ''' <param name="fp">Picture filepath to load</param>
    ''' <returns>Image expression</returns>
    ''' <remarks></remarks>
    Shared Function LoadPicFromFile(ByVal fp As String) As Drawing.Image
        Dim si As New IO.MemoryStream(IO.File.ReadAllBytes(fp))
        LoadPicFromFile = Image.FromStream(si)
    End Function
End Class

Public Class MyXUt
    Shared Function GetElementText(ByVal el As XmlElement, ByVal nz As String) As String
        GetElementText = nz
        If Not el Is Nothing Then
            For Each o As XmlNode In el.ChildNodes
                If o.NodeType = XmlNodeType.Text Then
                    GetElementText = o.Value
                    Exit For
                End If
            Next
        End If
    End Function
End Class

Public Class MyDirUt
    Shared ReadOnly Property DataDir() As String
        Get
            If ApplicationDeployment.IsNetworkDeployed Then
                DataDir = ApplicationDeployment.CurrentDeployment.DataDirectory ' for ClickOnce
            Else
                DataDir = Application.StartupPath ' for Local
            End If
        End Get
    End Property
End Class

Public Class MyWordClrUt
    ' http://www.dungeon-master.com/forum/viewtopic.php?p=91917#91917

    ' 0     1     2     3     4     5     6     7     8     9     a     b     c     d     e     f
    ' 00 00 03 33 04 44 03 10 00 66 04 20 00 40 00 60 07 00 07 50 06 43 07 70 02 22 05 55 00 07 07 77
    ' 00 00 02 22 03 33 03 10 00 66 04 10 00 30 00 50 06 00 06 40 05 32 07 60 01 11 04 44 00 06 06 66
    ' 00 00 01 11 02 22 02 10 00 66 03 10 00 20 00 40 05 00 05 30 04 21 07 50 00 00 03 33 00 05 05 55
    ' 00 00 00 00 01 11 01 00 00 66 02 10 00 10 00 30 04 00 04 20 03 10 06 40 00 00 02 22 00 04 04 44
    ' 00 00 00 00 00 00 00 00 00 66 01 00 00 00 00 20 03 00 03 10 02 00 05 30 00 00 01 11 00 03 03 33
    ' 00 00 00 00 00 00 00 00 00 66 00 00 00 00 00 10 02 00 02 00 01 00 03 20 00 00 00 00 00 02 02 22

    Public Shared Function WordClrTo1Color(ByVal p As Short) As Drawing.Color
        Dim b1 As Byte = CByte((p >> 8) And 255) ' hi byte (0/R)
        Dim b2 As Byte = CByte((p >> 0) And 255) ' lo byte (G/B)
        WordClrTo1Color = Color.FromArgb( _
            Math.Min(255, ((b1 >> 0) And 15) * 36), _
            Math.Min(255, ((b2 >> 4) And 15) * 36), _
            Math.Min(255, ((b2 >> 0) And 15) * 36))
    End Function

    Public Shared Function OneColorToWordClr(ByVal clr As Drawing.Color) As Short
        OneColorToWordClr = 0 _
            Or ((Int(clr.R / 36) And 15) << 8) _
            Or ((Int(clr.G / 36) And 15) << 4) _
            Or ((Int(clr.B / 36) And 15) << 0)
    End Function
End Class

