【问题标题】:C# get current cursor iconC#获取当前光标图标
【发布时间】:2017-07-21 21:30:25
【问题描述】:

我一直在尝试这样做,但无法做到,这就是我要做的:

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

[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
public static extern bool GetCursorInfo(out CURSORINFO pci);

[DllImport("user32.dll", EntryPoint = "CopyIcon")]
public static extern IntPtr CopyIcon(IntPtr hIcon);

[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
    public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
    public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
    public IntPtr hCursor;          // Handle to the cursor. 
    public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
}

public void etc()
{
    IntPtr hwndic = new IntPtr();
    CURSORINFO curin = new CURSORINFO();
    curin.cbSize = Marshal.SizeOf(curin);
    if (GetCursorInfo(out curin))
    {
        if (curin.flags == CURSOR_SHOWING)
        {
            hwndic = CopyIcon(curin.hCursor);
            SetSystemCursor(hwndic, OCR_NORMAL);
        }
    }
}

问题是复制的图标有时与默认的鼠标图标不同,例如,如果它在等待位置捕获它,那么它可能会给我鼠标等待图标。

我想获取正在运行的系统的默认光标图标(空闲时的图标),我该怎么做?

提前致谢。

【问题讨论】:

  • 看LoadCursor和IDC_ARROW常量
  • @Pascalz 这对我没有帮助,它没有检测到系统的光标是什么。
  • 复制前可以设置Cursor.Current = Cursors.Default吗?
  • @obl 我以前试过这样做,它说成员'cursor.current'不能用实例引用访问,而是用类型名称来限定它,据我所知,光标命令只能在表单设计器文件中使用,而不是在它之外。因此,即使我设法在应用程序启动时获取光标图标,用户也可能会在之后更改其光标图标,并且我将无法返回上次用户更改的光标图标。
  • @obl 也是您所说的即使可以正常工作也无法正常工作,因为 setsystemcursor 函数粘贴的光标超出了应有的阴影。无论如何我已经解决了这个问题,我会回答这个线程。

标签: c# wpf cursor


【解决方案1】:

我已经解决了我的问题,经过一些研究,我找到了一些可能一直有效的有趣的东西。

我没有尝试保存光标的图标并加载它,而是想到了一个不同的想法。

我注意到,每当我更改光标图标(比如说使用代码,更改为某个随机图标)时,每当我进入 Windows 光标设置时,图标都不会更改,但没有应用按钮来应用我之前的设置,因为 windows 认为我正在使用这些设置。

所以我将设置更改为一些随机的其他设置而不应用并返回到我之前的设置并应用,这样做将我的光标重置为其原始光标,在任何更改之前。

因此,windows 只是在“刷新”鼠标,所以我就这样做了,也许如果我可以强制刷新一切都会很完美,所以我找到了一种方法,使用这个函数:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction,UInt32 uiParam, String pvParam, UInt32 fWinIni);

当我查看 msdn 时,我在它的参数中发现了一些有趣的东西,参数“SPI_SETCURSORS (0x57)”,我引用了:

"重新加载系统游标。将 uiParam 参数设置为零,pvParam 参数设置为 NULL。"

所以我已经尝试过了,它确实有效,流程示例:

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern IntPtr LoadCursorFromFile(String str);

uint SPI_SETCURSORS = 0x57;
var NewArrow = LoadCursorFromFile("C:\\Users\\mtnju\\Downloads\\invisible.cur"); // loads some new cursor icon using the LoadCursorFromFile function
SetSystemCursor(NewArrow, OCR_NORMAL); // sets the new cursor icon using the SetSystemCursor function
SystemParametersInfo(SPI_SETCURSORS, 0, null, 0);// reloads all of the system cursors

我以为我需要 5 分钟才能完成这样的事情......我希望它会帮助你们,我真的很感谢 cmet 试图帮助我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多