c# – 用于多个游标行为的SetSystemCursor()

c# – 用于多个游标行为的SetSystemCursor(),第1张

概述我正在尝试将多个游标更改为Cross cursor.这是我正在使用的代码: [DllImport("user32.dll")]static extern bool SetSystemCursor(IntPtr hcur, uint id);[DllImport("user32.dll")]static extern IntPtr LoadCursor(IntPtr hInstance, in 我正在尝试将多个游标更改为Cross cursor.这是我正在使用的代码:
[Dllimport("user32.dll")]static extern bool SetSystemCursor(IntPtr hcur,uint ID);[Dllimport("user32.dll")]static extern IntPtr LoadCursor(IntPtr hInstance,int lpCursorname);[Dllimport("user32.dll",CharSet = CharSet.auto)]private static extern Int32 SystemParametersInfo(UInt32 uiAction,UInt32 uiParam,String pvParam,UInt32 fWinIni);//normal cursorprivate static uint OCR_norMAL = 32512;//The text selection (I-beam) cursor.private static uint OCR_IBEAM = 32513;//The cross-shaped cursor.private static uint OCR_CROSS = 32515;

然后我使用我做的这两个函数:

static public voID ChangeCursors() {    SetSystemCursor(LoadCursor(IntPtr.Zero,(int)OCR_CROSS),OCR_norMAL);    SetSystemCursor(LoadCursor(IntPtr.Zero,OCR_IBEAM);}static public voID RevertCursors() {    SystemParametersInfo(0x0057,null,0);}

如果我只使用SetSystemCursor(LoadCursor(IntPtr.Zero,OCR_norMAL);,一切正常. normal游标被Cross游标替换.

我的问题是当我尝试将多个游标更改为Cross游标时.如果我调用ChangeCursors(),预期的结果将是normal游标和I-beam游标被Cross游标替换.但结果真是奇怪.

当程序启动时根据光标的当前状态启动软件时,会发生以下奇怪的事情:

>如果软件启动时光标为“正常”,则它将变为“交叉”(这很好).此外,I-beam被normal替换(这很糟糕,它应该是Cross).
>如果在软件启动时光标是工字梁,它会保持工字梁(这很糟糕,因为它应该是十字形).然后,通过将光标悬停到之前光标应该是normal的位置,它现在是Cross(这很好).然后,如果我在1秒前将鼠标悬停在光标所在的位置,它会神奇地变为正常(这很奇怪)并保持这种状态.

所以,我的问题是,如何使用SetSystemCursor()将2个或更多游标更改为交叉游标?

解决方法 不要对这种奇怪的行为感到困惑.每次分配时,只是游标被交换.

首先

normal  ==  normalIBeam   ==  IBeamCross   ==  Cross

您指定正常=交叉

normal  ==  CrossIBeam   ==  IBeamCross   ==  normal

现在分配IBeam = Cross(现在是正常的)

normal  ==  CrossIBeam   ==  normalCross   ==  IBeam

因此,为了不让它被交换,你必须保留所有游标的副本.我将举例说明normal和IBeam改为CROSS.

Program.cs中

static class Program{    [Dllimport("user32.dll")]    static extern bool SetSystemCursor(IntPtr hcur,uint ID);    [Dllimport("user32.dll")]    static extern IntPtr LoadCursor(IntPtr hInstance,int lpCursorname);    [Dllimport("user32.dll",CharSet = CharSet.auto)]    private static extern Int32 SystemParametersInfo(UInt32 uiAction,UInt32    uiParam,UInt32 fWinIni);    [Dllimport("user32.dll")]    public static extern IntPtr copyIcon(IntPtr pcur);    private static uint CROSS = 32515;    private static uint norMAL = 32512;    private static uint IBEAM = 32513;    [STAThread]    static voID Main()    {        Application.EnableVisualStyles();        Application.SetCompatibleTextRenderingDefault(false);        uint[] Cursors = {norMAL,IBEAM};        for (int i = 0; i < Cursors.Length; i++)            SetSystemCursor(copyIcon(LoadCursor(IntPtr.Zero,(int)CROSS)),Cursors[i]);        Application.Run(new Form1());        SystemParametersInfo(0x0057,0);    }}
总结

以上是内存溢出为你收集整理的c# – 用于多个游标行为的SetSystemCursor()全部内容,希望文章能够帮你解决c# – 用于多个游标行为的SetSystemCursor()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1252137.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存