ChangeDeskMode


Description:
This bit of a hack changes the mode of the desktop's ListView. It doesn't change the mode permanently, and depending on the OS, not all modes may produce the desired results.
 
Code:
Option Explicit

Public Enum enumViewModes
    viewIcon
    viewReport
    viewSmallIcon
    viewList
End Enum

Private Type StyleBits
    dwOld As Long
    dwNew As Long
End Type

Private Declare Function GetWindowLong Lib "user32" Alias _
   "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
   
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
   (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
   lParam As Any) As Long
   
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
   
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
   ByVal wCmd As Long) As Long

Private Const WM_STYLECHANGED = &H7D
Private Const GW_CHILD = 5
Private Const LVS_TYPEMASK = &H3
Private Const GWL_STYLE = (-16)

Public Sub ChangeDeskMode(viewNewMode As enumViewModes)

    Dim hWnd As Long
    Dim bitsStyle As StyleBits

    hWnd = FindWindow("Progman", "Program Manager")
    hWnd = GetWindow(hWnd, GW_CHILD)
    hWnd = GetWindow(hWnd, GW_CHILD)

    bitsStyle.dwOld = GetWindowLong(hWnd, GWL_STYLE)
    bitsStyle.dwNew = bitsStyle.dwOld
    bitsStyle.dwNew = bitsStyle.dwNew And Not LVS_TYPEMASK
    bitsStyle.dwNew = bitsStyle.dwNew Or viewNewMode

    SendMessage hWnd, WM_STYLECHANGED, GWL_STYLE, bitsStyle

End Sub
 
Sample Usage:
 
ChangeDeskMode viewSmallIcon