c++ 后台应用程序循环播放壁纸

c++ 后台应用程序循环播放壁纸

#include<iostream>
#include<stdlib.h>
#include<sstream>
#include<string>
#include<fstream>
#include<Windows.h>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<ctime>
#include<algorithm>
#include<io.h>
#include "resource.h"
using namespace std;

const string play_folder = "./play_folder.txt";
vector<string> ImageList;

#define MAX_LOADSTRING 100

// 全局变量: 
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
HWND hWnd;

// 此代码模块中包含的函数的前向声明: 
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此放置代码。

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_VIRUSE2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化: 
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_VIRUSE2));

    MSG msg;

    // 主消息循环: 
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}

//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_VIRUSE2);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释: 
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // 将实例句柄存储在全局变量中

                       //将窗口句柄也保存到了全局变量
    hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, SW_HIDE);
    //ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    return TRUE;
}


void changeWallPaper(string imge)
{
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, PVOID(imge.c_str()), SPIF_SENDCHANGE);
}

//核心代码
void getFileNames(string path, vector<string>& files)
{
    //文件句柄
    //注意:我发现有些文章代码此处是long类型,实测运行中会报错访问异常
    intptr_t hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是目录,递归查找
            //如果不是,把文件绝对路径存入vector中
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFileNames(p.assign(path).append("\\").append(fileinfo.name), files);
            }
            else
            {
                string name = fileinfo.name;
                //找到最后一个.的位置 
                int last_dot = name.size() - 1;
                while (last_dot > -1)
                {
                    if (name[last_dot] == '.')
                    {
                        break;
                    }
                    last_dot--;
                }
                //获取后缀类型 
                string type = "";
                for (int i = last_dot + 1; i != name.size(); i++)
                {
                    //大写转小写 
                    if (name[i] >= 'A' && name[i] <= 'Z')
                    {
                        name[i] += 32;
                    }
                    type += name[i];
                }
                //过滤出来jpg和png 
                if (type == "jpg" || type == "png")
                {
                    files.push_back(p.assign(path).append("\\").append(fileinfo.name));
                }
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

void init()
{
    //读取txt获取要播放哪些文件夹
    vector<string> folders_path;
    ifstream infile;
    infile.open(play_folder.c_str(), ios::in);
    if (!infile)
    {
        ofstream outfile;
        outfile.open(play_folder.c_str(), ofstream::out);
        outfile.close();
        infile.open(play_folder.c_str(), ios::in);
    }
    string buff;
    while (getline(infile, buff))
    {
        folders_path.push_back(buff);
    }
    infile.close();
    //获取这些文件夹下所有的jpg,png
    for (int i = 0; i != folders_path.size(); i++)
    {
        getFileNames(folders_path[i], ImageList);
    }
    //打乱顺序
    srand((unsigned int)time(0));
    random_shuffle(ImageList.begin(), ImageList.end());
    cout << "init done!" << endl;
    cout << ImageList.size() << endl;
}


void change()
{
    //没有图片就直接结束,但是这个应用窗口并没有结束,没有写那个事件
    if (!ImageList.size())return;
    //死循环播放
    while (1)
    {
        for (int i = 0; i != ImageList.size(); i++)
        {
            changeWallPaper(ImageList[i]);
            Sleep(3000);
        }
    }
}

DWORD WINAPI change(LPVOID lparam)
{
    init();
    change();
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        ::CreateThread(
            NULL,
            NULL,
            change,
            NULL,
            0,
            NULL
        );
    }
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

文章目录