Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Refresh the desktop

Posted on 1999-08-27
16
Medium Priority
?
504 Views
Last Modified: 2008-03-06
I can change the recycle bin icon in my program, but to get it to take effect, I have to type F5. I have used every bit of code I can think of, including the API for refreshing the desktop, but nothing will simulate the F5 key without physically typing it. Can anyone help in this matter?
0
Comment
Question by:microboard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +6
16 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 1534251
Try this (if you haven't already):

SendMessage HWND_BROADCAST, WM_WININICHANGE, SPI_SETICONMETRICS, 0


0
 
LVL 10

Expert Comment

by:viktornet
ID: 1534252
The following code simulates the F5 key...

...
dim c1 as long

c1=FindWindowEx(FindWindowEx(FindWindow("Progman","Program
Manager"),0,"SHELLDLL_DefView",""),0,"SysListView32","")

call PostMessage(c1,WM_KEYDOWN,VK_F5,0)
call PostMessage(c1,WM_KEYUP,VK_F5, 2^31)
...

that should do it...

..-=ViKtOr=-..
0
 

Author Comment

by:microboard
ID: 1534253
viktornet the code you gave doesnt seem to have any effect.

Erick37 the code you sent causes the screen to repaint itself but not to simulate the F5 key; that is, to update itself. This seems to be a very big problem. I'll add another 100 points on to this question then since it seems to be harder than I thought..

thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1534254
could you show me the exact code you used? and did you declare the API functions?
0
 

Expert Comment

by:tgoglia
ID: 1534255
How about trying the SendKeys command passing "{F5}" as a parameter?

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1534256
microboard, try

call PostMessage(c1,WM_KEYDOWN,VK_F5,0)
     call PostMessage(c1,WM_KEYUP,VK_F5, 0)

instead of

call PostMessage(c1,WM_KEYDOWN,VK_F5,0)
     call PostMessage(c1,WM_KEYUP,VK_F5, 2^31)

and tell me how that works..
0
 
LVL 1

Expert Comment

by:plasmatek
ID: 1534257
I am sure there is a registry setting you can modify which makes the desktop refresh itself every 10ms or so... can't quite remember where it is.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 1534258
Here is a link to a very simple program which adds shortcuts to the desktop, and then refreshes the desktop.

http://www.thescarms.com/VBasic/Shortcuts.htm

It uses the following APIs to refresh the desktop:

' Refresh the desktop to display the shortcut.
            '
            Call SHGetSpecialFolderLocationD(Me.hwnd, CSIDL_DESKTOP, lpil)
            Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)

I'm sure with a little modification, this could work.

0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1534259
Try this:|

SetForegroundWindow(GetDesktopWindow())
SendKeys("{F5}")


0
 
LVL 2

Expert Comment

by:header
ID: 1534260
I have code that will kill the explorer, then it will automatically restart itself.  There are some side effects though, all of the tray icons are emptied out (the startup folder is not re-processed).

If you want to see if this will work, press ctrl+alt+del.  Then end the 'Explorer' task.  If the shut down windows box pops up, press cancel.  Then when the 'End Task' box comes up press 'End Task'.  This will kill the explorer and it will automatically refresh itself.  If this fixed your problem let me know and I will post the code.
0
 
LVL 6

Expert Comment

by:setiawan
ID: 1534261
microboard, try this one
To Simulate F5 key

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2

keybd_event 115, 31, 0, 0                'key down
keybd_event 115, 31, KEYEVENTF_KEYUP, 0  'key up

0
 

Author Comment

by:microboard
ID: 1534262
Viktornet your new code did refresh the desktop, but again the same problem as before: nothing is being updated. You would think Windows would have made a simple API just to handle the updating of the desktop. I know they have the UpdateWindow but it doesnt do anything but refresh as well

Erick37 thanks for your tip but no good on that. The code isnt actually updateing the desktop but setting in something new, which doesnt affect anything else. But it was a good idea.

MikeP no go on the code.

setiawan, nice twist to the code you posted, but sorry to say it didnt even refresh the screen.

plasmatek, yes your right about the refreshing, but that's all it does. It doesnt update.

As for the code I'm using: when I try new code, I only have that particular code I'm testing in use. There is not anything else to hinder it.
0
 
LVL 6

Accepted Solution

by:
setiawan earned 800 total points
ID: 2056304
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
 

Author Comment

by:microboard
ID: 2056997
Thanks Setiawan. Your code did the trick.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 2057824
It's not worth the code... 2 pages of code for simple refresh... get outta here... :))
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6870966
GREETINGS!

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question