美文网首页编程语言爱好者
使用代码修改Windows系统壁纸的方法

使用代码修改Windows系统壁纸的方法

作者: 平仄_pingze | 来源:发表于2018-03-18 17:08 被阅读19次

    必须是能够修改和立刻刷新,看到效果的方法。所以不使用修改注册表的方式,而是直接调用系统dll方法。关键方法为user32.dll的SystemParametersInfo
    (在Windows7下测试通过)

    另外,所有方法均需要图片为bmp格式。否则需要先转换得到bmp格式的图片。

    Python

    import win32gui
    
    def setWallpaper(imgPath):
        win32gui.SystemParametersInfo(20, imgPath, 3)
    

    * 推荐使用pillow库进行图像格式转换。

    Go

    import (
      "syscall"
      "unsafe"
      "fmt"
    )
    
    func setWallPaper(imgPath string) {
        dll := syscall.NewLazyDLL("user32.dll")
        proc := dll.NewProc("SystemParametersInfoW")
        ret, _, _ := proc.Call(20, 1,
            uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(imgPath))),
                0x1 | 0x2)
        fmt.Print(ret)
    }
    

    * 原生image库支持图像格式转换。支持bmp需要golang.org/x/image/bmp

    Node

    推荐调用其他语言编译结果。或写编写C++ addon。
    * node的库node-ffi可以调用dll;库sharp可以处理图片,但不支持bmp。

    C++

    在微软官方网站给出的代码基础上修改:

    #include <windows.h>
    #include <iostream>
    #pragma comment(lib, "user32.lib")    
    
    int main()  
    {     
        BOOL fResult;
        UINT* bmpPath = (UINT*)"D:\\Pictures\\wp.bmp"; 
        
        fResult = SystemParametersInfo(SPI_SETDESKWALLPAPER,
                                       1,
                                       bmpPath,
                                       SPIF_UPDATEINIFILE);
        std::cout << fResult;
        return 0;
    }
    

    C#

    // 部分
    [DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
    static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
    
    void fucntion SetWallpaper(imgPath) {
      int nResult;
      nResult = SystemParametersInfo(20, 1, bmpPath, 0x1 | 0x2);
    }
    

    相关文章

      网友评论

        本文标题:使用代码修改Windows系统壁纸的方法

        本文链接:https://www.haomeiwen.com/subject/oxaeqftx.html