Skip to content

golang设置桌面壁纸

Published: at 01:21 PM | 2 min read

golang语言编写,设置windows桌面壁纸,图片来自于必应网站(cn.bing.com)

github源码地址:https://github.com/tujiaw/gowallpaper
有兴趣的Star一下吧

编译好的程序:https://pan.baidu.com/s/1l5OW9GeuUF0r5TFaBWkWZg (提取码:pcqh)

直接双击运行,显示如下:

设置微软必应的壁纸,用法如下:
day     - 每天更新壁纸
now     - 设置当天壁纸
prev    - 设置前一天壁纸
next    - 设置后一天壁纸
rand    - 间隔随机切换壁纸(如每分钟切换壁纸:rand 1m)
quit    - 退出
#

golang调用windows API

package winapi

import (
	"log"
	"syscall"
	"unsafe"
)

var ApiList = map[string][]string {
	"user32.dll": {
		"MessageBoxW",
		"SystemParametersInfoW",
	},
	"kernel32.dll": {

	},
}

var ProcCache map[string]*syscall.Proc

func init() {
	ProcCache = make(map[string]*syscall.Proc)
	for dllName, apiList := range ApiList {
		d, err := syscall.LoadDLL(dllName)
		if err != nil {
			panic(err)
		}
		for _, name := range apiList {
			api, err := d.FindProc(name)
			if err != nil {
				log.Println(err, name)
			}
			ProcCache[name] = api
		}
		_ = syscall.FreeLibrary(d.Handle)
	}
}

func WinCall(name string, a ...uintptr) {
	if api, ok := ProcCache[name]; ok {
		_, _, err := api.Call(a...)
		if err != nil {
			log.Println(err)
		}
	} else {
		log.Println("api not found, name:", name)
	}
}

func IntPtr(n int) uintptr {
	return uintptr(n)
}

func StrPtr(s string) uintptr {
	p, _ := syscall.UTF16PtrFromString(s)
	return uintptr(unsafe.Pointer(p))
}

func ShowMessage(title, text string) {
	WinCall("MessageBoxW", IntPtr(0), StrPtr(text), StrPtr(title), IntPtr(0))
}

func SetWallpaper(bmpPath string) {
	WinCall("SystemParametersInfoW", IntPtr(20), IntPtr(0), StrPtr(bmpPath), IntPtr(3))
}