打开wpf窗口,无需拉伸其他应用程序的键盘焦点

时间:2016-10-13 11:43:25

标签: c# wpf

当我在记事本中键入时,我想打开一个WPF窗口,而不会从记事本中窃取焦点。我还想输入记事本。它附带的样本工作正常。但现在我点击记事本来隐藏WPF窗口,但它无法正常工作。 我在DispatcherTimer tick(每10秒)调用下面的代码(GlobalActivate方法)。 我的代码是:

public static class SystemWindows
{
    #region Constants

    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;
    const UInt32 SWP_SHOWWINDOW = 0x0040;
    const UInt32 SWP_SHOWNOACTIVATE = 4;

    #endregion

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    /// <summary>
    /// Activate a window from anywhere by attaching to the foreground window
    /// </summary>
    public static void GlobalActivate(this Window w, IntPtr hWnd)
    {
        //Get the process ID for this window's thread
        var interopHelper = new WindowInteropHelper(w);
        var thisWindowThreadId = GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);

        //Get the process ID for the foreground window's thread
        var currentForegroundWindow = GetForegroundWindow();
        var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);

        //Attach this window's thread to the current window's thread
        AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

        if (w.WindowState == WindowState.Minimized)
            ShowWindow(hWnd, 4);
        else
            ShowWindow(hWnd, 8);

        //Set the window position
        // SetWindowPos(hWnd, fi.HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWNOACTIVATE);

        //Detach this window's thread from the current window's thread
        AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);

        ////Show and activate the window
        //if (w.WindowState == WindowState.Minimized) w.WindowState = WindowState.Normal;
        //w.Show();
        //w.Activate();
    }

    #region Imports

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

    [DllImport("user32.dll")]
    private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    #endregion
}

MainWindow中的我的代码是:

DispatcherTimer dt;

public MainWindow()
{
    InitializeComponent();

    _objWindowInteropHelper = new WindowInteropHelper(this);

    dt = new DispatcherTimer();
    dt.Tick += new EventHandler(doActivate);
    dt.Interval = new TimeSpan(0, 0, 5);
}

/// <summary>
/// WindowInteropHelper object that is used to get a Handle to this window. it is used to flash window when user working on other
/// things like email, surfing and at same time message arrive from participant.
/// </summary>
private readonly WindowInteropHelper _objWindowInteropHelper;

private void doActivate(object sender, EventArgs e)
{
    this.GlobalActivate(_objWindowInteropHelper.Handle);
}

protected override void OnDeactivated(EventArgs e)
{
    base.OnDeactivated(e);

    dt.Start();
}

0 个答案:

没有答案