开发者

MessageBox.Show is not modal in the setup project

开发者 https://www.devze.com 2023-02-15 18:37 出处:网络
I have a VS2010 setup project, and one of it\'s uninstall custom actions shows notifications using MessageBox.Show method, but they sometimes appear to be behind the setup wizard window, which is tota

I have a VS2010 setup project, and one of it's uninstall custom actions shows notifications using MessageBox.Show method, but they sometimes appear to be behind the setup wizard window, which is totally unacceptable. Is there a way to show them on top of the wizard, maybe make them modal? Or should I create custom windows if it is impossible to show message boxes in a always-on-top or modal way? I am not specifying any owners in the messagebox.show method. The custom action calls a method of a class inside dll, not exe file. I tried to find the process with name "msiexec" and mainwindowtitle == my setup project wizard window title, but in开发者_如何学编程 case of uninstall both processes just don't have any window titles and handles, although the uninstall window is displayed!


I should use MessageBoxOptions.DefaultDesktopOnly in the MessageBox.Show method.


This works while installing or uninstalling on Windows XP and 7, as long as you have only one setup process going on:

        NativeWindow nativeWnd = new NativeWindow();
        try
        {
            IntPtr hWnd = (from p in Process.GetProcessesByName("msiexec") where p.MainWindowHandle != IntPtr.Zero select p.MainWindowHandle).SingleOrDefault();
            if (hWnd == IntPtr.Zero)
                MessageBox.Show(message, title, buttons, icon, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            else
            {
                nativeWnd.AssignHandle(hWnd);
                MessageBox.Show(nativeWnd, message, title, buttons, icon);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Exception thrown in ShowModalDlg", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }
        finally { nativeWnd.ReleaseHandle(); }


You can create a Inherited Form (Add new Windows Form -> Inherited Form) or checkout the second constructor (IWin32Window) of MessageBox and assign the owner property. It displays a message box in front of the specified object.


It looks like during uninstall (and also repair), the setup is run by explorer.exe instead of msiexec.exe. So in the solution suggested here, try looking for the setup window in "explorer" process, if it's not found in "msiexec". Note that unlike msiexec, in case of explorer the setup window is not the main window. So you will need to p/invoke EnumWindows and GetWindowThreadProcessId to get the window.

0

精彩评论

暂无评论...
验证码 换一张
取 消