Results 1 to 11 of 11

Thread: Change Wallpaper (Resolved)

  1. #1

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650

    Change Wallpaper (Resolved)

    I'm using this code to choose a random picture from a folder that I have selected and then change the wallpaper to it. The problem is that the wallpaper doesn't actually change. What have I done wrong?
    VB Code:
    1. Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
    2. Sub Main()
    3. Dim pic() As String
    4. Dim file As String
    5. Dim path As String
    6. Dim count As Integer
    7. Dim i As Integer
    8. Randomize Timer
    9. path = "C:\Windows\Desktop\Graphics\Desktops\"
    10. file = Dir(path)
    11. Do While file <> ""
    12. If file <> "." And file <> ".." Then
    13. ReDim Preserve pic(count)
    14. pic(count) = file
    15. count = count + 1
    16. End If
    17. file = Dir
    18. Loop
    19. i = Round(Rnd * count)
    20. SystemParametersInfo 20, 0, path & pic(i), &H1
    21. End Sub
    Last edited by Mushroom Realm; Jun 13th, 2003 at 04:22 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I don't know if this is any help but anyway :

    VB Code:
    1. 'This project needs
    2. '- a common dialog box
    3. '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    4. '   and select Microsoft Common Dialog control)
    5. '- a button
    6. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
    7. Const SPI_SETDESKWALLPAPER = 20
    8. Const SPIF_UPDATEINIFILE = &H1
    9. Private Sub Command1_Click()
    10.     'KPD-Team 1998
    11.     'URL: [url]http://www.allapi.net/[/url]
    12.     'E-Mail: [email][email protected][/email]
    13.     'Set the commondialogbox' title
    14.     CDBox.DialogTitle = "Choose a bitmap"
    15.     'Set the filter
    16.     CDBox.Filter = "Windows Bitmaps (*.BMP)|*.bmp|All Files (*.*)|*.*"
    17.     'Show the 'Open File'-dialog
    18.     CDBox.ShowOpen
    19.     'Change the desktop's background
    20.     SystemParametersInfo SPI_SETDESKWALLPAPER, 0, CDBox.FileName, SPIF_UPDATEINIFILE
    21. End Sub
    22. Private Sub Form_Load()
    23.     Command1.Caption = "Set Wallpaper"
    24. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    That's more or less the code that I used. Could the problem be that I'm using a jpeg instead of a bitmap?

    *edit*

    The problem was that I was using a jpeg. Heres a fix:
    VB Code:
    1. Sub Main()
    2. Dim pic() As String
    3. Dim file As String
    4. Dim path As String
    5. Dim count As Integer
    6. Dim i As Integer
    7. Randomize Timer
    8. path = "C:\Windows\Desktop\Graphics\Desktops\"
    9. file = Dir(path)
    10. Do While file <> ""
    11. If file <> "." And file <> ".." Then
    12. ReDim Preserve pic(count)
    13. pic(count) = file
    14. count = count + 1
    15. End If
    16. file = Dir
    17. Loop
    18. count = count - 1
    19. i = Round(Rnd * count)
    20. SavePicture LoadPicture(path & pic(i)), path & "current.bmp"
    21. SystemParametersInfo 20, 0, path & "current.bmp", &H1
    22. End Sub
    The code changes the Wallpaper, but how can you make the desktop refresh itself? Right now I'm doing it manually.

    *edit*

    I tried SendMessage with WM_PAINT, and WM_NCPAINT, and then with both in a row, I sent it to the desktop window but that didn't work. Anyone have any ideas?
    Last edited by Mushroom Realm; Jun 9th, 2003 at 09:46 PM.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by manavo11
    Refresh the desktop
    Is it really that SIMPLE? c'mon.... you're yanking my chain aren't you? I've been looking for that piece of code for some time. Now I cna go back and finish that wall paper flipper I was working on.... Thanks!
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Happy coding


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    It didn't refresh it for me. Well it looked like it refreshed, but it seemed to be refreshing the same image that was laready there, but when I refreshed manually it loaded the new image. I also tried passing GetDesktopWindow() as the hwnd, but that didn't work either.

  8. #8

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    Anyone got another way?

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I've created a similar program to rotate my wallpaper, and this is the code I use:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SystemParametersInfo Lib "user32" Alias _
    4.   "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
    5.   ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
    6.  
    7.  
    8. Private Const SPIF_SENDWININICHANGE = &H2
    9. Private Const SPIF_UPDATEINIFILE = &H1
    10. Private Const SPI_SETDESKWALLPAPER = 20
    11.  
    12. Private Sub ChangeWallpaper(sFileName As String)
    13. Dim ret As Long, pPicture As StdPicture
    14.  
    15.   Set pPicture = LoadPicture(sFileName)
    16.   SavePicture pPicture, "C:\WINDOWS\TEMP\desktop.bmp"
    17.  
    18.   ret = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, ByVal "C:\WINDOWS\TEMP\desktop.bmp", _
    19.     SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE)
    20. End Sub

    And it refreshes the desktop for me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    That worked I guess you needed the &H2 in there as well.

  11. #11
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Post

    With the code The Hobo submitted, how do you tile and center??

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