gpt4 book ai didi

c# - 为什么 Cursor.Show() 和 Cursor.Hide() 不立即隐藏或显示光标?

转载 作者:太空狗 更新时间:2023-10-29 22:05:21 26 4
gpt4 key购买 nike

我正在为可视化工具编写拖动系统。单击并拖动时,它会移动您在窗口中看到的内容。当鼠标碰到面板的边缘时,我开始重新定位光标,使其永远不会离开框。如果光标在框内,它会跟踪光标所在的虚拟位置。这部分代码工作正常。

只要有 MouseMoved 事件并且位置在框内,我就会执行 Cursor.Show()。如果它在框外,我执行 Cursor.Hide()。当用户松开鼠标按钮时,我执行 Cursor.Show()。

有多个问题。当第一次隐藏调用发生时,它不会隐藏。我必须让光标的虚拟位置在包含窗口之外才能隐藏。当我搬回去时,它不会变得可见,即使正在调用 Show 也是如此。最后,当释放鼠标按钮时,尽管调用了 Show,但光标不会出现。

我并没有要求人们调试我的代码,而是想知道事件系统中发生了什么导致 Cursor.Hide/Show 无法按我预期的方式工作。我的印象是,如果一个名为 Hide 的控件,则光标在该控件内的任何时候都会被隐藏;同样,如果我从控件调用 show。

最佳答案

对于遇到此问题的任何人,请尝试类似的方法:

    private bool _CursorShown = true;
public bool CursorShown
{
get
{
return _CursorShown;
}
set
{
if (value == _CursorShown)
{
return;
}

if (value)
{
System.Windows.Forms.Cursor.Show();
}
else
{
System.Windows.Forms.Cursor.Hide();
}

_CursorShown = value;
}
}

并使用它:

CursorShown = false; // Will hide the cursor
CursorShown = false; // Will be ignored
CursorShown = true; // Will show the cursor
CursorShown = true; // Will be ignored

关于c# - 为什么 Cursor.Show() 和 Cursor.Hide() 不立即隐藏或显示光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7639502/

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