Developer API Tricks in VB.Net 2008

Search This thread

lyriquidperfection

Inactive Recognized Developer
Jun 14, 2009
2,571
2,996
Nottingham
Here are a few useful tricks using p/invoke that some developers may find usefull and some were hard to find on the net so I created my own Wrappers with a little research.

The following code is a Helper Class to apply the WM6.5 visual styles to some controls using .net framework. This Wrapper lets you apply the visual style to the Tab Control, Apply Background Image to ListView, Enable Double Buffering for ListView, Show Grid Lines in ListView and Enable Visual Style for ListView. To call these functions you just simply put the control in the brackets you wish to apply the visual style to. For Example: EnableListViewStyle(lvMyListview)

Code:
Public Class VisualStyleHelper

        Public Const GWL_STYLE As Integer = -16
        Public Const GW_CHILD As Int32 = 5
        Public Const LVS_EX_THEME As Integer = &H2000000
        Public Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = &H1000 + 54
        Public Const LVM_GETEXTENDEDLISTVIEWSTYLE As Integer = &H1000 + 55
        Public Const LVS_EX_GRIDLINES As Integer = &H1
        Public Const LVS_EX_DOUBLEBUFFER As Integer = &H10000
        Public Const LVBKIF_SOURCE_HBITMAP As Integer = &H1
        Public Const LVM_SETBKIMAGE As Integer = (&H1000 + 138)

        Private Declare Function SendMessage Lib "coredll" (ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As UInt32, ByVal lParam As Integer) As IntPtr
        Private Declare Function SendMessage Lib "coredll" (ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As UInt32, ByRef img As LVBKIMAGE) As IntPtr

        Public Structure LVBKIMAGE
            Public ulFlags As Integer
            Public hbm As IntPtr
            Public pszImage As IntPtr
            Public cchImageMax As Integer
            Public xOffsetPercent As Integer
            Public yOffsetPercent As Integer
        End Structure

        'Tab Control Helper
        Public Shared Sub EnableTabStyle(ByVal tabControl As TabControl)
            Dim hNativeTab As IntPtr = WindowHelper.GetWindow(tabControl.Handle, GW_CHILD)
            Dim Style As Integer = WindowHelper.GetWindowLong(hNativeTab, GWL_STYLE)

            Style = WindowHelper.SetWindowLong(hNativeTab, GWL_STYLE, Style Or &H4000)
        End Sub

        'ListView Control Helper
        Public Shared Sub EnableListViewStyle(ByVal listView As ListView)
            Dim currentStyle As Integer = SendMessage(listView.Handle, CUInt(LVM_GETEXTENDEDLISTVIEWSTYLE), 0, 0)

            SendMessage(listView.Handle, CUInt(LVM_SETEXTENDEDLISTVIEWSTYLE), 0, currentStyle Or LVS_EX_THEME)
        End Sub

        'Set Background Image to ListView
        Public Shared Sub SetBackgroundImage(ByVal listView As ListView, ByVal path As String)
            Dim bitmap As New Bitmap(path)
            Dim hBitmap As IntPtr = bitmap.GetHbitmap()

            bitmap.Dispose()

            Dim lvImage As New LVBKIMAGE()
            lvImage.hbm = hBitmap
            lvImage.ulFlags = LVBKIF_SOURCE_HBITMAP

            SendMessage(listView.Handle, LVM_SETBKIMAGE, 0, lvImage)
        End Sub

        'Display GridLines in ListView
        Public Shared Sub ShowGridLines(ByVal listView As ListView)
            Dim currentStyle As Integer = SendMessage(listView.Handle, CUInt(LVM_GETEXTENDEDLISTVIEWSTYLE), 0, 0)

            SendMessage(listView.Handle, CUInt(LVM_SETEXTENDEDLISTVIEWSTYLE), 0, currentStyle Or LVS_EX_GRIDLINES)
        End Sub

        'Reduces Flicker in ListView
        Public Shared Sub EnableDoubleBuffering(ByVal listView As ListView)
            Dim currentStyle As Integer = SendMessage(listView.Handle, CUInt(LVM_GETEXTENDEDLISTVIEWSTYLE), 0, 0)

            SendMessage(listView.Handle, CUInt(LVM_SETEXTENDEDLISTVIEWSTYLE), 0, currentStyle Or LVS_EX_DOUBLEBUFFER)
        End Sub
    End Class


Here is a simple function to return the given bytes in Megabytes as a string:

Code:
Public Shared Function CBytesToMBytes(ByVal Bytes As Double) As String
            Dim dblAns As Double
            dblAns = (Bytes / 1024) / 1024
            Return Format(dblAns, "###,###,##0.00 MB")
End Function


Here is a Class Wrapper I created which lets you use various methods to Soft Reset and even use HTCUtil.dll to Hard Reset your device:

Code:
Public Class DeviceBootHelperClass

        Const EWX_REBOOT = 2

        <DllImport("HtcUtil")> _
        Public Shared Sub HTCUtilTurnOnFlightMode()
        End Sub

        <DllImport("HtcUtil")> _
        Public Shared Sub HTCUtilSetClearStorageFlag()
        End Sub

        <DllImport("HtcUtil")> _
        Public Shared Sub HTCUtilPowerOffReset()
        End Sub

        <DllImport("HtcUtil")> _
        Public Shared Sub HTCUtilDeviceOff()
        End Sub

        <DllImport("HtcUtil")> _
        Public Shared Sub HTCUtilEnterBootloaderMode()
        End Sub

        <DllImport("aygshell")> _
        Private Shared Function ExitWindowsEx(ByVal dwFlags As Integer, ByVal dwReserved As Integer) As Boolean
        End Function

        <DllImport("coredll")> _
        Private Shared Function KernelIoControl(ByVal dwIoControlCode As Integer, _
        ByVal lpInBuf As IntPtr, ByVal nInBufSize As Integer, ByVal lpOutBuf As IntPtr, _
        ByVal nOutBufSize As Integer, ByRef lpBytesReturned As Integer) As Integer
        End Function

        Public Shared Sub KernelIOControlSoftResetMethod()
            Const IOCTL_HAL_REBOOT As Integer = &H101003C
            Dim bytesReturned As Integer = 0
            KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned)
        End Sub

        Public Shared Sub HTCUtilSoftResetMethod()
            Try
                HTCUtilPowerOffReset()
            Catch ex As Exception
                
            End Try
        End Sub

        Public Shared Sub HTCUtilPowerOffMethod()
            Try
            HTCUtilDeviceOff()

            Catch ex As Exception
               
            End Try
        End Sub

        Public Shared Sub HTCUtilHardResetMethod()
            Try
                    HTCUtilTurnOnFlightMode()
                    HTCUtilSetClearStorageFlag()
                    HTCUtilPowerOffReset()
                End If
               
            Catch ex As Exception
                
            End Try
        End Sub

        Public Shared Sub ExitWindowsExSoftResetMethod()
            Try
                ExitWindowsEx(EWX_REBOOT, 0)
            Catch ex As Exception
               
            End Try
        End Sub

        Public Shared Sub EnterBootloaderMode()
            Try
                HTCUtilEnterBootloaderMode()
            Catch ex As Exception
                
            End Try
        End Sub
    End Class


If you like my work, then please feel free to buy me a beer! :)
 
Last edited:

v3patel

Senior Member
Mar 1, 2007
312
2
Can you add
wifi on/off/toggle
bluetooth on/off/toggle
gprs on/off/toggle
 

Corias

Senior Member
VisualStyleHelper needs WindowHelper class, which is not included. :( Google search for WindowHelper shows C# class only, no VB code.
Here it is:
Code:
    Public Class WindowHelper


        Private Const GW_HWNDFIRST As UInteger = 0
        Private Const GW_HWNDLAST As UInteger = 1
        Private Const GW_HWNDNEXT As UInteger = 2
        Private Const GW_HWNDPREV As UInteger = 3
        Private Const GW_OWNER As UInteger = 4
        Private Const GW_CHILD As UInteger = 5

        <DllImport("coredll.dll")> _
        Public Shared Function GetWindow(ByVal hWnd As IntPtr, ByVal uCmd As UInteger) As IntPtr
        End Function

        <DllImport("coredll.dll", SetLastError:=True)> _
        Public Shared Function GetWindowText(ByVal hWnd As IntPtr, <Out()> ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
        End Function

        <DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Public Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
        End Function

        <DllImport("coredll.dll")> _
        Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
        End Function

        <DllImport("coredll.dll")> _
        Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal cmd As Integer) As Integer
        End Function

        <DllImport("coredll.dll")> _
        Public Shared Function GetActiveWindow() As IntPtr
        End Function

        <DllImport("coredll.dll")> _
        Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal newWndProc As IntPtr) As Integer
        End Function


    End Class
 
Last edited:

lyriquidperfection

Inactive Recognized Developer
Jun 14, 2009
2,571
2,996
Nottingham
@Corias

Lol, how could I have missed including the Window Helper Class! I will prepare some files and include all my classes in them so it will give everyone better understanding.

BR

Gaz