加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#获取当前光标图标

发布时间:2020-12-15 21:07:21 所属栏目:百科 来源:网络整理
导读:我一直试图这样做而且无法做到,这就是我所要做的: [DllImport("user32.dll")]static extern bool SetSystemCursor(IntPtr hcur,uint id);[DllImport("user32.dll",EntryPoint = "GetCursorInfo")]public static extern bool GetCursorInfo(out CURSORINFO p
我一直试图这样做而且无法做到,这就是我所要做的:

[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);
        }
    }
}

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

我希望得到正在运行的系统的默认光标图标(空闲时的图标),我该怎么办?

提前致谢.

解决方法

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

而不是试图保存光标的图标并加载它我想到了一个不同的想法.

我注意到每当我更改光标图标(假设使用代码,某个随机图标)时,每当我进入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:UsersmtnjuDownloadsinvisible.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,null,0);// reloads all of the system cursors

我认为这需要5分钟才能完成这样的事情…我希望它会帮助你们,我真的很感激那些试图帮助我的评论.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读