5,600,335 Members 34,979 Now Online
XDA Developers Android and Mobile Development Forum

Developer API Tricks in VB.Net 2008

Tip us?
 
lyriquidperfection
Old
(Last edited by lyriquidperfection; 5th October 2010 at 01:22 AM.)
#1  
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 2330
Posts: 2,304
Join Date: Jun 2009
Location: Nottingham

 
DONATE TO ME
Arrow Developer API Tricks in VB.Net 2008

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!
My Applications

EFS Professional v2.1.8 (With Qualcomm NV Tools) - For Samsung Android Devices

LG CSMG Tool v1.0.21 - For LG Mobile Server Queries and Firmware Checking

PIT Magic v1.3.10 - Samsung PIT Creator, Editor and Analyzer

Odin ROM Package Builder v1.3 - Complete Pre-Root and Repack Utilities for Linux

Samsung Quick Root v1.7.10 And HTC Quick Root v1.1.13 - All In One Root / Unroot Solutions
------------------------------------------------------------------------------------
If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter!

 
orb3000
Old
#2  
orb3000's Avatar
XDA News Writer / Senior Moderator
Thanks Meter 3010
Posts: 22,272
Join Date: Feb 2007
Location: T r a v e l i n g Likes: HTC & XDA Dislikes: apples...

 
DONATE TO ME
Thanks, this will be very useful
orb


HTC One Max - HTC Butterfly S - HTC Universal

XDA is about developing and is for developers. Any user that recognises that will gain the most benefit from this site


 
This is and always will be a site for developers, pure and simple. Without them we are nothing, without them there would be no reason for XDA Developers to exist; we should never ever forget that. Without them this place would not be called XDA-Developers but something else, e.g Mobile Phone User Support Services For Ungrateful Nerds.

Previous devices: HTC: Treo 650 - Excalibur - Diamond - Diamond 2 - Maple - HD2 - Rhodium - Desire Z - Desire HD - Vivid - Sensation - One V - One X - Titan II - One X+ - DNA - Butterfly
 
lyriquidperfection
Old
#3  
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 2330
Posts: 2,304
Join Date: Jun 2009
Location: Nottingham

 
DONATE TO ME
Quote:
Originally Posted by orb3000 View Post
Thanks, this will be very useful
Hi, I have also wrote my own Window API wrapper which allows for enabling and disabling windows, getting the full exe file path from a given window class, a simple class for restarting an application and more will be uploaded soon. All this is used in my Touch Tools 2.0 Suite!
My Applications

EFS Professional v2.1.8 (With Qualcomm NV Tools) - For Samsung Android Devices

LG CSMG Tool v1.0.21 - For LG Mobile Server Queries and Firmware Checking

PIT Magic v1.3.10 - Samsung PIT Creator, Editor and Analyzer

Odin ROM Package Builder v1.3 - Complete Pre-Root and Repack Utilities for Linux

Samsung Quick Root v1.7.10 And HTC Quick Root v1.1.13 - All In One Root / Unroot Solutions
------------------------------------------------------------------------------------
If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter!

 
orb3000
Old
#4  
orb3000's Avatar
XDA News Writer / Senior Moderator
Thanks Meter 3010
Posts: 22,272
Join Date: Feb 2007
Location: T r a v e l i n g Likes: HTC & XDA Dislikes: apples...

 
DONATE TO ME
Great to know!, looking forward for your release
orb


HTC One Max - HTC Butterfly S - HTC Universal

XDA is about developing and is for developers. Any user that recognises that will gain the most benefit from this site


 
This is and always will be a site for developers, pure and simple. Without them we are nothing, without them there would be no reason for XDA Developers to exist; we should never ever forget that. Without them this place would not be called XDA-Developers but something else, e.g Mobile Phone User Support Services For Ungrateful Nerds.

Previous devices: HTC: Treo 650 - Excalibur - Diamond - Diamond 2 - Maple - HD2 - Rhodium - Desire Z - Desire HD - Vivid - Sensation - One V - One X - Titan II - One X+ - DNA - Butterfly
 
v3patel
Old
#5  
v3patel's Avatar
Senior Member
Thanks Meter 2
Posts: 311
Join Date: Mar 2007
Can you add
wifi on/off/toggle
bluetooth on/off/toggle
gprs on/off/toggle
 
Corias
Old
(Last edited by Corias; 9th October 2010 at 11:05 AM.)
#6  
Corias's Avatar
Senior Member
Thanks Meter 8
Posts: 161
Join Date: May 2009
Location: Moscow
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
 
lyriquidperfection
Old
#7  
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 2330
Posts: 2,304
Join Date: Jun 2009
Location: Nottingham

 
DONATE TO ME
@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
My Applications

EFS Professional v2.1.8 (With Qualcomm NV Tools) - For Samsung Android Devices

LG CSMG Tool v1.0.21 - For LG Mobile Server Queries and Firmware Checking

PIT Magic v1.3.10 - Samsung PIT Creator, Editor and Analyzer

Odin ROM Package Builder v1.3 - Complete Pre-Root and Repack Utilities for Linux

Samsung Quick Root v1.7.10 And HTC Quick Root v1.1.13 - All In One Root / Unroot Solutions
------------------------------------------------------------------------------------
If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter!

 
Corias
Old
#8  
Corias's Avatar
Senior Member
Thanks Meter 8
Posts: 161
Join Date: May 2009
Location: Moscow
Good. I tried to convert VC# Window helper, everything works except EnumChildWindows (VB reports missing type Window). Any ideas on it?
 
mumtaz64
Old
#9  
mumtaz64's Avatar
Member
Thanks Meter 1
Posts: 58
Join Date: Jul 2009
Location: Constantine
Awesome work, will try it when i finish the main skeleton of my little program. Will post soon.

... and thank you

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


TRENDING IN THEMER...