Refresh desktop (WIN10)

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Refresh desktop (WIN10)

Post by captain_skank »

Hi All,

One for all you windows experts : is it possible to refresh the windows desktop ?

The reason i ask is my desktop background image is stored in a location, but I replace that image ( with another of the same name ), but I need to then refresh the desktop to make it display.

So Right Click on the desktop and select refresh from the popup menu.

I found a topic on another site that say the following should do the trick, but i'm not sure how to correctly format it for RunProgram.

Code: Select all

RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
Anyone got any advice.

Thanks in advance
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Refresh desktop (WIN10)

Post by Marc56us »

captain_skank wrote: I found a topic on another site that say the following should do the trick, but i'm not sure how to correctly format it for RunProgram.

Code: Select all

RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
RunProgram( "Program", "all others parameters together", "Working dir")

Code: Select all

RunProgram("RUNDLL32.EXE", "USER32.DLL,UpdatePerUserSystemParameters 1, True", "")
The working directory is not important in this case but this parameter is necessary as soon as parameters are given.

:wink:
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Refresh desktop (WIN10)

Post by BarryG »

If the wallpaper image is a BMP image, you can set it as simply as this:

Code: Select all

SystemParametersInfo_(#SPI_SETDESKWALLPAPER,0,"D:\Image.bmp",0)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Refresh desktop (WIN10)

Post by Mijikai »

Iirc this may also work:

Code: Select all

Macro RefreshDesktop()
  InvalidateRect_(#Null,#Null,#False)
EndMacro
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Refresh desktop (WIN10) [Solved]

Post by captain_skank »

Thanks for the replies.

Mijikai - no that doesn't work

BarryG - works - but I use PNG's

Marc56us - Thanks this works but only if i put it in a small loop for some reason

Code: Select all

For x = 1 To 25
  RunProgram("RUNDLL32.EXE", "USER32.DLL,UpdatePerUserSystemParameters 1, True", "")
Next
Anyhoo thanks for the help
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Refresh desktop (WIN10)

Post by Marc56us »

captain_skank wrote:BarryG - works - but I use PNG's
Windows background use JPG.
Conversion is easy with PB

Code: Select all

UsePNGImageDecoder()
UseJPEGImageEncoder()
...
OpenImage(...
... 
SaveImage(...

PureBasic - Image

:wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Refresh desktop (WIN10)

Post by RASHAD »

Hi
Any acceptable Image (bmp,jpg,png,....) are allowed
Tested PB 5.71 x86 - Windows 10 x64 (1909)

Code: Select all

FileName.s = OpenFileRequester("Select Wallpaper image", "*.*", "All Files (*.*)|*.*", 1, 0) 
If FileName <> "" 
  SystemParametersInfo_(#SPI_SETDESKWALLPAPER, 0, FileName, #SPIF_UPDATEINIFILE | #SPIF_SENDWININICHANGE)
EndIf 
Egypt my love
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: Refresh desktop (WIN10)

Post by CELTIC88 »

try :

SHChangeNotify_($8000000,0,0,0)
interested in Cybersecurity..
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Refresh desktop (WIN10)

Post by BarryG »

RASHAD wrote:Any acceptable Image (bmp,jpg,png,....) are allowed
Good to know. Microsoft has changed it then, because it was definitely only BMPs in the past (per MSDN, and also my tests where JPGs wouldn't work).

For compatibility, I'd still recommended converting a JPG to BMP first, otherwise the wallpaper won't be set on older Windows. I don't when the change to allow non-BMP came in.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Refresh desktop (WIN10)

Post by IdeasVacuum »

Hi Rashad
SystemParametersInfo_(#SPI_SETDESKWALLPAPER, 0, FileName, #SPIF_UPDATEINIFILE | #SPIF_SENDWININICHANGE)
How to save the current wallpaper first, including preferences (how an image is made to fit the Desktop) so it can be restored accurately later?

Edit: oh, hang on - #SPI_GETDESKWALLPAPER

Code: Select all

sgOriginalPath.s
gOrgFilePath = AllocateMemory(1024)

SystemParametersInfo_(#SPI_GETDESKWALLPAPER, 1024, gOrgFilePath, 0)

sgOriginalPath = PeekS(gOrgFilePath, 1024, #PB_Unicode)

Debug sgOriginalPath

FreeMemory(gOrgFilePath)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
AZJIO
Addict
Addict
Posts: 1372
Joined: Sun May 14, 2017 1:48 am

Re: Refresh desktop (WIN10)

Post by AZJIO »

Combined to get the finished code.
Screen refresh after file replacement.

Code: Select all

Define FileName.s, *mem
*mem = AllocateMemory(1024)
SystemParametersInfo_(#SPI_GETDESKWALLPAPER, 1024, *mem, 0)
FileName = PeekS(*mem, 1024 , #PB_Unicode)
; Debug FileName
FreeMemory(*mem)
SystemParametersInfo_ (#SPI_SETDESKWALLPAPER, 0, FileName, #SPIF_UPDATEINIFILE | #SPIF_SENDWININICHANGE)
Post Reply