在C++ Builder中要实现桌布的自动更换功能,我认为主要两个关键点,其一既然要“自动”那就免不了要用到定时器,我们可以用Timer这个组件来完成,这个还好解决,第二个问题可能比较麻烦了,我们如何来设置桌布,好像BCB中并没有提供什么组件,那我们也就只好自力更生了,虽说本人只是一只菜鸟,但这个问题还是难不了我,它主要是用到了SystemParametersInfo 这个Windows API函数,它的声明如下:
BOOL SystemParametersInfo( UINT uiAction, // system parameter to query or set UINT uiParam, // depends on action to be taken PVOID pvParam, // depends on action to be taken UINT fWinIni // user profile update flag ); |
键名 | 含义 |
WallPaper | 桌布的文件名,但只限于BMP格式的位图 |
WallPaperStyle | 设置桌布样式 若为0,桌布将以原始尺寸显示在桌面中央 若为1,桌布将填满桌面 若为2,将桌布放大的画面大小,图像有可能失真 |
WallPaperOriginX | 如果WallPaperStyle为0,则这表示图像左上角X轴坐标 |
WallPaperOriginY | 如果WallPaperStyle为0,则这表示图像左上角Y轴坐标 |
#include 〈Registry.hpp〉 //先在注册表里添好桌布样式,在设置桌布 TRegistry *Registry = new TRegistry; try { Registry-〉OpenKey(“Control PanelDesktop”,false); Registry-〉WriteString("TileWallpaper","0"); //设置桌布样式 Registry-〉WriteString("WallpaperStyle",wallStyle); } __finally { delete Registry; } } //设置桌布 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,FileName.c_str(),SPIF_UPDATEINIFILE SPIF_SENDWININICHANGE); |
void __fastcall TForm1::Timer1Timer(TObject *Sender) { if (ListBox1-〉Items-〉Count〈=0) { return ; } if (index〉=ListBox1-〉Items-〉Count) { index=0; } //如果是JPG格式就将其转换为BMP格式,将其存到临时文件夹中 if(UpperCase(ListBox1-〉Items-〉Strings[index]).Pos(".JPG") UpperCase(ListBox1-〉Items-〉Strings[index]).Pos(".JPEG")) { AnsiString fileName=ExtractFileName(ListBox1-〉Items-〉Strings[index]); int len=fileName.LastDelimiter("."); fileName=fileName.SubString(0,len-1)+".bmp"; unsigned int *size=new unsigned int(256); char * buffer=new char[256]; GetTempPath(*size,buffer); fileName=AnsiString(buffer)+fileName; JPEGToBMP(ListBox1-〉Items-〉Strings[index],fileName); //设置桌布 SystemParametersInfo(SPI_SETDESKWALLPAPER,0,ListBox1-〉Items-〉Strings[index].c_str(), SPIF_UPDATEINIFILE SPIF_SENDWININICHANGE); } else { //设置桌布 SystemParametersInfo(SPI_SETDESKWALLPAPER,0,ListBox1-〉Items-〉Strings[index].c_str(), SPIF_UPDATEINIFILE SPIF_SENDWININICHANGE); } //重建系统缓存 RebuildIconCache(); index++; } |
……