0

我创建了一个应用程序,但焦点有问题。表单加载时如何禁用另一个主表单的焦点?

4

1 回答 1

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);
于 2013-05-29T08:32:49.637 回答