English 中文(简体)
AutoComplete textbox and "Hide Pointer While Typing" in windows
原标题:

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.

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.

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.

Any help is greatly appreciated!

最佳回答

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();
      });
  });
}
问题回答

Look like win api function SystemParametersInfo would do what you need; parameters to control the pointer vanish feature are:

SPI_GETMOUSEVANISH and SPI_SETMOUSEVANISH

The Mouse Vanish accessibility feature has been added for Windows Me and Windows XP. This feature hides the pointer when the user is typing. The mouse pointer reappears when the user moves the mouse. This feature keeps the pointer from obscuring the text being typed, for example, in an e-mail or other document.

check SystemParametersInfo Function description on msdn for more info





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签