gpt4 book ai didi

c# - 自定义控件退出时光标未更改

转载 作者:可可西里 更新时间:2023-11-01 10:39:33 26 4
gpt4 key购买 nike

我遇到了这个问题:我创建了一个自定义控件(C#、WinForms、Framework 4.0),当用户按下某个键时我需要在其中更改光标(这有效);退出控制我想恢复到以前的光标..但这不起作用:退出光标仍然是当前光标。怎么了?

protected override void OnMouseEnter(EventArgs e)
{
oldCursor = Cursor;
base.OnMouseEnter(e);
}

protected override void OnMouseLeave(EventArgs e)
{
Cursor = oldCursor;
base.OnMouseLeave(e);
}

当按下按钮时,我会:

this.Cursor = NewCursor.CreateCursor(
Properties.Resources.cur_ZoomIn, 14, 9, Color.White);

在哪里

public static Cursor CreateCursor(
Bitmap bmp_parm, int xHotSpot, int yHotSpot, Color? transparent)
{
Image img = bmp_parm;
Bitmap bmp = new Bitmap(img, new Size(img.Width, img.Height));
if (transparent.HasValue) bmp.MakeTransparent(transparent.Value);

if (cursor != IntPtr.Zero)
DestroyIcon(cursor);

IntPtr ptr = bmp.GetHicon();
IconInfo tmp = new IconInfo();
GetIconInfo(ptr, ref tmp);
tmp.xHotspot = xHotSpot;
tmp.yHotspot = yHotSpot;
tmp.fIcon = false;
cursor = CreateIconIndirect(ref tmp);

if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor);
if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask);
if (ptr != IntPtr.Zero) DestroyIcon(ptr);

return new Cursor(cursor);
}

我四处搜索(例如 here 和其他地方),我的代码似乎是正确的...

最佳答案

当你执行这个时:

oldCursor = Cursor;

您只需将引用传递给您的 Cursor 字段。之后您更改此字段:

this.Cursor = NewCursor.CreateCursor(
Properties.Resources.cur_ZoomIn, 14, 9, Color.White);

这也将 oldCursor 字段更改为引用类型对象。所以你应该改变你保存 oldCursor 的方式。

关于c# - 自定义控件退出时光标未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6807184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com