Not take focus, but allow interaction?

后端 未结 1 986
天命终不由人
天命终不由人 2020-12-03 08:39

The onscreen keyboard in Windows 7 will let you keep focus on a textbox while you type using the keyboard. In C# and .Net, how can I force another application to retain focu

相关标签:
1条回答
  • 2020-12-03 09:26

    You have to set the WS_EX_NOACTIVATE extended style on your window. The easiest way is by using P/Invoke.

    private const int WS_EX_NOACTIVATE = 0x08000000;
    private const int GWL_EXSTYLE = -20;
    
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hwnd, int index);
    
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
    
    ...
    
    var window = new Window();
    window.SourceInitialized += (s, e) => {
        var interopHelper = new WindowInteropHelper(window);
        int exStyle = GetWindowLong(interopHelper.Handle, GWL_EXSTYLE);
        SetWindowLong(interopHelper.Handle, GWL_EXSTYLE, exStyle | WS_EX_NOACTIVATE);
    };
    window.Show();
    

    You can also use WPF interop class HwndSource to create your window. Its constructor accepts the extended window style.

    0 讨论(0)
提交回复
热议问题