如何在C+&#x2B中快速更改壁纸?
是否有任何快速的方法可以更改C ++中的Windows壁纸?我正在尝试为墙纸动画,并正在浏览图像目录,并将墙纸设置为每几毫秒,问题即使在线程上运行,有时在墙纸更改之间存在几秒钟的时间差距?我该如何解决?我做错了吗?这是我的代码:
#include <windows.h>
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
void thinggg()
{
cout << "Directory(0) or Files(1)?\n";
int choice;
cin >> choice;
int num = 0;
string* images;
if (choice == 1)
{
cout << "How many images?\n";
cin >> num;
cin.ignore();
images = new string[num];
for (int i = 0; i < num; i++)
{
string loc;
cout << i + 1 << ": ";
getline(cin, loc);
images[i] = loc;
}
}
else
{
cin.ignore();
string loc;
cout << "Enter folder location: ";
getline(cin, loc);
// there is probably a much better to do this
for (const auto& entry : fs::directory_iterator(loc))
{
num++;
}
images = new string[num];
num = 0;
for (const auto& entry : fs::directory_iterator(loc))
{
string path = entry.path().string();
if (path.find("png") != string::npos || path.find("jpg") != string::npos || path.find("bmp") != string::npos)
{
images[num] = path;
}
num++;
}
}
int intervals;
cout << "Enter FPS: ";
cin >> intervals;
cin.ignore();
intervals = 1000 / intervals;
int i = 0;
while (true)
{
wstring loc = wstring(images[i].begin(), images[i].end());
const wchar_t* a = loc.c_str();
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)a, SPIF_SENDCHANGE);
i++;
if (i == num) i = 0;
this_thread::sleep_for(chrono::milliseconds(intervals));
}
}
int main()
{
thread t(thinggg);
t.join();
cout << "quit?" << endl;
int a;
cin >> a;
if (a == 1)
{
t.native_handle();
return 0;
}
}
Is there any quick way to change a windows wallpaper in C++? I'm trying to animate a wallpaper and am going through a directory of images and setting the wallpaper to an image every few milliseconds, problem is, even when running on a thread, there is sometimes a time gap of a few seconds between wallpaper changes? How do I fix this? Am I doing something wrong? Here's my code:
#include <windows.h>
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
void thinggg()
{
cout << "Directory(0) or Files(1)?\n";
int choice;
cin >> choice;
int num = 0;
string* images;
if (choice == 1)
{
cout << "How many images?\n";
cin >> num;
cin.ignore();
images = new string[num];
for (int i = 0; i < num; i++)
{
string loc;
cout << i + 1 << ": ";
getline(cin, loc);
images[i] = loc;
}
}
else
{
cin.ignore();
string loc;
cout << "Enter folder location: ";
getline(cin, loc);
// there is probably a much better to do this
for (const auto& entry : fs::directory_iterator(loc))
{
num++;
}
images = new string[num];
num = 0;
for (const auto& entry : fs::directory_iterator(loc))
{
string path = entry.path().string();
if (path.find("png") != string::npos || path.find("jpg") != string::npos || path.find("bmp") != string::npos)
{
images[num] = path;
}
num++;
}
}
int intervals;
cout << "Enter FPS: ";
cin >> intervals;
cin.ignore();
intervals = 1000 / intervals;
int i = 0;
while (true)
{
wstring loc = wstring(images[i].begin(), images[i].end());
const wchar_t* a = loc.c_str();
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)a, SPIF_SENDCHANGE);
i++;
if (i == num) i = 0;
this_thread::sleep_for(chrono::milliseconds(intervals));
}
}
int main()
{
thread t(thinggg);
t.join();
cout << "quit?" << endl;
int a;
cin >> a;
if (a == 1)
{
t.native_handle();
return 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论