
5th October 2010, 01:18 AM
(Last edited by lyriquidperfection; 5th October 2010 at 01:22 AM.)
|
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 1847
Posts: 2,088
Join Date: Jun 2009
Location: Nottingham
DONATE TO ME
|
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
------------------------------------------------------------------------------------ If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter! 
|

5th October 2010, 11:27 PM
|
XDA News Writer / Forum Moderator
Thanks Meter 2489

Posts: 21,665
Join Date: Feb 2007
Location: T r a v e l i n g Likes: HTC & XDA Dislikes: apples...
|
Thanks, this will be very useful
orb
HTC Butterfly - HTC One not coming... - HTC Universal Tomal WM 6.5
XDA is about developing and is for developers. Any user that recognises that will gain the most benefit from this site
|

6th October 2010, 01:24 AM
|
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 1847
Posts: 2,088
Join Date: Jun 2009
Location: Nottingham
DONATE TO ME
|
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!
My Applications
------------------------------------------------------------------------------------ If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter! 
|

8th October 2010, 08:07 AM
|
XDA News Writer / Forum Moderator
Thanks Meter 2489

Posts: 21,665
Join Date: Feb 2007
Location: T r a v e l i n g Likes: HTC & XDA Dislikes: apples...
|
Great to know!, looking forward for your release
orb
HTC Butterfly - HTC One not coming... - HTC Universal Tomal WM 6.5
XDA is about developing and is for developers. Any user that recognises that will gain the most benefit from this site
|

9th October 2010, 12:31 AM
|
Senior Member
Thanks Meter 2
Posts: 308
Join Date: Mar 2007
|
Can you add
wifi on/off/toggle
bluetooth on/off/toggle
gprs on/off/toggle
|

9th October 2010, 10:29 AM
(Last edited by Corias; 9th October 2010 at 11:05 AM.)
|
Senior Member
Thanks Meter 4
Posts: 147
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
|

9th October 2010, 12:38 PM
|
Recognized Developer / Retired Forum Moderator - OP
Thanks Meter 1847
Posts: 2,088
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
------------------------------------------------------------------------------------ If you like my hard work, please buy me a coffee and a pack of nappies for my little daughter! 
|

9th October 2010, 01:08 PM
|
Senior Member
Thanks Meter 4
Posts: 147
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?
|

18th October 2010, 02:38 PM
|
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 |
|
|
|
| Display Modes |
Linear Mode
|
|
|