如何在C+&#x2B中快速更改壁纸?

发布于 2025-02-03 05:11:03 字数 2240 浏览 2 评论 0原文

是否有任何快速的方法可以更改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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文