自动完成文本框和“键入时隐藏指针"在窗户里 [英] AutoComplete textbox and "Hide Pointer While Typing" in windows

查看:14
本文介绍了自动完成文本框和“键入时隐藏指针"在窗户里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序如何禁用键入时隐藏指针"选项?我遇到了光标隐藏的问题,并且在不按 Esc 键或失去窗口焦点的情况下无法将其恢复.该应用程序已用 C# 编写并使用 WPF.不需要特定于技术的答案,因为使用任何技术都有可能.

How can the "Hide Pointer While Typing" option be disabled by application? I'm having an issue with the cursor hiding and not getting it back without pressing escape or losing window focus. The application has been written in C# and uses WPF. A technology specific answer is not required because it'll likely be possible using any technology.

场景如下:用户可以在 TextBox 中键入内容,并且自动完成列表显示在该框下方.但是,一旦用户开始打字,他/她就无法再从下拉列表中选择项目,因为没有鼠标光标.

Here's the scenario: A user can type in a TextBox and an AutoComplete list shows up below the box. Once the user starts typing though, he/she can no longer select an item from the drop down because there is no mouse cursor.

我注意到 Firefox 没有这种行为.例如,在地址栏中输入 URL 时,鼠标光标永远不会消失.我在其他地方也看到过这种行为,所以我知道这一定是可能的.

I noticed that Firefox does not have this behavior. For example, while typing in a URL in the address bar the mouse cursor never disappears. There are other places I've seen this behavior so I know it must be possible.

非常感谢任何帮助!

推荐答案

我通过设置断点发现,当您第一次在文本框中键入时,WPF 读取公共 SystemParameters.MouseVanish 属性,该属性调用 SystemParametersInfo(SPI_GETMOUSEVANISH, ...) 获取鼠标消失设置.对 SystemParameters.MouseVanish 的后续调用使用缓存的值.

I discovered by setting breakpoints that the first time you type in a textbox, WPF reads the public SystemParameters.MouseVanish property which calls SystemParametersInfo(SPI_GETMOUSEVANISH, ...) to get the mouse vanish setting. Subsequent calls to SystemParameters.MouseVanish use the cached value.

两种可能的解决方案:

  1. 访问SystemParameters.MouseVanish,然后使用反射覆盖缓存的结果,以便后续调用返回false.
  2. 调用Win32的SystemParametersInfo(SPI_SETMOUSEVANISH, ...)关闭vanish(没有通知),然后访问SystemParameters.MouseVanish,然后调用SystemParametersInfo(SPI_SETMOUSEVANISH, ...) 将其设置回之前的值(没有通知)
  1. Access SystemParameters.MouseVanish, then use reflection to overwrite the cached result so subsequent calls return false.
  2. Call Win32's SystemParametersInfo(SPI_SETMOUSEVANISH, ...) to turn off vanish (with no notify), then access SystemParameters.MouseVanish, then call SystemParametersInfo(SPI_SETMOUSEVANISH, ...) to set it back to its prior value (with no notify)

这些都可以在用户开始在文本框中输入之前的任何时间完成.

Either of these can be done at any time before the user starts typing in the textbox.

以下是反射解决方案的外观:

Here is how the reflection solution would look:

void LocallyDisableMouseVanish()
{
  if(SystemParameters.MouseVanish)
    foreach(var field in typeof(SystemParameters).GetFields(BindingFlags.NonPublic | BindingFlags.Static)
      if(field.Name.Contains("mouseVanish"))
        field.SetValue(null, false);
}

对于普通目的,这样做一次就足够了,但用户可能会在您的应用程序运行时更改设置,这将导致 MouseVanish 值在下次访问时重新加载.如果您想防止这种情况发生,请使用 HwndSource.AddHook 挂钩 WM_SETTINGCHANGE 并安排回调以重新应用修复:

For ordinary purposes doing this once would be enough, but there is a possibility that the user will change settings while your app is running, which would cause the MouseVanish value to be reloaded on next access. If you want to guard against this, use HwndSource.AddHook to hook WM_SETTINGCHANGE and schedules a callback to re-apply the fix:

const int WM_SETTINGCHANGE = 26;

public void AddSettingChangeHook()
{
  _settingChangeWatcher = new HwndSource(new HwndSourceParameters("WM_SETTINGSCHANGE watcher"));
  _settingChangeWatcher.AddHook((IntPtr hwnd, IntPtr msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
  {
    if((int)msg == WM_SETTINGCHANGE)
      Dispatcher.Invoke(DispatcherPriority.Input, new Action(() =>
      {
        LocallyDisableMousePointerVanish();
      });
  });
}

这篇关于自动完成文本框和“键入时隐藏指针"在窗户里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆