WPF:Win32 Hooks(三)让MessageBox具有线程安全性

同系列文章:

/* ----------------------------------------------------------
 * 文件名称:MessageBoxPlus.cs
 * 
 * 作者:秦建辉
 * 
 * MSN:splashcn@msn.com
 * QQ:36748897
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio V2010
 *      .NET Framework 4 Client Profile
 * 
 * 版本历史:
 *      V1.1	2012年07月27日		
 *              增加线程安全性支持
 *              
 *      V1.0	2012年06月13日
 *              WPF版本,让MessageBox自动显示在父窗体中心
------------------------------------------------------------ */
using System;
using System.Windows;

namespace Splash.Windows
{
    public class MessageBoxPlus
    {
        public static MessageBoxResult Show(Window owner, String messageBoxText)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText);
                }));
            }
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText, caption);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText, caption);
                }));
            }            
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText, caption, button);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText, caption, button);
                }));
            }            
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText, caption, button, icon);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText, caption, button, icon);
                }));
            }            
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult);
                }));
            }
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
        {
            if (owner.Dispatcher.CheckAccess())
            {
                owner.CenterChild();
                return MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
            }
            else
            {
                return (MessageBoxResult)owner.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    owner.CenterChild();
                    return MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
                }));
            }
        }
    }
}

Comments are closed.