2

I have a small WPF application, say SubWPF , which initially displays a home window that allows the user to browse a file. I am launching this wpf application on the button click of another application say MainWPF. In short, when I click on "Launch Sub", SubWPF.exe is launched from MainWPF through Process class as given below.

Process p = new Process();
p.StartInfo.FileName = "SubWPF.exe";
       
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Verb = "runas";
p.Start();

The issue is in the background I have the MainWPF window. I need the subwpf to be always on top and prevent the user from clicking background buttons of mainwpf. To be precise, the behaviour of a showdialog() needs to be achieved. I cannot use TopMost=true for my subwpf as once the user selects a file in the home window, this window is closed and another window showing the contents of file is displayed. Setting TopMost=true is not allowing me to close the file selection window.

James Z
  • 12,209
  • 10
  • 24
  • 44
user3323130
  • 109
  • 1
  • 8

1 Answers1

1

Exactly modality you will not reach with this answer, but you will be able to block a main window for input, if you maximize your sub window to the size of parent(got the idea right now - you could make a transparent background and put your actual business control to the center of sub window).
One more pitfall is a possibility to resize main window - to bypass it you could try to change temporary the main window style: Modify the windows style of another application using winAPI.
Alternative to maximizing you could capture the mouse and on every click do move sub window to the top, or call the developer team and do as Leonid Malyshev offer in the comment. So you will need to put some logic to the Window.Loaded event handler of sub window end set WindowStyle="None" for the sub window:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

private void SubWindow_Loaded(object sender, RoutedEventArgs e)
{
    var p = Process.GetProcessesByName("YourMainWindow")[0];

    SetParent(new WindowInteropHelper(this).Handle, p.MainWindowHandle);

    WindowState = WindowState.Maximized; //Does maximize sub window to the size of parent window.

    IntPtr HWND_TOPMOST = new IntPtr(-1);
    const short SWP_NOACTIVATE = 0x0010;

    SetWindowPos(new WindowInteropHelper(this).Handle, p.MainWindowHandle, 0, 0, (int)Width, (int)Height, SWP_NOACTIVATE);
}
Rekshino
  • 6,954
  • 2
  • 19
  • 44