AutoComplete textbox and “Hide Pointer While Typing” in windows

前端 未结 2 1256
时光取名叫无心
时光取名叫无心 2021-01-03 12:29

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 l

2条回答
  •  攒了一身酷
    2021-01-03 13:11

    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.

    Two possible solutions:

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

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

提交回复
热议问题