问题描述
我知道这听起来可能是一个重复的问题,但请相信我不是.
I know this may sound to be a duplicate question but trust me it's not.
我已经提到了这个问题,但是涉及的并不多我正在尝试使用console application
时提供帮助,应答者本人告诉他他不知道ShowCursor(FALSE)无法在控制台应用程序中工作的原因.
I have referred this question, but was not of much help as I am trying with a console application
and the answerer himself tells he does not know the reason why ShowCursor(FALSE) does not work for console applications.
此线程没有帮助我也是.
这是我尝试过的事情:
使用ShowCursor():
while(ShowCursor(false)>=0); //did not work
我首先怀疑是由于 msdn :When Windows starts up, it checks if you have a mouse. If so, then the cursor show count is initialized to zero; otherwise, it is initialized to negative one
我想也许是在最新的Windows中,它无法将连接的鼠标或触控板识别为已安装的鼠标,也许这就是为什么它不起作用的原因.以下代码显示情况并非如此:
I first suspected that it was because of this statement in the msdn :When Windows starts up, it checks if you have a mouse. If so, then the cursor show count is initialized to zero; otherwise, it is initialized to negative one
I thought maybe in the latest windows, it doesn't recognize the connected mouse or the trackpad as an installed mouse and maybe that's why it didn't work. The following code shows it is not the case:
void UsingShowCursor()
{
CURSORINFO info;
info.cbSize = sizeof(CURSORINFO);
cout << ShowCursor(FALSE);
cout << ShowCursor(FALSE);
cout << ShowCursor(FALSE);
GetCursorInfo( &info ); //info.flags is CURSOR_SHOWING
}
因为我得到-1,-2,-3.这意味着最初的显示光标计数显然为0,并且确实标识了已安装的鼠标.
Because I get -1, -2, -3. That means the initial show cursor count is obviously 0 and it does identify the installed mouse.
要注意的另一件事是GetCursorInfo()
还会告诉您光标正在显示.
And another thing to note is that the GetCursorInfo()
also tells that the cursor is showing.
使用SetCursor()
void UsingSetCursor()
{
HCURSOR prev = SetCursor(NULL);
int i = 0;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
if( SetCursor(prev) == NULL ) //check if the previos cursor was NULL
cout<<"cursor was hidden and shown after 10 secs\n";
}
也不起作用.这也行不通:
Doesn't work either.This also did not work:
SetCursor(LoadCursor(NULL, NULL));
使用LoadImage
也不起作用.
void UsingLoadImage()
{
// Save a copy of the default cursor
HANDLE arrowHandle = LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED);
HCURSOR hcArrow = CopyCursor(arrowHandle);
HCURSOR noCursorHandle = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR,1,1,LR_SHARED); //a single pixel thick cursor so that it wont be visible
HCURSOR noCursor = CopyCursor(noCursorHandle);
SetSystemCursor(noCursor, OCR_NORMAL);
int i =0 ;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
//revert to previous cursor
SetSystemCursor(hcArrow, OCR_NORMAL);
DestroyCursor(hcArrow);
}
可能是什么错误?如何隐藏控制台应用程序的鼠标?
What can be the mistake? How can we hide the mouse for a console application?
推荐答案
您可以使用 LoadImage()实现所需的功能.这是您在问题中引用的函数UsingUseImage()的修改后的工作版本.您必须在Visual Studio项目中包括一个游标资源文件.从此处下载光标或创建自己的光标.
You can use LoadImage() to achieve what you want. Here is the modified working version of the function UsingLoadImage() you quoted in the question. You have to include a cursor resource file to your visual studio project. Download the cursor from here or create your own.
Resource Files->Add->Existng Item
并浏览到nocursor.cur文件.
Resource Files->Add->Existng Item
and browse to the nocursor.cur file.
void UsingLoadImage()
{
// Save a copy of the default cursor
HANDLE arrowHandle = LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED);
HCURSOR hcArrow = CopyCursor(arrowHandle);
// Set the cursor to a transparent one to emulate no cursor
HANDLE noCursorHandle = LoadImage(GetModuleHandle(NULL), L"nocursor.cur", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE); //worked
//HANDLE noCursorHandle = LoadCursorFromFile(L"nocursor.cur"); //this also worked
HCURSOR noCursor = CopyCursor(noCursorHandle);
SetSystemCursor(noCursor, OCR_NORMAL);
int i =0 ;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
SetSystemCursor(hcArrow, OCR_NORMAL);
DestroyCursor(hcArrow);
}
这会将普通的箭头光标替换为透明的箭头光标.如果要隐藏所有其他光标,例如文本,加载,手形光标等,则必须分别隐藏它们.如果您不希望出现这种情况,则应像许多评论者所指出的那样,退出控制台应用程序.
This would replace the normal arrow cursor with the transparent one. If you want to hide all the other cursor like the text, loading, hand cursors etc you have to hide them individually. If you don't want that to be the case, then you should opt out of the console application as many commenters have pointed out.
希望这会有所帮助.
这篇关于ShowCursor(FALSE)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!