如何在WPF中禁用焦点?

时间:2013-05-29 08:28:39

标签: wpf

我创建了一个应用程序,但我对焦点有疑问。如何在表单加载时禁用其他主窗体的焦点?

1 个答案:

答案 0 :(得分:0)

WPF表单具有ShowActivated属性。将此设置为false,表单将无法获得焦点。

您也可以覆盖OnActivated方法:

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