I have an application that creates a separate window running on it's own dispatcher (the reason for this is complicated, but sadly required)
What I want is for this separate window to ALWAYS be displayed above the application window. From what I have read, this should be possible using pinvoke and SetWindowLong, but I can't seem to get it to work. I don't get any exceptions, but the separate window doesn't stay on top of the application window. It goes behind the parent when you select the parent window.
And just to mention, I'm trying to avoid using TopMost as I only want it to be above the parent window, not all other windows.
Any ideas what I am doing wrong?
internal class WindowHelper
{
private const int GWL_HWNDPARENT = -8;
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int index, int newStyle);
public static void SetOwnerWindow(IntPtr hwndOwned, IntPtr intPtrOwner)
{
try
{
if (hwndOwned != IntPtr.Zero && intPtrOwner != IntPtr.Zero)
{
SetWindowLong(hwndOwned, GWL_HWNDPARENT, intPtrOwner.ToInt32());
}
}
catch { }
}
}
public partial class MainWindow : Window
{
IntPtr parentHandle = IntPtr.Zero;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
parentHandle = new WindowInteropHelper(this).Handle;
CreateNewWindow();
}
private void CreateNewWindow()
{
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
}
private void ThreadStartingPoint()
{
var tempWindow = new Window();
tempWindow.Show();
IntPtr childHandle = new WindowInteropHelper(tempWindow).Handle;
WindowHelper.SetOwnerWindow(childHandle, parentHandle);
System.Windows.Threading.Dispatcher.Run();
}
}
Turns out I needed to use the 64bit version of
SetWindowLonghttp://pinvoke.net/default.aspx/user32/SetWindowLongPtr.html