Custom Installer in .Net showing Form behind insta

2020-02-01 05:08发布

[RunInstaller(true)]
public partial class Installer1 : Installer
{
    public Installer1()
    {
        InitializeComponent();
    }

    public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);
    }
    private void Installer1_AfterInstall(object sender, InstallEventArgs e)
    {
        Form1 topmostForm = new Form1();
        topmostForm.BringToFront();
        topmostForm.TopMost = true;            
        topmostForm.ShowDialog();
  } }

I need to display the topmostForm in front of the default Windows Installer UI. The above is sample code inside my CustomAction that I am using to create a Form. Setting the TopMost property or using ShowDialog is not helping. Is there any other solution to make my form the top most and focussed?

10条回答
▲ chillily
3楼-- · 2020-02-01 05:33

You can use form option TopMost and Focus method. But there much better way. You can get installer process, then get it window handler and then use it as parameter in ShowDialog method::

var proc = Process.GetProcessesByName("msiexec").FirstOrDefault(p => p.MainWindowTitle == "Name of product");
var formResult = proc != null
  ? form.ShowDialog(new WindowWrapper(proc.MainWindowHandle))
  : form.ShowDialog();

WindowWrapper is something like this:

public class WindowWrapper : IWin32Window
{
  private readonly IntPtr hwnd;
  public IntPtr Handle {
    get { return hwnd; }
  }
  public WindowWrapper(IntPtr handle) {
    hwnd = handle;
  }
}
查看更多
冷血范
4楼-- · 2020-02-01 05:34

If you want to have complete control over installer user interface for branding or custom dialogs and don't want to use installer builder software like InstallShield then you can create a C++ application to serve as shell for Windows Installer - there is no need to implement installer actions such as copying files by yourself.

Windows Installer has API for such purpose. With function MsiSetExternalUIRecord you can provide a callback to capture installer notifications such as messages and progress updates.

查看更多
贼婆χ
5楼-- · 2020-02-01 05:39

Call this.focus() in your form.OnLoad method. That makes it show up in front of the installer. Simple fix.

查看更多
登录 后发表回答