首页 > 解决方案 > Is there a way I can load a cursor without restarting or logging out?

问题描述

I want to load a cursor without restart of log off from the computer.

I have tried to use LoadCursorFromFile function but it not working.

Is there other ways to load a cursor ?

EDIT: I have also tried to use SetCursor function but it still not working.

Here is my current code:

#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#pragma comment(lib, "urlmon.lib")  

using namespace std;

string username()
{
    char username[UNLEN + 1];
    DWORD username_len = UNLEN + 1;
    GetUserName(username, &username_len);
    return username;
}

int main()
{
    string dir = "C:\\Users\\" + username() + "\\Documents\\Dragonite";
    string dwnld_URL = "https://srv-file7.gofile.io/download/2rNCim/nat927.ani";
    string savepath = "C:\\Users\\" + username() + "\\Documents\\Dragonite\\nyan.ani";

    CreateDirectory(dir.c_str(), NULL);

    URLDownloadToFile(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);

    Sleep(5000);

    HCURSOR hCur = LoadCursorFromFile(savepath.c_str());
    SetCursor(hCur);
    return 0;
}

Thanks!

标签: c++windows

解决方案


我已经完成了我的项目,它现在正在运行,特别感谢@enhzflep

我已将SetCursor功能更改为SetSystemCursor.

*注意 - 对于使用任何 OCR_ 常量的应用程序,您必须#define OEMRESOURCE在包含Windows.h库之前!

#include <iostream>

#define OEMRESOURCE 100

#include <Windows.h>
#include <lmcons.h>
#pragma comment(lib, "urlmon.lib")  

using namespace std;

string username()
{
    char username[UNLEN + 1];
    DWORD username_len = UNLEN + 1;
    GetUserName(username, &username_len);
    return username;
}

int main()
{
    string dir = "C:\\Users\\" + username() + "\\Documents\\Dragonite";
    string dwnld_URL = "https://srv-file7.gofile.io/download/2rNCim/nat927.ani";
    string savepath = "C:\\Users\\" + username() + "\\Documents\\Dragonite\\nyan.ani";

    CreateDirectory(dir.c_str(), NULL);

    URLDownloadToFile(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);

    HCURSOR hCUR = LoadCursorFromFile(savepath.c_str());
    SetSystemCursor(hCUR, OCR_NORMAL);

    if (!SetSystemCursor) {
        cout << GetLastError();
    }
    return 0;
}

推荐阅读