1

I am writing an application in WPF that attaches to skype, moves with it and is sent to back/front with it. The only thing I am missing is whenever skype is brought to front, I need my WPF application to be sent to front with it.

The way I was thinking of doing is, detecting when Skype goes to the top most (in Z-Order) on my desktop and sending my application to front with it.

This is what I have: For connecting to the process

public List<Process> skypeProcess;
public IntPtr ptr;
public Rect skypeRect;

public SkypeWindowPosition()
{
    skypeProcess = Process.GetProcessesByName("skype").ToList();

    ptr = skypeProcess[0].MainWindowHandle;
    SetForegroundWindow(ptr);
    skypeRect = new Rect();
    GetWindowRect(ptr, ref skypeRect);
}

I wanted to use GetTopWindow from user32.dll but it doesn't work the way I want it to work.

I tried:

if (lyncWin.ptr == LyncWindowPosition.GetTopWindow(new IntPtr(0)))
{
    Console.WriteLine("Yes");
}
else
{
    Console.WriteLine("No");
}

But even when skype is on top, it evaluates to "No".

I also have:

int GetZOrder(IntPtr hWnd)
{
    var z = 0;
    for (var skypeHandle = hWnd; skypeHandle != IntPtr.Zero; skypeHandle = LyncWindowPosition.GetWindow(skypeHandle, 3)) z++;
    return z;
}

Which tells me the Z position of skype, but Z would be different depending on how many windows are active.

Any help on figuring out how to determine if skype is on top, or any better way to do it would be appreciated.

Rafael
  • 727
  • 13
  • 30
  • I forgot to add, I also checked everywhere on SO, I couldn't find something similiar to this that had the appropriate answer or an answer at all. – Rafael Mar 27 '17 at 16:05
  • See this: http://stackoverflow.com/questions/9665579/setting-up-hook-on-windows-messages There are a number of events you can hook to here https://msdn.microsoft.com/en-us/library/windows/desktop/dd318066(v=vs.85).aspx For instance you probably want `EVENT_SYSTEM_MINIMIZEEND` – TyCobb Mar 27 '17 at 16:17
  • I will try that, @TyCobb – Rafael Mar 27 '17 at 17:30
  • @TyCobb, What are the values for the Flags in SetWinEventHook? They don't show at MSDN https://msdn.microsoft.com/en-us/library/windows/desktop/dd373640(v=vs.85).aspx – Rafael Mar 27 '17 at 18:06
  • Nvm I found them at https://source.winehq.org/source/include/winuser.h – Rafael Mar 27 '17 at 18:08

1 Answers1

0

I solved my own question:

Turns out I should have used GetForegroundWindow()

So I did:

if (LyncWindowPosition.GetForegroundWindow().GetHashCode() == lyncWin.ptr.GetHashCode())
{
    window.Topmost = true;
    window.Topmost = false;
}
Rafael
  • 727
  • 13
  • 30