Results 1 to 8 of 8

Thread: [RESOLVED] How to force desktop refresh in Windows 11?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    35

    Resolved [RESOLVED] How to force desktop refresh in Windows 11?

    Is there a way to programmatically force a desktop refresh in Windows 11?

    Frequently when my code places a new file onto the desktop, or removes a file from the desktop, there is no visual change, unless I force a desktop refresh by manually pressing the F5 key. I’ve tried programmatically sending “F5” to the desktop via Wscript.Shell – but no success, and older methods I’ve found in searches don’t seem to work in Windows 11.

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    35

    Re: How to force desktop refresh in Windows 11?

    I've found that this code works to refresh the desktop in the special case when my desktop window happens to have hwnd_of_window=65800

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const WM_KEYDOWN = &H100

    Private Sub Command1_Click()
    hwnd_of_window=65800
    PostMessage 65800, WM_KEYDOWN, vbKeyF5, 0
    End Sub

    My remaining question is how to consistently find the hwnd_of_window of desktop window properly ...

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    35

    Re: How to force desktop refresh in Windows 11?

    This code works to refresh the desktop, but it works only when there is no application window on top of screen location 0,0:

    Code:
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xpoint As Long, ByVal ypoint As Long) As Long
    Private Const WM_KEYDOWN = &H100
    
    Private Sub RefreshDesktop()
      PostMessage WindowFromPoint(0, 0), WM_KEYDOWN, vbKeyF5, 0
    End Sub
    I'm still searching for a better way than WindowFromPoint to find the Desktop hwnd...
    Last edited by SeabrookStan; Jun 23rd, 2024 at 01:10 PM.

  4. #4

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    35

    Re: How to force desktop refresh in Windows 11?

    Thank you VanGoghGaming that code does appear to work.

    There is some subtle difference in the appearance of the flicker when the Desktop refreshes compared to PostMessage F5, I don't know if that difference really matters. Nonetheless I'll put your suggestion into my project because it seems to work, so thank you!

    I suppose it is academic now, but regarding my original code above, I still don't understand why the API GetDesktopWindow returns a value different from WindowFromPoint(0, 0), and why the former does not work with PostMessage, while WindowFromPoint does work. If anyone can explain, I'd appreciate it.

  6. #6
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,619

    Lightbulb Re: How to force desktop refresh in Windows 11?

    GetDesktopWindow returns a virtual HWND whose value is universally &H10010 on all Windows machines. This virtual HWND does not work with PostMessage, instead you can use the HWND from GetShellWindow.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    35

    Re: How to force desktop refresh in Windows 11?

    Wow! Thank you again. I'll post both solutions here in case anyone is interested in the future.


    Desktop Refresh Solution 1:
    Code:
    '  Source:'  https://www.vbforums.com/showthread.php?140936-Window-Refresh&p=790365&viewfull=1#post790365
    
    
    Private Declare Sub SHChangeNotify Lib "shell32.dll" _
      (ByVal wEventId As Long, _
      ByVal uFlags As Long, _
      dwItem1 As Any, _
      dwItem2 As Any)
    Private Const SHCNE_ASSOCCHANGED = &H8000000
    Private Const SHCNF_IDLIST = &H0&
     
    Public Sub RefreshSystemIcons()
        SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
    End Sub

    Desktop Refresh Solution 2:
    Code:
    ' SeabrookStan June 2024
    
    Private Declare Function GetShellWindow Lib "user32" () As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
      (ByVal hWnd As Long, ByVal wMsg As Long, _
      ByVal wParam As Long, _
      ByVal lParam As Long) As Long
    Private Const WM_KEYDOWN = &H100
    
    
    Public Sub RefreshDesktop_PostMessageF5()
      PostMessage GetShellWindow, WM_KEYDOWN, vbKeyF5, 0
    End Sub

    And a demo of both solutions is attached:
    Attached Files Attached Files

  8. #8
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    Re: [RESOLVED] How to force desktop refresh in Windows 11?

    I use InvalidateRect 0,0,1 with best results.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width