“Listener” for detect changes on mouse icon

百般思念 提交于 2020-01-05 05:40:06

问题


I seen here a code that retrieves the current icon of mouse as string, but this code had uses a TTimer for make it.

So, i want know if exist some event (Listener) for detect these change on mouse cursor icon.

Below is code that uses a TTimer:

const
  HighCursor = 13;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FCursorHandles: array [0..HighCursor] of HCURSOR;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  OEMCursors: array [0..HighCursor] of Integer = (OCR_NORMAL, OCR_IBEAM,
      OCR_WAIT, OCR_CROSS, OCR_UP, OCR_SIZENWSE, OCR_SIZENESW, OCR_SIZEWE,
      OCR_SIZENS, OCR_SIZEALL, OCR_NO, OCR_HAND, OCR_APPSTARTING,
      32651 {OCR_HELP?});

  CursorNames: array [0..HighCursor] of string = ('OCR_NORMAL', 'OCR_IBEAM',
      'OCR_WAIT', 'OCR_CROSS', 'OCR_UP', 'OCR_SIZENWSE', 'OCR_SIZENESW',
      'OCR_SIZEWE', 'OCR_SIZENS', 'OCR_SIZEALL', 'OCR_NO', 'OCR_HAND',
      'OCR_APPSTARTING', 'OCR_HELP');

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to HighCursor do
    FCursorHandles[i] := LoadImage(0, MakeIntResource(OEMCursors[i]),
        IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR or LR_DEFAULTSIZE or LR_SHARED);
end;

procedure TForm1.Timer1Timer(Sender: TObject);

  function GetCursorName(Cursor: HCURSOR): string;
  var
    i: Integer;
  begin
    for i := 0 to HighCursor do
      if Cursor = FCursorHandles[i] then begin
        Result := CursorNames[i];
        Exit;
      end;
    Result := 'Unknown Cursor';  // A custom cursor.
  end;

var
  CursorInfo: TCursorInfo;
begin
  CursorInfo.cbSize := SizeOf(CursorInfo);
  if GetCursorInfo(CursorInfo) then
    Label1.Caption := GetCursorName(CursorInfo.hCursor)
  else
    Label1.Caption := 'Fail: ' + SysErrorMessage(GetLastError);
end;

回答1:


The way that applications listen for events is via Windows messages. There is no message sent when the cursor image is changed, so there is nothing to listen for; your code using a timer is the only possibility.

See Cursors at MSDN for the functions and notifications that Windows provides for cursors.



来源:https://stackoverflow.com/questions/39185470/listener-for-detect-changes-on-mouse-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!