コード例 #1
0
ファイル: MessageBoxEx.cs プロジェクト: zcnet4/lua-tilde
		internal string ShowImpl(IWin32Window window, string text, string title, string [] buttons, MessageBoxIcon icon, string defaultButton)
		{
			System.Diagnostics.Debug.Assert(buttons.Length <= 4 && buttons.Length >= 1);

			mButtons = buttons;
			mButtonMap = new Dictionary<DialogResult, string>();

			int buttonstype;
			switch (buttons.Length)
			{
				case 1:
					buttonstype = Win32.MB_OK;
					mButtonMap[DialogResult.OK] = buttons[0];
					break;

				case 2:
					if (buttons[1] == "Cancel")
					{
						buttonstype = Win32.MB_OKCANCEL;
						mButtonMap[DialogResult.OK] = buttons[0];
						mButtonMap[DialogResult.Cancel] = buttons[1];
					}
					else
					{
						buttonstype = Win32.MB_YESNO;
						mButtonMap[DialogResult.Yes] = buttons[0];
						mButtonMap[DialogResult.No] = buttons[1];
					}
					break;

				case 3:
					if (buttons[2] == "Cancel")
					{
						buttonstype = Win32.MB_YESNOCANCEL;
						mButtonMap[DialogResult.Yes] = buttons[0];
						mButtonMap[DialogResult.No] = buttons[1];
						mButtonMap[DialogResult.Cancel] = buttons[2];
					}
					else
					{
						buttonstype = Win32.MB_ABORTRETRYIGNORE;
						mButtonMap[DialogResult.Abort] = buttons[0];
						mButtonMap[DialogResult.Retry] = buttons[1];
						mButtonMap[DialogResult.Ignore] = buttons[2];
					}
					break;

				case 4:
					if (buttons[3] == "Cancel")
					{
						buttonstype = Win32.MB_ABORTRETRYIGNORE + Win32.MB_HELP;
						mButtonMap[DialogResult.Abort] = buttons[0];
						mButtonMap[DialogResult.Retry] = buttons[1];
						mButtonMap[DialogResult.Ignore] = buttons[2];
						mButtonMap[DialogResult.Cancel] = buttons[3];
					}
					else
					{
						buttonstype = Win32.MB_ABORTRETRYIGNORE + Win32.MB_HELP;
						mButtonMap[DialogResult.Abort] = buttons[0];
						mButtonMap[DialogResult.Retry] = buttons[1];
						mButtonMap[DialogResult.Ignore] = buttons[2];
						mButtonMap[DialogResult.OK] = buttons[3];
					}
					break;

				default:
					throw new ApplicationException("MessageBoxEx can only present 4 buttons");
			}

			int defaultFlag = 0;
			for(int index = 0; index < buttons.Length; ++index)
			{
				if (buttons[index] == defaultButton)
					defaultFlag = mDefaultFlags[index];
			}

			DialogResult result = DialogResult.None;

			// Make sure the hook gets uninstalled in the event of an exception
			using(WindowsHook mWindowsHook = new WindowsHook())
			{
				mWindowsHook.WindowCreated += new WindowsHook.WindowsEventHandler(WndCreated);
				mWindowsHook.WindowDestroyed += new WindowsHook.WindowsEventHandler(WndDestroyed);
				mWindowsHook.WindowActivated += new WindowsHook.WindowsEventHandler(WndActivated);
				mWindowsHook.Install();

				// This call to Win32.MessageBox blocks until the dialog has been closed
				result = (DialogResult) Win32.MessageBox(window.Handle, text, title, buttonstype + (int)icon + defaultFlag);
			}

			string btnresult;
			if(mButtonMap.TryGetValue(result, out btnresult))
				return btnresult;
			else
				throw new ApplicationException("Unexpected return from Win32.MessageBox() call: " + result.ToString());
		}
コード例 #2
0
        internal string ShowImpl(IWin32Window window, string text, string title, string [] buttons, MessageBoxIcon icon, string defaultButton)
        {
            System.Diagnostics.Debug.Assert(buttons.Length <= 4 && buttons.Length >= 1);

            mButtons   = buttons;
            mButtonMap = new Dictionary <DialogResult, string>();

            int buttonstype;

            switch (buttons.Length)
            {
            case 1:
                buttonstype = Win32.MB_OK;
                mButtonMap[DialogResult.OK] = buttons[0];
                break;

            case 2:
                if (buttons[1] == "Cancel")
                {
                    buttonstype = Win32.MB_OKCANCEL;
                    mButtonMap[DialogResult.OK]     = buttons[0];
                    mButtonMap[DialogResult.Cancel] = buttons[1];
                }
                else
                {
                    buttonstype = Win32.MB_YESNO;
                    mButtonMap[DialogResult.Yes] = buttons[0];
                    mButtonMap[DialogResult.No]  = buttons[1];
                }
                break;

            case 3:
                if (buttons[2] == "Cancel")
                {
                    buttonstype = Win32.MB_YESNOCANCEL;
                    mButtonMap[DialogResult.Yes]    = buttons[0];
                    mButtonMap[DialogResult.No]     = buttons[1];
                    mButtonMap[DialogResult.Cancel] = buttons[2];
                }
                else
                {
                    buttonstype = Win32.MB_ABORTRETRYIGNORE;
                    mButtonMap[DialogResult.Abort]  = buttons[0];
                    mButtonMap[DialogResult.Retry]  = buttons[1];
                    mButtonMap[DialogResult.Ignore] = buttons[2];
                }
                break;

            case 4:
                if (buttons[3] == "Cancel")
                {
                    buttonstype = Win32.MB_ABORTRETRYIGNORE + Win32.MB_HELP;
                    mButtonMap[DialogResult.Abort]  = buttons[0];
                    mButtonMap[DialogResult.Retry]  = buttons[1];
                    mButtonMap[DialogResult.Ignore] = buttons[2];
                    mButtonMap[DialogResult.Cancel] = buttons[3];
                }
                else
                {
                    buttonstype = Win32.MB_ABORTRETRYIGNORE + Win32.MB_HELP;
                    mButtonMap[DialogResult.Abort]  = buttons[0];
                    mButtonMap[DialogResult.Retry]  = buttons[1];
                    mButtonMap[DialogResult.Ignore] = buttons[2];
                    mButtonMap[DialogResult.OK]     = buttons[3];
                }
                break;

            default:
                throw new ApplicationException("MessageBoxEx can only present 4 buttons");
            }

            int defaultFlag = 0;

            for (int index = 0; index < buttons.Length; ++index)
            {
                if (buttons[index] == defaultButton)
                {
                    defaultFlag = mDefaultFlags[index];
                }
            }

            DialogResult result = DialogResult.None;

            // Make sure the hook gets uninstalled in the event of an exception
            using (WindowsHook mWindowsHook = new WindowsHook())
            {
                mWindowsHook.WindowCreated   += new WindowsHook.WindowsEventHandler(WndCreated);
                mWindowsHook.WindowDestroyed += new WindowsHook.WindowsEventHandler(WndDestroyed);
                mWindowsHook.WindowActivated += new WindowsHook.WindowsEventHandler(WndActivated);
                mWindowsHook.Install();

                // This call to Win32.MessageBox blocks until the dialog has been closed
                result = (DialogResult)Win32.MessageBox(window.Handle, text, title, buttonstype + (int)icon + defaultFlag);
            }

            string btnresult;

            if (mButtonMap.TryGetValue(result, out btnresult))
            {
                return(btnresult);
            }
            else
            {
                throw new ApplicationException("Unexpected return from Win32.MessageBox() call: " + result.ToString());
            }
        }