Пример #1
0
        private static DialogResult ShowCore(IWin32Window owner, string text, string caption,
                                             MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton,
                                             MessageBoxOptions options, bool showHelp)
        {
            MB style = GetMessageBoxStyle(owner, buttons, icon, defaultButton, options, showHelp);

            IntPtr handle = IntPtr.Zero;

            if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0))
            {
                if (owner is null)
                {
                    handle = GetActiveWindow();
                }
                else
                {
                    handle = Control.GetSafeHandle(owner);
                }
            }

            IntPtr userCookie = IntPtr.Zero;

            if (Application.UseVisualStyles)
            {
                // CLR4.0 or later, shell32.dll needs to be loaded explicitly.
                if (Kernel32.GetModuleHandleW(Libraries.Shell32) == IntPtr.Zero)
                {
                    if (Kernel32.LoadLibraryFromSystemPathIfAvailable(Libraries.Shell32) == IntPtr.Zero)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(lastWin32Error, string.Format(SR.LoadDLLError, Libraries.Shell32));
                    }
                }

                // Activate theming scope to get theming for controls at design time and when hosted in browser.
                // NOTE: If a theming context is already active, this call is very fast, so shouldn't be a perf issue.
                userCookie = ThemingScope.Activate(Application.UseVisualStyles);
            }

            Application.BeginModalMessageLoop();
            try
            {
                return((DialogResult)MessageBoxW(new HandleRef(owner, handle), text, caption, style));
            }
            finally
            {
                Application.EndModalMessageLoop();
                ThemingScope.Deactivate(userCookie);

                // Right after the dialog box is closed, Windows sends WM_SETFOCUS back to the previously active control
                // but since we have disabled this thread main window the message is lost. So we have to send it again after
                // we enable the main window.
                User32.SendMessageW(new HandleRef(owner, handle), User32.WM.SETFOCUS);
            }
        }
Пример #2
0
        private ImageListStreamer(SerializationInfo info, StreamingContext context)
        {
            SerializationInfoEnumerator sie = info.GetEnumerator();

            if (sie is null)
            {
                return;
            }

            while (sie.MoveNext())
            {
                if (string.Equals(sie.Name, "Data", StringComparison.OrdinalIgnoreCase))
                {
#if DEBUG
                    try
                    {
#endif
                    byte[] dat = (byte[])sie.Value;
                    if (dat != null)
                    {
                        // We enclose this imagelist handle create in a theming scope.
                        IntPtr userCookie = ThemingScope.Activate(Application.UseVisualStyles);

                        try
                        {
                            using MemoryStream ms = new MemoryStream(Decompress(dat));
                            lock (internalSyncObject)
                            {
                                ComCtl32.InitCommonControls();
                                nativeImageList = new ImageList.NativeImageList(new Ole32.GPStream(ms));
                            }
                        }
                        finally
                        {
                            ThemingScope.Deactivate(userCookie);
                        }

                        if (nativeImageList.Handle == IntPtr.Zero)
                        {
                            throw new InvalidOperationException(SR.ImageListStreamerLoadFailed);
                        }
                    }
#if DEBUG
                }
                catch (Exception e)
                {
                    Debug.Fail("ImageList serialization failure: " + e.ToString());
                    throw;
                }
#endif
                }
            }
        }
Пример #3
0
        private static DialogResult ShowCore(IWin32Window owner, string text, string caption,
                                             MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton,
                                             MessageBoxOptions options, bool showHelp)
        {
            if (!ClientUtils.IsEnumValid(buttons, (int)buttons, (int)MessageBoxButtons.OK, (int)MessageBoxButtons.RetryCancel))
            {
                throw new InvalidEnumArgumentException(nameof(buttons), (int)buttons, typeof(MessageBoxButtons));
            }

            // valid values are 0x0 0x10 0x20 0x30 0x40, chop off the last 4 bits and check that it's between 0 and 4.
            if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(icon, /*numBitsToShift*/ 4, /*min*/ 0x0, /*max*/ 0x4))
            {
                throw new InvalidEnumArgumentException(nameof(icon), (int)icon, typeof(MessageBoxIcon));
            }
            // valid values are 0x0 0x100, 0x200, chop off the last 8 bits and check that it's between 0 and 2.
            if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(defaultButton, /*numBitsToShift*/ 8, /*min*/ 0x0, /*max*/ 0x2))
            {
                throw new InvalidEnumArgumentException(nameof(defaultButton), (int)defaultButton, typeof(DialogResult));
            }

            // options intentionally not verified because we don't expose all the options Win32 supports.

            if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0)
            {
                throw new InvalidOperationException(SR.CantShowModalOnNonInteractive);
            }
            if (owner != null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
            {
                throw new ArgumentException(SR.CantShowMBServiceWithOwner, nameof(options));
            }
            if (showHelp && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
            {
                throw new ArgumentException(SR.CantShowMBServiceWithHelp, nameof(options));
            }

            MB style = (showHelp) ? MB.HELP : 0;

            style |= (MB)buttons | (MB)icon | (MB)defaultButton | (MB)options;

            IntPtr handle = IntPtr.Zero;

            if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0))
            {
                if (owner == null)
                {
                    handle = GetActiveWindow();
                }
                else
                {
                    handle = Control.GetSafeHandle(owner);
                }
            }

            IntPtr userCookie = IntPtr.Zero;

            if (Application.UseVisualStyles)
            {
                // CLR4.0 or later, shell32.dll needs to be loaded explicitly.
                if (Kernel32.GetModuleHandleW(Libraries.Shell32) == IntPtr.Zero)
                {
                    if (Kernel32.LoadLibraryFromSystemPathIfAvailable(Libraries.Shell32) == IntPtr.Zero)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(lastWin32Error, string.Format(SR.LoadDLLError, Libraries.Shell32));
                    }
                }

                // Activate theming scope to get theming for controls at design time and when hosted in browser.
                // NOTE: If a theming context is already active, this call is very fast, so shouldn't be a perf issue.
                userCookie = ThemingScope.Activate(Application.UseVisualStyles);
            }

            Application.BeginModalMessageLoop();
            DialogResult result;

            try
            {
                result = Win32ToDialogResult(MessageBoxW(new HandleRef(owner, handle), text, caption, style));
            }
            finally
            {
                Application.EndModalMessageLoop();
                ThemingScope.Deactivate(userCookie);
            }

            // Right after the dialog box is closed, Windows sends WM_SETFOCUS back to the previously active control
            // but since we have disabled this thread main window the message is lost. So we have to send it again after
            // we enable the main window.
            User32.SendMessageW(new HandleRef(owner, handle), User32.WM.SETFOCUS);
            return(result);
        }