gpt4 book ai didi

c# - WPF-防止在鼠标单击时激活窗口

转载 作者:行者123 更新时间:2023-12-03 21:36:28 25 4
gpt4 key购买 nike

我正在编写屏幕键盘,因为在旧机器上打开/关闭内置屏幕键盘很慢。

当我点击一个按钮时,我想让主窗口保持焦点并防止键盘窗口获得焦点。类似于内置的 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);

此代码没有任何区别,键盘窗口仍会在鼠标单击时激活。

最佳答案

如果我在窗口创建时设置它对我有用:

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

protected override void OnSourceInitialized(EventArgs e)
{
var hWnd = new WindowInteropHelper(this).Handle;
int style = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, style | WS_EX_NOACTIVATE);

base.OnSourceInitialized(e);
}

关于c# - WPF-防止在鼠标单击时激活窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com