【发布时间】:2016-02-13 23:20:08
【问题描述】:
我正在编写屏幕键盘,因为在旧机器上打开/关闭内置屏幕键盘很慢。
当我点击一个按钮时,我希望主窗口保持焦点并防止键盘窗口获得焦点。类似于内置的 Windows 10 屏幕键盘。
https://stackoverflow.com/a/12628353/4077230
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
//Set the window style to noactivate.
WindowInteropHelper helper = new WindowInteropHelper(this);
SetWindowLong(helper.Handle, GWL_EXSTYLE,
GetWindowLong(helper.Handle, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
}
private const int GWL_EXSTYLE = -20;
private const int WS_EX_NOACTIVATE = 0x08000000;
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
这段代码没有区别,鼠标点击时键盘窗口仍然被激活。
【问题讨论】:
-
将
Application.Current.MainWindow = this;放入YourMainWindow Loaded 事件中,将Activate((YourMainWindow )Application.Current.MainWindow));放入键盘窗口MouseUp 事件中。 -
我尝试了窗口 PreviewMouseLeftButtonDown 但在鼠标单击之前调用了 OnActivated,因此主窗口失去了清除 Keyboard.FocusedElement 的框架的焦点。