MouseCursor не скрывается с помощью SetSystemCursorC#

Место общения программистов C#
Ответить
Anonymous
 MouseCursor не скрывается с помощью SetSystemCursor

Сообщение Anonymous »

я пытаюсь скрыть курсор, встряхивая мышь, как в стиле Mac OS
также редактирую репозиторий AngryMouse, чтобы реализовать эту функцию
AngryMouse
но функция SetSystemCursor не работает через секунду встряхните... то есть после повторного использования функции с теми же параметрами
есть ли способ решить эту проблему или альтернативный способ сделать это успешно
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace AngryMouse.Animation
{
public static class CursorManager
{
private static IntPtr _originalArrowCursor;
private static IntPtr _transparentCursor;
private static bool _isCursorHidden = false;
private static bool _isInitialized = false;
private static readonly object cursorLock = new object();

// Constants for cursor types
private const uint OCR_NORMAL = 32512; // The standard arrow cursor

[DllImport("user32.dll")]
private static extern bool SetSystemCursor(IntPtr hCursor, uint id);

[DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string lpFileName);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyCursor(IntPtr hCursor);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern int GetLastError();

public static void Initialize()
{
if (!_isInitialized)
{
lock (cursorLock)
{
// Load the original cursor once for restoration purposes
_originalArrowCursor = LoadCursorFromFile(@"C:\Windows\Cursors\macosCursors-s-b\Normal.cur");
if (_originalArrowCursor == IntPtr.Zero)
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Error loading original cursor: {error}");
}
_transparentCursor = CreateTransparentCursor();
_isInitialized = true;
}
}
}

private static IntPtr CreateTransparentCursor()
{
using (Bitmap bmp = new Bitmap(32, 32)) // Create a transparent 32x32 bitmap
{
return bmp.GetHicon(); // Create a cursor from the transparent icon
}
}

public static void HideCursor()
{
if (!_isInitialized) Initialize();

lock (cursorLock)
{
// Hide the cursor only if it is not already hidden
if (!_isCursorHidden && _transparentCursor != IntPtr.Zero)
{
DestroyCursor(_originalArrowCursor);
// Set the cursor to the transparent version
bool result = SetSystemCursor(_transparentCursor, OCR_NORMAL);

if (!result)
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Error hiding cursor: {error}");
}
else
{
_isCursorHidden = true;
Console.WriteLine("Cursor hidden successfully.");
}
}
else
{
Console.WriteLine("Cursor is already hidden.");
}
}
}

public static void ShowCursor()
{
if (!_isInitialized) Initialize();

lock (cursorLock)
{
// Show the cursor only if it is currently hidden
if (_isCursorHidden && _originalArrowCursor != IntPtr.Zero)
{
DestroyCursor(_transparentCursor);
// Restore the original cursor
bool result = SetSystemCursor(_originalArrowCursor, OCR_NORMAL);
if (!result)
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Error showing cursor: {error}");
}
else
{
_isCursorHidden = false;
Console.WriteLine("Cursor shown successfully.");

// Re-create the transparent cursor to ensure it is available
DestroyCursor(_transparentCursor);
_transparentCursor = CreateTransparentCursor();
}
}
else
{
Console.WriteLine("Cursor is already visible.");
}
}
}
}
}



Подробнее здесь: https://stackoverflow.com/questions/791 ... stemcursor
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»