自定义控件退出时光标没有变化
我遇到了这个问题:我创建了一个自定义控件(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);
}
(例如这里和其他地方)并且我的代码似乎是正确的...
I had this problem: I created a custom control (C#, WinForms, Framework 4.0) in which I need to change cursor when user presses some key (this works); exiting from control I want to restore to previous cursor.. but that doesn't work: exiting cursor remains current one. What's wrong?
protected override void OnMouseEnter(EventArgs e)
{
oldCursor = Cursor;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
Cursor = oldCursor;
base.OnMouseLeave(e);
}
When button is pressed I do:
this.Cursor = NewCursor.CreateCursor(
Properties.Resources.cur_ZoomIn, 14, 9, Color.White);
where
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);
}
I googled around (for example here and elsewhere) and my code seems right...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行此操作时:
您只需将引用传递给
Cursor
字段即可。之后,您更改此字段:这也会更改
oldCursor
字段,作为引用类型对象。所以你应该改变保存
oldCursor
的方式。When you execute this:
you simply pass the reference to your
Cursor
field. After that you change this field:This changes the
oldCursor
field also, as reference type object.So you should change the way you save
oldCursor
.