我正在开发一个应用程序,以特定的时间间隔随机更改用户壁纸。一切都按预期工作,除了SystemParametersInfo()偶尔设置0的错误,导致壁纸被重置为黑屏。
0错误应该表示操作成功,但事实并非如此。每次设置新背景时都不会发生此错误,而是随机发生。我搜索过这个网站和其他许多可能的解决方案..但无济于事。
void setBackground() {
dirSize = FindMaxFileNum(); //Reads num of .jpg's in the folder.
if(dirSize != -1)
{
srand( (unsigned int)time(NULL));
bgChoice = rand() % dirSize + 1;
//Ensures backgrounds don't repeat.
for(int i=0; i<=2; i++)
{
if(lastBackground[i] == bgChoice)
{ bgChoice = rand() % dirSize + 1; }
}
//Tracks the last background used.
lastBackground[bgCount] = bgChoice;
bgCount++;
//Resets the counter so we can track the last 3 bg's used.
if(bgCount == 2)
{ bgCount = 0; }
}
else
{
dwLastError = GetLastError();
ssLastError << "Error: " << dwLastError << ".";
MessageBox(NULL, ssLastError.str().c_str(), "Error!", MB_ICONEXCLAMATION | MB_OK);
ssLastError.str("");
}
ssFilePath.str(""); //Ensures the stringstream is clean.
ssFilePath << strLocalDirectory << bgChoice << ".jpg";
if( SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)ssFilePath.str().c_str(),
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE) != 0)
{
// Refresh the desktop - cleaning of PIDL is in the WM_DESTROY message.
SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&pidl);
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,pidl,0);
}
else
{
dwLastError = GetLastError();
ssLastError << "Error: " << dwLastError << ".";
MessageBox(NULL, ssLastError.str().c_str(), "Error!", MB_ICONEXCLAMATION | MB_OK);
ssLastError.str("");
}
}