Now Gesture Tweaks Replaces Google Now with Custom Action

On most modern Android devices with software navigation keys, swiping up from the … more

ToqAN Fixes Android 5.0 Notification Bug on Qualcomm Toq

The Qualcomm Toq is probably one of the lesser known smartwatches on the market … more

How to Root and Unlock the Google Nexus 6 on a Mac – XDA TV

In the past, XDA Developer TV Producer droidmodd3rx has shown you how to … more

Set Up Your MediaTek Device with Comprehensive Beginner’s Guide

As can be seen with the use of MediaTek chips in Android One devices … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Developer API Tricks in VB.Net 2008

OP lyriquidperfection

5th October 2010, 02:18 AM   |  #1  
OP Recognized Developer / Retired Forum Moderator
Flag Nottingham
Thanks Meter: 2,610
 
2,505 posts
Join Date:Joined: Jun 2009
Donate to Me
More
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 by lyriquidperfection; 5th October 2010 at 02:22 AM.
6th October 2010, 12:27 AM   |  #2  
orb3000's Avatar
XDA Portal Team / Senior Moderator
Flag T r a v e l i n g Likes: HTC & XDA Dislikes: apples...
Thanks Meter: 3,045
 
22,289 posts
Join Date:Joined: Feb 2007
Donate to Me
Thanks, this will be very useful
6th October 2010, 02:24 AM   |  #3  
OP Recognized Developer / Retired Forum Moderator
Flag Nottingham
Thanks Meter: 2,610
 
2,505 posts
Join Date:Joined: Jun 2009
Donate to Me
More
Quote:
Originally Posted by orb3000

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!
8th October 2010, 09:07 AM   |  #4  
orb3000's Avatar
XDA Portal Team / Senior Moderator
Flag T r a v e l i n g Likes: HTC & XDA Dislikes: apples...
Thanks Meter: 3,045
 
22,289 posts
Join Date:Joined: Feb 2007
Donate to Me
Great to know!, looking forward for your release
9th October 2010, 01:31 AM   |  #5  
v3patel's Avatar
Senior Member
Thanks Meter: 2
 
312 posts
Join Date:Joined: Mar 2007
Can you add
wifi on/off/toggle
bluetooth on/off/toggle
gprs on/off/toggle
9th October 2010, 11:29 AM   |  #6  
Corias's Avatar
Senior Member
Flag Moscow
Thanks Meter: 8
 
164 posts
Join Date:Joined: May 2009
More
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 by Corias; 9th October 2010 at 12:05 PM.
9th October 2010, 01:38 PM   |  #7  
OP Recognized Developer / Retired Forum Moderator
Flag Nottingham
Thanks Meter: 2,610
 
2,505 posts
Join Date:Joined: Jun 2009
Donate to Me
More
@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
9th October 2010, 02:08 PM   |  #8  
Corias's Avatar
Senior Member
Flag Moscow
Thanks Meter: 8
 
164 posts
Join Date:Joined: May 2009
More
Good. I tried to convert VC# Window helper, everything works except EnumChildWindows (VB reports missing type Window). Any ideas on it?
18th October 2010, 03:38 PM   |  #9  
mumtaz64's Avatar
Member
Flag Constantine
Thanks Meter: 1
 
58 posts
Join Date:Joined: Jul 2009
More
Awesome work, will try it when i finish the main skeleton of my little program. Will post soon.

... and thank you

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes