通过程序定期更改桌面壁纸

Programmatically change the desktop wallpaper periodically

本文关键字:桌面壁纸 过程 程序      更新时间:2023-10-16

创建一个定期更改桌面壁纸的程序的最佳方法是什么?我也想围绕这个程序创建一个GUI。我是一名计算机科学专业的学生,因此我知道Java和C++等语言的基本编程。这将在Windows 7操作系统上完成。

对于这样的项目,最好使用什么语言?

理想情况下,我想使用系统时钟来触发更改。这可能吗?

我疯了吗?

任何答案都将不胜感激。非常感谢。

在Java中:

import java.util.*;
public class changer
{
    public static native int SystemParametersInfo(int uiAction,int uiParam,String pvParam,int fWinIni);
    static
    {
        System.loadLibrary("user32");
    }
    public int Change(String path)
    {
       return SystemParametersInfo(20, 0, path, 0);
    }
    public static void main(String args[])
    {
        String wallpaper_file = "c:\wallpaper.jpg";
        changer mychanger = new changer();
        mychanger.Change(wallpaper_file);
    }
}

在Win32 C++中,可以使用SetTimer来触发更改。

#define STRICT 1 
#include <windows.h>
#include <iostream.h>
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) 
{
  LPWSTR wallpaper_file = L"C:\Wallpapers\wallpaper.png";
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallpaper_file, SPIF_UPDATEINIFILE);

  cout << "Programmatically change the desktop wallpaper periodically: " << dwTime << 'n';
  cout.flush();
}
int main(int argc, char *argv[], char *envp[]) 
{
    int Counter=0;
    MSG Msg;
    UINT TimerId = SetTimer(NULL, 0, 2000, &TimerProc); //2000 milliseconds
    cout << "TimerId: " << TimerId << 'n';
   if (!TimerId)
    return 16;
   while (GetMessage(&Msg, NULL, 0, 0)) 
   {
        ++Counter;
        if (Msg.message == WM_TIMER)
        cout << "Counter: " << Counter << "; timer messagen";
        else
        cout << "Counter: " << Counter << "; message: " << Msg.message << 'n';
        DispatchMessage(&Msg);
    }
   KillTimer(NULL, TimerId);
return 0;
}

这是一个相当简单的项目,可以使用任何可以调用Win32 API函数的语言(例如C++)轻松完成。更改壁纸的非明显功能是带有SPI_SETDESKWALLPAPER标志的SystemParametersInfo。你给它一个新图像的文件名,壁纸就会改变。