Do not want Form to display over other application windows
I am displaying a new Form from one process by passing the Show method the handle of a Form created in another process. I only want this new Form to display above the passed Form, like a MessageBox.
However, this newly launched Form appears above other application windows, despite:
- Setting Process.WindowStyle.Hidden to the Form-displaying process
- Overriding the ShowWithoutActivation and CreateParams properties of the Form.
- Making sure Form.TopMost is 开发者_Go百科not true
I have checked that the window handle is valid from the second process. Focus is not stolen, however.
Process A:
Pass (Form) window handle to a new Process B via the command line
Process B:
Display a new Form using Form.Show(anotherProcessWindowHandle)
You are battling the rulez of SetForegroundWindow(). Note the long list of "it won't work" in the Remarks section. AllowSetForegroundWindow() is the solution.
To ensure that a window always overlaps another window, display it with the Show() or ShowDialog() override that takes the owner argument. For example:
using (var dlg = new MyMessageBoxForm(ex.Message)) {
dlg.ShowDialog(this);
}
An owned window is always shown on the top of its owner and gets minimized and closed along with its owner.
Making the window of another process the owner of your window is expressly forbidden by the Windows SDK. Windows Forms won't let you do this. There is however an appcompat mode for Windows version 3 apps. Back then, there was no problem doing this, it didn't support threading. P/Invoke SetParent() on your window and pass the window handle of the window from the other process. It breaks the warranty, but tends to work well.
精彩评论