C++ 在特定时间间隔后隐藏和显示鼠标光标

C++ 在特定时间间隔后隐藏和显示鼠标光标,c++,winapi,mouse,visual-c++,C++,Winapi,Mouse,Visual C++,如果我的系统鼠标未移动,我想在5秒钟后隐藏鼠标。我问了这个问题 现在我可以检查鼠标的活动并更改光标。。。 我的代码是: #define WINDOWS_LEAN_AND_MEAN #include <windows.h> #include <io.h> #include <iostream> #include <ctime> #include "resource.h" HCURSOR hCurBlank; HCURSOR hCurstanda

如果我的系统鼠标未移动,我想在5秒钟后隐藏鼠标。我问了这个问题

现在我可以检查鼠标的活动并更改光标。。。 我的代码是:

#define WINDOWS_LEAN_AND_MEAN

#include <windows.h>
#include <io.h>
#include <iostream>
#include <ctime>
#include "resource.h"

HCURSOR hCurBlank; 
HCURSOR hCurstandard;

void blank_cursor()
{
     hCurBlank =  LoadCursor(NULL,  MAKEINTRESOURCE(IDC_CURSOR1)); //loading a cursor from a file
     HCURSOR hcCopyblnkcur = CopyCursor(hCurBlank);
    BOOL bret=  SetSystemCursor( hcCopyblnkcur, 32512); //will set the cursor by using load cursor from file

}

void default_cursor()
{

            hCurstandard = LoadCursor(NULL,  MAKEINTRESOURCE(IDC_CURSOR2));
            HCURSOR hcursdef = CopyCursor(hCurstandard);
            HCURSOR hCurStandard = GetCursor();
            //SetCursor(hCurstandard);
        BOOL brets =SetSystemCursor( hcursdef, 32512);


}
void determine_idle_time()
{
    LASTINPUTINFO lif;   //Typedef for last input info will result in cb and dwtime
    lif.cbSize = sizeof(LASTINPUTINFO);
    DWORD tickCount, idleCount;   // Double word value


    for (;;)
    {
        GetLastInputInfo(&lif);    //It will check the system's last activity module
        tickCount = GetTickCount();    //GetTickCount will get the time from which system has been started and will be continue
        //upto 49 days
        idleCount = (tickCount - lif.dwTime) / 1000;  //This will return the time in the seconds. It will check for 
        //starting time and the last input info time. so the last activity time will be returned

        std::cout<< "Idle time: " << idleCount << " seconds." << std::endl;


        // Sleep for some milliseconds before the next output.
        DWORD randomized_time = rand() % (800 - 600 + 1) + 10;   //rand()%(800-300+1) + 300;
        //rand will pick any random value and this will generate a double value
        Sleep(randomized_time);
        //Sleep(500);
        if(idleCount>10)
        {
            blank_cursor();

        }
        else 
        {
            default_cursor();

        }
    }
}

int main() {
   srand(unsigned(time(NULL)));
   determine_idle_time();


}
#定义WINDOWS_LEAN_和_MEAN
#包括
#包括
#包括
#包括
#包括“resource.h”
HCURSOR hCurBlank;
HCURSOR hCurstandard;
无效空白光标()
{
hCurBlank=LoadCursor(NULL,MAKEINTRESOURCE(IDC_CURSOR1));//从文件加载光标
HCURSOR hcCopyblnkcur=复制光标(hCurBlank);
BOOL bret=SetSystemCursor(hcCopyblnkcur,32512);//将使用从文件加载光标来设置光标
}
无效默认光标()
{
hCurstandard=LoadCursor(NULL,MAKEINTRESOURCE(IDC_CURSOR2));
HCURSOR hcursdef=复制光标(hCurstandard);
HCURSOR hCurStandard=GetCursor();
//设置光标(hCurstandard);
布尔-布雷茨=设置系统光标(hcursdef,32512);
}
无效确定空闲时间()
{
LASTINPUTINFO lif;//最后输入信息的Typedef将产生cb和dwtime
lif.cbSize=sizeof(LASTINPUTINFO);
DWORD tickCount,idleCount;//双字值
对于(;;)
{
GetLastInputInfo(&lif);//它将检查系统的最后一个活动模块
tickCount=GetTickCount();//GetTickCount将获取系统启动并继续运行的时间
//最多49天
idleCount=(tickCount-lif.dwTime)/1000;//这将以秒为单位返回时间。它将检查
//开始时间和上次输入信息时间。因此将返回上次活动时间

std::您是否尝试过只使用
SetCursor(LoadCursor(NULL,NULL));
?这不是一个完全的骗局,但有一些关于SetSystemCursor的警示故事和避免它的方法。@Daboyzuk,我尝试过,但它不起作用。@RetiredInja,我使用过这个,在使用LoadCursorFromFile()时还写了我的问题。),它可以正常工作。但是它需要直接的.cur或.ani文件,而我在资源中的映像无法执行此操作。的文档没有说明第一个参数可以为NULL,第二个参数的某些值除外。在其他情况下,您应该为其提供模块实例句柄。