光标不改变自定义控制出口

我有这个问题:我创build了一个自定义控件(C#,WinForms,框架4.0),我需要改变光标时,用户按下一些键(这个工程); 退出控制我想恢复到以前的光标..但是这不起作用:退出光标保持当前之一。 怎么了?

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

当按下button我做:

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

我search周围(例如在这里和其他地方),我的代码似乎是正确的…

当你执行这个:

 oldCursor = Cursor; 

您只需将参考传递给您的Cursor字段。 之后,你改变这个领域:

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

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