WPF VSTO of pop and Winform

Write plug-pop is sure to have, there is a page to show only pop function ah!
Pop record about some aspects of pit encountered some time ago, one also stepped up rewarding ah!

WPF pop

The easiest way to pop, newa form, and then call the Showmethod.

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

Then according to different needs, there are some things to adjust, something very simple example is displayed in the taskbar, whether to allow the maximum to minimize drag, and so is running

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

As above, meal Pili crash operation. Indeed, pop out a bit - but click it wrong. CPC or switch focus to the main form when the pop main form will be covered.
Immediately thought of a solution, set to pop the top level on ok ah! You do not have to worry about being covered.

window.Topmost = true;

Sure enough to achieve the desired, even if the focus is switched, it is still displayed in the pop top. emmmm, so big a problem is resolved, secretly touch a fish, but sub it! Happy tab to switch modes fishing in troubled waters. But, what the hell! Why this pop kind enough to come ah! The original page is not just a plug-top, and all software are top ah!
The right solution

Window window = new Window();
window.Title = "我是WPF弹窗!!!!";
new System.Windows.Interop.WindowInteropHelper(window) { Owner = new IntPtr(Globals.ThisAddIn.Application.HWND) };
window.Show();

WindowInteropHelper name looked a bit like a class of their own definition, in fact, Microsoft is a helper class to assist interoperable between WPF and Win32 code. The simplest application is the pop-pop of a WPF in Win32.

When this time we can see, the main focus window, the window will not be covered elastic, and to minimize the main window, the pop up and will be received, switch to other software, will normally cover pop !

Winform pop

Winform popups and WPF almost certainly, after all, is to have some of Winform WPF. Like two sentences bombs a window, but the problem is the same as above!

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

Careful that can be found in the input Showmethod when, in fact, there is an overloaded method, you need to pass a IWin32Windowobject. And IWin32Windowin fact is an interface class, inside a property only handle returns.

//owner:任何实现 System.Windows.Forms.IWin32Window 并表示将拥有此窗体的顶级窗口的对象。
public void Show(IWin32Window owner);

public interface IWin32Window
{
    //获取表示由实现者的窗口句柄。
    IntPtr Handle { get; }
}

Try and build a class that inherits IWin32Windowthe interface, and then according to the handle of the main page to look at this new class, and as a parameter passed to inside the Show method.

public class WinWrap : IWin32Window
{
    private IntPtr m_Handle;
    public IntPtr Handle
    {
        get { return m_Handle; }
    }
    //构造函数,参数是父窗口的句柄
    public WinWrap(int handle)
    {
        this.m_Handle = new IntPtr(handle);
    }
}

Form form = new Form();
form.Text = "这是Winform弹窗!!!";
WinWrap owner = new WinWrap(Globals.ThisAddIn.Application.HWND);
form.Show(owner);

to sum up

Sigh and Winform WPF is really the same strain ah!

Guess you like

Origin www.cnblogs.com/cplemom/p/11514668.html
pop