强制不同的应用程序在顶部C#

时间:2016-05-21 00:13:08

标签: c# wpf pinvoke

我们有一个占用整个屏幕的.net 4.6.1 C#WPF应用程序。我们希望能够在我们的主应用程序之上打开/关闭不同的小应用程序窗口(这样他们就可以与我们的支持人员聊天)。有几个第三方工具,所以我知道这是可能的:

http://www.makeuseof.com/tag/5-simple-ways-selected-windows-top-windows/

我们已经尝试了很多东西,我可以让它来到前面,但是一旦我们触摸我们的应用程序(占据整个屏幕),我们想要留在顶部的那个就到了后面...... Saw一些解决方案会迫使其他应用程序保持领先,但它会冻结主应用程序......但这并不理想,因为我们仍然希望能够使用我们的应用程序而其他应用程序在我们的应用程序之上打开。 ..

有什么想法吗?感谢

    public static void SearchForRunningProcessByWindow(string windowCaption, ShowWindowState windowState = ShowWindowState.SW_SHOWNORMAL)
    {
        IntPtr hWnd = FindWindowByCaption(0, windowCaption);
        if (hWnd != IntPtr.Zero)
        {
            ShowWindow(hWnd, (int)ShowWindowState.SW_SHOWNORMAL);
            SetForegroundWindow(hWnd);

            // this is supposed to make it stay on top... ?
            SetWindowPos(hWnd, new IntPtr(-1), 0, 0, 0, 0, 0x4000 | 0x0002 | 0x0001);
            return true;
        }
    }
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
编辑:尝试使用计时器的强力方式但是使我们的主要应用程序无法使用:

private static Timer _timer;
static void ForceOnTop(string windowCaption)
{
    var timeout = 250;
    _timer = new Timer(TimerCallback, new Tuple<long, string>(timeout, windowCaption), timeout, Timeout.Infinite);
}

private static void TimerCallback(object state)
{
    var @tuple = (Tuple<long, string>) state;
    SearchForRunningProcessByWindow(@tuple.Item2);
    _timer.Change(@tuple.Item1, Timeout.Infinite);
}

0 个答案:

没有答案