如何确定当前鼠标光标是否为动画

How do I determine if the current mouse cursor is animated?

本文关键字:是否 动画 光标 鼠标 何确定      更新时间:2023-10-16

是否有一种方法可以确定当前鼠标光标是否为动画

我正在寻找一种方法如何保存当前光标一段时间前。我发现DrawIconEx函数非常适合我的目的。不幸的是,我不知道如何确定当前光标是否为动画。我希望如果我设置istepIfAniCur参数为1,在静态光标的情况下,DrawIconEx返回False,但它真的忽略了这个参数并返回True,它不允许我在循环中使用它来获取静态光标以及动画中的所有帧。在动画的情况下,一个工作如预期的,所以当你离开范围与istepIfAniCur它返回False。

那么我如何发现HICON (HCURSOR)是动画光标呢?DrawIconEx如何确定光标是动画的?

Thanks to lot

我找到了一个解决方案-传递到DrawIconEx函数的istepIfAniCur参数的UINT的最大值。不可能有人创建4,294,967,295帧的动画光标(对于某些光标电影可能是可能的)

有了这个事实,你可以将这个值传递给DrawIconEx函数,如果光标是动画的(因为超出了帧范围),它将返回False,如果是静态的,它将返回True,因为它忽略了istepIfAniCur参数。您应该将0传递给diFlags参数,因为不需要绘制任何东西。

下面是Delphi的例子:

if not DrawIconEx(Canvas.Handle, 0, 0, hCursor, 0, 0, High(Cardinal), 0, 0) then
  Caption := 'Cursor is animated ...'
else
  Caption := 'Cursor is not animated ...';

因为我承诺c++标签这是我的翻译尝试

if (!DrawIconEx(this->Canvas->Handle, 0, 0, hCursor, 0, 0, UINT_MAX, NULL, 0))
  this->Caption = "Cursor is animated ...";
else
  this->Caption = "Cursor is not animated ...";


超出帧范围也由操作系统错误ERROR_INVALID_PARAMETER指示,当DrawIconEx失败时,您可以使用GetLastError函数检查。

最佳方式:

typedef HCURSOR(WINAPI* GET_CURSOR_FRAME_INFO)(HCURSOR, LPCWSTR, DWORD, DWORD*, DWORD*);
GET_CURSOR_FRAME_INFO fnGetCursorFrameInfo = 0;
HMODULE libUser32 = LoadLibraryA("user32.dll");
if (!libUser32)
{
  return false;
}
fnGetCursorFrameInfo = reinterpret_cast<GET_CURSOR_FRAME_INFO>(GetProcAddress(libUser32, "GetCursorFrameInfo"));
if (!fnGetCursorFrameInfo)
{
  return false;
}
DWORD displayRate, totalFrames;
fnGetCursorFrameInfo(hcursor, L"", 0, &displayRate, &totalFrames);

以下是Delphi中的示例(并尝试翻译为c++)我如何尝试使用GetIconInfo函数获得光标尺寸,但它不像我预期的那样工作。在动画光标的情况下,它总是返回一帧的宽度,所以似乎GetIconInfo根本不关心帧。还是我错了?

procedure TForm1.Timer1Timer(Sender: TObject);
var
  IconInfo: TIconInfo;
  CursorInfo: TCursorInfo;
  Bitmap: Windows.TBitmap;
begin
  CursorInfo.cbSize := SizeOf(CursorInfo);
  GetCursorInfo(CursorInfo);
  GetIconInfo(CursorInfo.hCursor, IconInfo);
  if GetObject(IconInfo.hbmColor, SizeOf(Bitmap), @Bitmap) <> 0 then
  begin
    Caption := 'Cursor size: ' +
               IntToStr(Bitmap.bmWidth) + ' x ' +
               IntToStr(Bitmap.bmHeight) + ' px';
  end;
  DeleteObject(IconInfo.hbmColor);
  DeleteObject(IconInfo.hbmMask);
end;

我的Visual c++尝试(注意,我不懂c++,也没有编译器:)

CString txt;
ICONINFO ii;
CURSORINFO ci;
BITMAP bitmap;
ci.cbSize = SizeOf(CURSORINFO);
GetCursorInfo(ci);
GetIconInfo(ci.hCursor, ii);
GetObject(ii.hbmColor, sizeof(BITMAP), &bitmap);
txt.Format("Cursor width: %d px", bitmap.bmWidth);
MessageBox(txt);