1

We have a .net 4.6.1 C# WPF app that takes up whole screen. We want to be able to turn on/off having a different smaller app's window on top of our main app (so they can chat to our support people). There are several third party tools that do it so I know it is possible:

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

We have tried many things, I can get it to come to the front, but as soon as we touch our app (which takes up whole screen) the one we want to stay on top goes to the back... Saw a few solutions that would force the other app to stay on top, but it would freeze main app... but that is not ideal because we still want to be able to use our app while the other app is open on top of ours...

Any ideas? Thanks

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

EDIT: did attempt a brute force way w/ timer but makes our main app unusable:

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);
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
dan
  • 801
  • 15
  • 41
  • Is your smaller app started from your main app? Or separately? – Mark Hall May 21 '16 at 00:27
  • it was started from our main app, but our main app restarts occasionally... that's why I look up the other app by window caption – dan May 21 '16 at 00:44
  • What solution did you try that froze the main app? It sounds like the solution should involve multithreading. – prospector May 21 '16 at 01:09
  • Put in a checbox and handler that when checked, sets the window to topmost. To get back to normal uncheck the checkbox. Woops I misread the question yes, you'll need to drop to com layer. – JWP May 21 '16 at 01:53
  • thanks for reply but not really sure what you mean... our main app covers whole screen. chat app is very small and takes up a little part of app. so (at times) always want to show that small app in front of main app, and be able to use the main app or small app... i can get small app to show in front but when i click on main app it goes behind the main app.. thanks, Dan – dan May 21 '16 at 17:13
  • Window ownership is the way to do this. It is just more awkward cross process. – David Heffernan May 23 '16 at 06:16
  • 1
    Read this also http://stackoverflow.com/questions/14557050/showing-wpf-window-from-other-process-in-modal-mode and maybe you should reconsider disputing the duplicate closure. Do you know what window ownership means? – David Heffernan May 23 '16 at 06:21
  • Are you the author of the other app? If not, you're probably on a hiding to nothing - because even if you do assume ownership over the window, that other application was probably never written to *expect* that it would be part of a cross-process ownership tree. And then [hilarity ensues](https://blogs.msdn.microsoft.com/oldnewthing/20130412-00/?p=4683) – Damien_The_Unbeliever May 23 '16 at 06:46
  • thanks for comments - yes I know what window ownership means, I didn't think one app was able to own a window in different process. but I don't want the smaller chat app/window to take modal (which that wpf link w/ seems to do), I just want it in front so we can use main app and see/use the smaller chat app. (that's what those 3rd party tools i mentioned above do). that's why using timer to bring to foreground doesn't work - it makes smaller app active and so can't use main app, smaller app keeps getting focus. The app is not written by us it is written by Log Me In. Thank you. – dan May 24 '16 at 21:55

0 Answers0