ShowCursor(FALSE)不起作用

编程入门 行业动态 更新时间:2024-10-15 02:30:04
本文介绍了ShowCursor(FALSE)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这听起来可能是一个重复的问题,但请相信我不是.

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)不起作用

本文发布于:2023-11-24 01:12:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   ShowCursor   FALSE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!