自定义控件退出时光标没有变化

发布于 2024-11-26 11:07:17 字数 1402 浏览 2 评论 0原文

我遇到了这个问题:我创建了一个自定义控件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-12-03 11:07:17

当您执行此操作时:

oldCursor = Cursor;

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

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

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

When you execute this:

oldCursor = Cursor;

you simply pass the reference to your Cursor field. After that you change this field:

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

This changes the oldCursor field also, as reference type object.
So you should change the way you save oldCursor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文