自定义控件退出时光标没有改变
本文关键字:改变 光标 退出 时光 自定义控件 | 更新日期: 2023-09-27 18:03:24
我有这个问题:我创建了一个自定义控件(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);
}
我搜索了一下(例如这里和其他地方),我的代码似乎是正确的…
当你执行这个命令时:
oldCursor = Cursor;
您只需将引用传递给您的Cursor
字段。然后修改这个字段:
this.Cursor = NewCursor.CreateCursor(
Properties.Resources.cur_ZoomIn, 14, 9, Color.White);
这也改变了oldCursor
字段,作为引用类型对象。所以你应该改变你保存oldCursor
的方式。