首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

XP上更换壁纸

2012-08-16 
XP下更换壁纸求代码,一段~~~~~[解决办法]VB.NET codeImports System.Runtime.InteropServicesImports Syst

XP下更换壁纸
求代码,一段~~~~~

[解决办法]

VB.NET code
Imports System.Runtime.InteropServicesImports System.DrawingModule modMain    Const SPI_SETDESKWALLPAPER As Integer = &H14    Const SPIF_UPDATEINIFILE As Integer = &H1    Const SPIF_SENDWININICHANGE As Integer = &H2    Declare Auto Function SystemParametersInfo Lib "user32.dll" ( _    ByVal uAction As Integer, ByVal uParam As Integer, _    ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer    ''' <summary>    ''' 使用这个函数来设置壁纸    ''' </summary>    ''' <param name="fileName">壁纸文件地址,请使用绝对路径,确保文件存在且为BMP格式</param>    ''' <returns>传回一个布尔值指示成功与否</returns>    ''' <remarks></remarks>    Function SetWallpaper(ByVal fileName As String) As Boolean        Try            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fileName, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)        Catch ex As Exception            '利用Console.Write可以在命令行中回传信息#If DEBUG Then            Windows.Forms.MessageBox.Show(ex.Message, "出错啦", Windows.Forms.MessageBoxButtons.OK)#End If            Console.WriteLine("更换壁纸时出错")            Return False        End Try        Return True    End Function    ''' <summary>    ''' 主过程,无论错误与否都将返回0    ''' </summary>    ''' <param name="args">参数列表,列表为[路径,显示方式,无效……]</param>    ''' <returns>返回0</returns>    ''' <remarks></remarks>    Function Main(ByVal ParamArray args() As String) As Integer        '检查文件是否存在        If Not My.Computer.FileSystem.FileExists(args(0)) Then            Return 0        End If        '转换文件格式        Dim imgTP As New Drawing.Bitmap(args(0))        Dim tmpPath As String = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.MyPictures, "tmpImage.bmp")        If My.Computer.FileSystem.FileExists(tmpPath) Then            My.Computer.FileSystem.DeleteFile(tmpPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)        End If        imgTP.Save(tmpPath, Imaging.ImageFormat.Bmp)        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "Wallpaper", args(0))        Select Case args(1)(0)            Case "0"c                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "TileWallpaper", "0")                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "WallpaperStyle", "0")            Case "2"c                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "TileWallpaper", "0")                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "WallpaperStyle", "2")            Case "1"c                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "TileWallpaper", "1")                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop\", "WallpaperStyle", "0")        End Select        'API 只支持BMP格式图片        If SetWallpaper(tmpPath) Then            '   Debug.WriteLine("Wallpaper Should be Changed.")            '   利用Console.Write可以在命令行中回传信息        End If        Return 0    End FunctionEnd Module
[解决办法]
#include<windows.h>
#include "Shlwapi.h"
#pragma comment(lib, "Shlwapi.lib")
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine, int iCmdShow)
{
DWORD thesleep = 10000;
HWND a = NULL, thelasthwnd = NULL;
BOOL therect = 0;
RECT arect = {0, 0, 0, 0};
PRECT alprect = &arect;


UINT uiAction = 20, uiParam = 0, fWinIni = 3, theexec = 0;
while(1)
{
Sleep (thesleep);
a = GetForegroundWindow ();
if (a != NULL && a != thelasthwnd)
{
thelasthwnd = a;
therect = GetWindowRect (a, alprect);
if (therect != 0)
{
if (arect.left <= 30 && arect.right >= (GetSystemMetrics(SM_CXSCREEN) - 30) && arect.top<= 30 && arect.bottom >= (GetSystemMetrics(SM_CYSCREEN) - 50))
{
therect = SystemParametersInfo (uiAction, uiParam, L"C:\\wallpaper\\now.bmp", fWinIni);
theexec = WinExec ("C:\\wallpaper\\rename.exe", 0);
}
}
}
}
return 0;
}

热点排行