Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Windows.Forms

Namespace Win32

    ''' <summary>
    ''' Class object used to retrieve Windows CE native drawing methods.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class NativeDrawing

#Region "P/invokes"

        <DllImport("Coredll.dll", EntryPoint:="DrawFocusRect")> _
        Private Shared Function DrawFocusRect(ByVal hDC As IntPtr, ByRef lprc As NATIVE_RECT) As Boolean
        End Function

#End Region

        ''' <summary>
        ''' Draws the focus rectangle from the specified Graphics handle 
        ''' at the designated Rectangle location by invoking the
        ''' DrawFocusRect platform function.
        ''' </summary>
        ''' <param name="gx">Graphics object from which to derive the context handle.</param>
        ''' <param name="rect">Rectangle object on which to draw the focus rectangle.</param>
        ''' <returns>True if draw focus rectangle is successful; otherwise, false.</returns>
        ''' <remarks></remarks>
        Public Function DrawFocusRectangle(ByVal gx As Graphics, ByVal rect As Rectangle) As Boolean
            Dim nativeRect As NATIVE_RECT
            nativeRect.left = rect.Left
            nativeRect.top = rect.Top
            nativeRect.right = rect.Right
            nativeRect.bottom = rect.Bottom

            ' Declare hdc handle
            Dim hdc_Handle As IntPtr
            Try
                ' Get HDC
                hdc_Handle = gx.GetHdc

                ' Invoke and return
                Return DrawFocusRect(hdc_Handle, nativeRect)
            Catch ex As Exception
                MessageBox.Show("Exception when drawing focus rectangle")
            Finally
                ' Clean up
                gx.ReleaseHdc(hdc_Handle)
            End Try
        End Function

    End Class

    ''' <summary>
    ''' Wrapper for a native CE RECT structure.  Defines the coordinates 
    ''' of the upper-left and lower-right corners of a rectangle.
    ''' </summary>
    ''' <remarks></remarks>
    Public Structure NATIVE_RECT

        '<FieldOffset(0)> _
        Public left As Integer

        '<FieldOffset(4)> _
        Public top As Integer

        '<FieldOffset(8)> _
        Public right As Integer

        '<FieldOffset(12)> _
        Public bottom As Integer

    End Structure

End Namespace