[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Refreshing desktop from VB application

Hi!

I have a program that changes the icon on My Computer and Recycled (by changing the register). What I am looking for is a command that refreshes the windows desktop from the VB Application.

(VB6)
0
Ecmil
Asked:
Ecmil
1 Solution
 
hesCommented:
Add this code to a bas module.

Option Explicit

Public Const RDW_ALLCHILDREN = &H80
Public Const RDW_ERASE = &H4
Public Const RDW_ERASENOW = &H200
Public Const RDW_FRAME = &H400
Public Const RDW_INTERNALPAINT = &H2
Public Const RDW_INVALIDATE = &H1
Public Const RDW_NOCHILDREN = &H40
Public Const RDW_NOERASE = &H20
Public Const RDW_NOFRAME = &H800
Public Const RDW_NOINTERNALPAINT = &H10
Public Const RDW_UPDATENOW = &H100
Public Const RDW_VALIDATE = &H8

Public Declare Function GetDesktopWindow Lib "user32" () As Long


Public Declare Function RedrawWindowAny Lib "user32" Alias "RedrawWindow" _
            (ByVal hwnd As Long, lprcUpdate As Any, _
             ByVal hrgnUpdate As Long, _
             ByVal fuRedraw As Long) As Long

Add this code to a form.

Public Sub RefreshDesktop()
    Call RedrawWindowAny(GetDesktopWindow(), vbNull, 1&, _
            RDW_INVALIDATE Or RDW_UPDATENOW Or RDW_ALLCHILDREN)
End Sub
0
 
setiawanCommented:
Hi Ecmil,

It may be necessary to request (force) Windows to update its icon information, perhaps as part of an icon-replacement program. This code shows how to do this by modifying the registry, then broadcasting the change. In response, Windows rebuilds its internal cache of icons.

The theory for this code was posted in the newsgroups in April 1998 (shows how far behind I am, eh?) by Jason M. Purcell. Jason used RegMon to watch how Microsoft's Tweak UI accomplished the feat.  There is no single API to do this .. the steps required and shown below are:

1. open HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
2. get the type of value and its size stored as "Shell Icon Size" (string)
3. get value of Shell Icon Size
4. change this value (i.e. decrement the value by one)
5. write the new value back to the registry
6. call SendMessageTimeout with HWND_BROADCAST
7. return the "Shell Icon Size" value to its original setting
8. call SendMessageTimeout with HWND_BROADCAST again
9. close the key
The result is Window's erasing all its icons, and recalculating them based on the registry settings. This means if you have changed a DefaultIcon key within the registry for some application or file, Windows will display the new icon when the refresh is completed.

Option Explicit

'Read/Write permissions
Public Const KEY_QUERY_VALUE As Long = &H1
Public Const KEY_SET_VALUE As Long = &H2
Public Const KEY_ALL_ACCESS As Long = &H3F
Public Const KEY_CREATE_SUB_KEY  As Long = &H4
Public Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Public Const KEY_NOTIFY As Long = &H10
Public Const KEY_CREATE_LINK As Long = &H20
Public Const READ_CONTROL As Long = &H20000
Public Const WRITE_DAC As Long = &H40000
Public Const WRITE_OWNER As Long = &H80000
Public Const SYNCHRONIZE As Long = &H100000
Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Public Const STANDARD_RIGHTS_READ As Long = READ_CONTROL
Public Const STANDARD_RIGHTS_WRITE As Long = READ_CONTROL
Public Const STANDARD_RIGHTS_EXECUTE As Long = READ_CONTROL
Public Const KEY_READ As Long = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Public Const KEY_WRITE As Long = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Public Const KEY_EXECUTE As Long = KEY_READ

Public Const HKEY_CLASSES_ROOT As Long = &H80000000
Public Const HKEY_CURRENT_USER As Long = &H80000001
Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
Public Const HKEY_USERS As Long = &H80000003

'Registration key types
Public Const REG_NONE As Long = 0                     'No value type
Public Const REG_SZ As Long = 1                       'null terminated string
Public Const REG_EXPAND_SZ As Long = 2                'null terminated string
Public Const REG_BINARY As Long = 3                   'Free form binary
Public Const REG_DWORD As Long = 4                    '32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN As Long = 4      '32-bit number (same as REG_DWORD)
Public Const REG_DWORD_BIG_ENDIAN As Long = 5         '32-bit number
Public Const REG_LINK As Long = 6                     'Symbolic Link (unicode)
Public Const REG_MULTI_SZ As Long = 7                 'Multiple Unicode strings
Public Const REG_RESOURCE_LIST As Long = 8            'Resource list in the resource map
Public Const REG_FULL_RESOURCE_DESCRIPTOR As Long = 9 'Resource list in the hardware description
Public Const REG_RESOURCE_REQUIREMENTS_LIST As Long = 10

'Return codes from Registration functions
Public Const ERROR_SUCCESS As Long = 0
Public Const ERROR_BADDB As Long = 1
Public Const ERROR_BADKEY As Long = 2
Public Const ERROR_CANTOPEN As Long = 3
Public Const ERROR_CANTREAD As Long = 4
Public Const ERROR_CANTWRITE As Long = 5
Public Const ERROR_OUTOFMEMORY As Long = 6
Public Const ERROR_INVALID_PARAMETER As Long = 7
Public Const ERROR_ACCESS_DENIED As Long = 8
Public Const ERROR_INVALID_PARAMETERS As Long = 87
Public Const ERROR_MORE_DATA As Long = 234
Public Const ERROR_NO_MORE_ITEMS As Long = 259

'SendMessageTimeout values
Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1A
Public Const SPI_SETNONCLIENTMETRICS As Long = 42
Public Const SMTO_ABORTIFHUNG As Long = &H2

Public Type SECURITY_ATTRIBUTES
   nLength                 As Long
   lpSecurityDescriptor    As Long
   bInheritHandle          As Long
End Type

Public Declare Function RegOpenKeyEx _
    Lib "advapi32.dll" Alias "RegOpenKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Public Declare Function RegSetValueExString _
    Lib "advapi32.dll" Alias "RegSetValueExA" _
   (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    ByVal lpValue As String, _
    ByVal cbData As Long) As Long

Public Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long

Public Declare Function SendMessageTimeout Lib "user32" _
   Alias "SendMessageTimeoutA" _
  (ByVal hwnd As Long, ByVal msg As Long, _
   ByVal wParam As Long, ByVal lParam As Long, _
   ByVal fuFlags As Long, ByVal uTimeout As Long, _
   lpdwResult As Long) As Long
   
Public Declare Function RegQueryValueEx Lib "advapi32.dll" _
   Alias "RegQueryValueExA" _
  (ByVal hKey As Long, ByVal lpszValueName As String, _
   ByVal lpdwRes As Long, lpType As Long, _
   lpData As Any, nSize As Long) As Long

Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'--end block--'
   

 Form Code
   
On a form, add a command button and the following code:

--------------------------------------------------------------------------------
 

Option Explicit

Private Sub ForceCacheRefresh()

   Dim hKey As Long
   Dim dwKeyType As Long

   Dim dwDataType As Long
   Dim dwDataSize As Long

   Dim sKeyName As String
   Dim sValue As String
   Dim sData As String
   Dim sDataRet As String

   Dim tmp As Long
   Dim sNewValue As String
   Dim dwNewValue As Long
   Dim success As Long
   
'HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\ShellIconSize
'HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\Shell Icon Size

'1. open HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
'2. get the type of value and its size stored at value "Shell Icon Size"
'3. get value of "Shell Icon Size"
'4. change this value (i.e. decrement the value by one)
'5. write it back to the registry
'6. call SendMessageTimeout HWND_BROADCAST
'7. return "Shell Icon Size" to its original setting
'8. call SendMessageTimeout HWND_BROADCAST again
'9. close the key

''''''''''''''''''''''''
'Sample Debug output
''''''''''''''''''''''''
'RegKeyOpen = 468
'RegGetStringSize = 3
'RegGetStringValue = 32
'Changing to = 31
'Changing back to = 32

  ''''''''''''''''''''''''
  '1. open key
   dwKeyType = HKEY_CURRENT_USER
   sKeyName = "Control Panel\Desktop\WindowMetrics"
   sValue = "Shell Icon Size"
   
   hKey = RegKeyOpen(HKEY_CURRENT_USER, sKeyName)
   
   If hKey <> 0 Then
   
      Debug.Print "RegKeyOpen = "; hKey
       
     ''''''''''''''''''''''''
     '2. Determine the size and type of data to be read.
     'In this case it should be a string (REG_SZ) value.
      dwDataSize = RegGetStringSize(ByVal hKey, sValue, dwDataType)
       
      Debug.Print "RegGetStringSize = "; dwDataSize
       
      If dwDataSize > 0 Then

        ''''''''''''''''''''''''
        '3. get the value for that key
         sDataRet = RegGetStringValue(hKey, sValue, dwDataSize)
         
        'if a value returned
         If sDataRet > "" Then
         
            Debug.Print "RegGetStringValue = "; sDataRet
             
           ''''''''''''''''''''''''
           '4, 5. convert sDataRet to a number and subtract 1,
           'convert back to a string, define the size
           'of the new string, and write it to the registry
            tmp = CLng(sDataRet)
            tmp = tmp - 1
            sNewValue = CStr(tmp) & Chr$(0)
            dwNewValue = Len(sNewValue)

            Debug.Print "Changing to = "; sNewValue
             
            If RegWriteStringValue(hKey, _
                                   sValue, _
                                   dwDataType, _
                                   sNewValue) = ERROR_SUCCESS Then
                                   
                                   
              ''''''''''''''''''''''''
              '6. because the registry was changed, broadcast
              'the fact passing SPI_SETNONCLIENTMETRICS,
              'with a timeout of 10000 milliseconds (10 seconds)
               Call SendMessageTimeout(HWND_BROADCAST, _
                                       WM_SETTINGCHANGE, _
                                       SPI_SETNONCLIENTMETRICS, _
                                       0&, SMTO_ABORTIFHUNG, _
                                       10000&, success)
                                       
              ''''''''''''''''''''''''
              '7. the desktop will have refreshed with the
              'new (shrunken) icon size. Now restore things
              'back to the correct settings by again writing
              'to the registry and posing another message.
               sDataRet = sDataRet & Chr$(0)
               
               Debug.Print "Changing back to = "; sDataRet
               
               Call RegWriteStringValue(hKey, _
                                       sValue, _
                                       dwDataType, _
                                       sDataRet)
                   
              ''''''''''''''''''''''''
              '8.  broadcast the change again
               Call SendMessageTimeout(HWND_BROADCAST, _
                                       WM_SETTINGCHANGE, _
                                       SPI_SETNONCLIENTMETRICS, _
                                       0&, SMTO_ABORTIFHUNG, _
                                       10000&, success)
               
             
            End If   'If RegWriteStringValue
         End If   'If sDataRet > ""
      End If   'If dwDataSize > 0
   End If   'If hKey > 0
         
   
  ''''''''''''''''''''''''
  '9. clean up
   Call RegCloseKey(hKey)

End Sub


Private Function RegGetStringSize(ByVal hKey As Long, _
                                  ByVal sValue As String,
                                  dwDataType As Long) As Long

   Dim success As Long
   Dim dwDataSize As Long
   
   success = RegQueryValueEx(hKey, _
                             sValue, _
                             0&, _
                             dwDataType, _
                             ByVal 0&, _
                             dwDataSize)
         
   If success = ERROR_SUCCESS Then
      If dwDataType = REG_SZ Then
       
         RegGetStringSize = dwDataSize
         
      End If
   End If

End Function


Private Function RegKeyOpen(dwKeyType As Long, sKeyPath As String) As Long

   Dim hKey As Long
   Dim dwOptions As Long
   Dim SA As SECURITY_ATTRIBUTES
   
   SA.nLength = Len(SA)
   SA.bInheritHandle = False
   
   dwOptions = 0&    
   If RegOpenKeyEx(dwKeyType, _
                   sKeyPath, dwOptions, _
                   KEY_ALL_ACCESS, hKey) = ERROR_SUCCESS Then
   
      RegKeyOpen = hKey
       
   End If

End Function


Private Sub Command1_Click()

   Call LockWindowUpdate(GetDesktopWindow())
   ForceCacheRefresh
   Call LockWindowUpdate(0)

End Sub


Private Function RegGetStringValue(ByVal hKey As Long,
                                   ByVal sValue As String, _
                                   dwDataSize As Long) As String

   Dim sDataRet As String
   Dim dwDataRet As Long
   Dim success As Long
   Dim pos As Long
   
  'get the value of the passed key
   sDataRet = Space$(dwDataSize)
   dwDataRet = Len(sDataRet)
   
   success = RegQueryValueEx(hKey, sValue, _
                             ByVal 0&, dwDataSize,
                             ByVal sDataRet, dwDataRet)

   If success = ERROR_SUCCESS Then
      If dwDataRet > 0 Then
       
         pos = InStr(sDataRet, Chr$(0))
         RegGetStringValue = Left$(sDataRet, pos - 1)
         
      End If
   End If
   
End Function


Public Function RegWriteStringValue(ByVal hKey, _
                                    ByVal sValue,
                                    ByVal dwDataType,
                                    sNewValue) As Long

   Dim success As Long
   Dim dwNewValue As Long
   
   dwNewValue = Len(dwNewValue)
   
   If dwNewValue > 0 Then
      RegWriteStringValue = RegSetValueExString(hKey, _
                                                sValue, _
                                                0&, _
                                                dwDataType, _
                                                sNewValue, _
                                                dwNewValue)
                                           
   End If

End Function

'--end block--'
   
code taken from : http://www.mvps.org/vbnet/ 

   regards
 
    danny
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now