Solution to the focus problem of C ා wsto window

Original address: https://www.cnblogs.com/cplemom/p/11514668.html Thanks to the original author, let me walk a lot of detours.
Write plug-in pop-up is sure to have, pop-up to show the function of the page ah!
Record some of the pop-up holes you met some time ago. It's also fruitful to step on them one by one!

1. WPF pop up window

The simplest form of pop-up, new a form, and then call the Show method.

Window window = new Window();
window.Show();

Then according to the different requirements, there are some things to adjust, such as whether to display in the taskbar, whether to allow the maximum and minimum, whether to run drag and drop, etc

window.ShowInTaskbar = false;
window.ResizeMode = ResizeMode.NoResize;
window.AllowDrop = false;

It's a crackling operation like above. It's true that the pop-up window is coming out, but there's something wrong with clicking it. Each time you click or focus switches to the main window, the pop-up window will be covered by the main window.
Immediately thought of a solution, set the pop-up window to the top of the ok ah! Don't worry about being covered.

window.Topmost = true;

As expected, even if the focus is switched, the pop-up window is still displayed on the top. emmmm, such a big problem has been solved. It's not too much to secretly touch a fish! Happy tab switch fishing mode. But, what the devil! Why is this pop-up coming! It turns out that it's not just the plug-in page that's on top, all the software is on top!

The correct solution is as follows:

Window window = new Window();
window.Title = "I am WPF Popup!!!!";
new System.Windows.Interop.WindowInteropHelper(window) { Owner = new IntPtr(Globals.ThisAddIn.Application.HWND) };
window.Show();


At this time, you can see that when the focus is on the main window, the pop-up window will not be covered, and when the main window is minimized, the pop-up window will also be closed, and when switching to other software, the pop-up window will be normally covered!

2. Winform pop up

Winform's pop-up window must be similar to that of WPF. After all, it is also a WPF that only existed before Winform. The same two words play a window, but the problem is the same as above!

Form form = new Form();
form.Show();

If you are careful, you can find that when you input the Show method, there is actually an overloaded method. You need to pass in an IWin32Window object. And IWin32Window is actually an interface class, with only one internal property that returns a handle.

//owner: any object that implements System.Windows.Forms.IWin32Window and represents the top-level window that will own this form.
public void Show(IWin32Window owner);

public interface IWin32Window
{
    //Gets the window handle representing the implementer.
    IntPtr Handle { get; }
}

Try to create a class that inherits the IWin32Window interface, and then go to new according to the handle of the main page, and pass it into the Show method as a parameter.

public class WinWrap : IWin32Window
{
    private IntPtr m_Handle;
    public IntPtr Handle
    {
        get { return m_Handle; }
    }
    //Constructor, parameter is the handle of the parent window
    public WinWrap(int handle)
    {
        this.m_Handle = new IntPtr(handle);
    }
}

Form form = new Form();
form.Text = "This is Winform Popup!!!";
WinWrap owner = new WinWrap(Globals.ThisAddIn.Application.HWND);
form.Show(owner);

3. Embedded WPF control in Winform pop-up window

Because some interfaces only allow incoming Winform forms or controls, you need to wrap WPF controls

var toolControl = new Controls.ToolControl();//WPF controls
var form = new System.Windows.Forms.UserControl();//Winform control
System.Windows.Forms.Integration.ElementHost elementHost = new System.Windows.Forms.Integration.ElementHost();
elementHost.Child = toolControl;
elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
form.Controls.Add(elementHost);      
Published 17 original articles, won praise 35, visited 3567
Private letter follow

Tags: Windows

Posted on Fri, 13 Mar 2020 23:53:21 -0400 by machiavelli1079