Пример #1
0
        private LinkClickedEventArgs ExecuteClickOnLink(int characterIndex)
        {
            Point previousPosition = new Point();
            BOOL  setOldCursorPos  = GetPhysicalCursorPos(ref previousPosition);

            LinkClickedEventArgs    result  = null;
            LinkClickedEventHandler handler = (sender, e) => result = e;

            _control.LinkClicked += handler;

            try
            {
                Point pt = _control.PointToScreen(_control.GetPositionFromCharIndex(characterIndex));

                // Adjust point a bit to make sure we are clicking inside the character cell instead of on its edge.
                MouseHelper.SendClick(pt.X + 2, pt.Y + 2);

                // Let the event queue drain after clicking, before moving the cursor back to the old position.
                Application.DoEvents();
            }
            finally
            {
                _control.LinkClicked -= handler;

                if (setOldCursorPos.IsTrue())
                {
                    // Move cursor to old position
                    MouseHelper.ChangeMousePosition(previousPosition.X, previousPosition.Y);
                    Application.DoEvents();
                }
            }

            return(result);
        }
Пример #2
0
        protected virtual void OnLinkClicked(LinkClickedEventArgs e)
        {
            LinkClickedEventHandler eh = (LinkClickedEventHandler)(Events[LinkClickedEvent]);

            if (eh != null)
            {
                eh(this, e);
            }
        }
Пример #3
0
        /// <summary>
        /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// linkclickedeventhandler.BeginInvoke(sender, e, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginInvoke(this LinkClickedEventHandler linkclickedeventhandler, Object sender, LinkClickedEventArgs e, AsyncCallback callback)
        {
            if (linkclickedeventhandler == null)
            {
                throw new ArgumentNullException("linkclickedeventhandler");
            }

            return(linkclickedeventhandler.BeginInvoke(sender, e, callback, null));
        }
Пример #4
0
        /// <summary>
        /// Displays the FlexibleMessageBox using the contents of the clipboard (if text is available on it) or
        /// some standard text. The localization (French vs. a custom pseudo-localization) and the version of the
        /// Show method (which buttons, icon, etc.) are randomized. This makes it easy to see fairly quickly what
        /// it would look like with several different options, but it makes it impossible to repeatedly test a
        /// particular option over and over. I thought this was probably faster and more useful than designing a
        /// whole UI that would allow the tester to configure which options would be used, but that could certainly
        /// be done later if it proved expedient.
        /// </summary>
        private void btnFlexibleMessageBox_Click(object sender, EventArgs e)
        {
            var random = new Random(DateTime.Now.Millisecond);

            if (random.Next(0, 6) == 5)
            {
                var LocalizedButtonText = FlexibleMessageBox.GetButtonTextLocalizationKeys.ToDictionary(k => k.Key, v => v.Value + " Loc");
                FlexibleMessageBox.GetButtonText = id => LocalizedButtonText[id];
            }
            else
            {
                FlexibleMessageBox.GetButtonText = null;
            }

            string caption;
            string msg;

            try
            {
                string clipboardText = Clipboard.GetText();
                if (clipboardText == String.Empty)
                {
                    throw new ApplicationException("This is fine. It will display the default caption and message");
                }
                var data = clipboardText.Split(new [] { '\n' }, 2);
                if (data.Length == 1)
                {
                    caption = "Flexible Message Box test caption";
                    msg     = clipboardText;
                }
                else
                {
                    caption = data[0];
                    msg     = data[1];
                }
            }
            catch (Exception)
            {
                caption = Text;
                msg     = "If you don't like this message, copy some text to your clipboard. If it's more than\n" +
                          "one line, the first line will be used as the caption of the flexible message box.";
            }

            LinkClickedEventHandler handler = msg.Contains("http:") ? (LinkClickedEventHandler)((s, args) =>
            {
                FlexibleMessageBox.Show("Link clicked: " + args.LinkText);
            }) : null;

            while (ShowFlexibleMessageBox(random.Next(0, 10), msg, caption, GetDefaultButton(random.Next(0, 3)), handler))
            {
                ;
            }
        }
Пример #5
0
        public RichTextBoxEx() : base()
        {
            // Otherwise, non-standard links get lost when user starts typing
            // next to a non-standard link
            DetectUrls   = false;
            ReadOnly     = true;
            ScrollBars   = RichTextBoxScrollBars.None;
            LinkClicked += new LinkClickedEventHandler(HandelLinkClicked);

            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the RichTextBoxEx class.
 /// </summary>
 public RichTextBoxEx()
 {
     if (ffInstalledFonts == null)
     {
         ffInstalledFonts = new System.Drawing.Text.InstalledFontCollection().Families;
     }
     AutoWordSelection = true;
     AcceptsTab        = true;
     DetectUrls        = true;
     LinkClicked      += new LinkClickedEventHandler(EhLinkClicked);
     Multiline         = true;
     KeyUp            += EhKeyUp;
     WordWrap          = true;
 }
Пример #7
0
        internal virtual void ug59t46kfjbmek2w5288yuf26cremrkl\u200A\u2009\u2000\u2005\u0002(RichTextBox \u0002)
        {
            EventHandler            value  = new EventHandler(this.\u0008);
            LinkClickedEventHandler value2 = new LinkClickedEventHandler(this.\u0002);

            if (this.\u0003 != null)
            {
                this.\u0003.TextChanged -= value;
                this.\u0003.LinkClicked -= value2;
            }
            this.\u0003 = \u0002;
            if (this.\u0003 != null)
            {
                this.\u0003.TextChanged += value;
                this.\u0003.LinkClicked += value2;
            }
        }
Пример #8
0
        void Init()
        {
            AcceptsTab   = true;
            LinkClicked += new LinkClickedEventHandler(HunterRichTextBox_LinkClicked);
            WindowsAPI.BeginPaint(this);
            SuspendLayout();
            BackColor    = System.Drawing.Color.FromArgb(((int)(((byte)(39)))), ((int)(((byte)(40)))), ((int)(((byte)(34)))));
            Font         = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            ForeColor    = System.Drawing.Color.White;
            Size         = new System.Drawing.Size(728, 475);
            ScrollBars   = RichTextBoxScrollBars.Vertical;
            base.Margin  = new Padding(0);
            base.ImeMode = ImeMode.On;
            ResumeLayout(false);
            WindowsAPI.EndPaint(this);

            TextChanged += new EventHandler(HunterRichTextBox_TextChanged);
        }
 /// <summary>
 /// Shows the specified message box.
 /// </summary>
 /// <param name="owner">The owner.</param>
 /// <param name="text">The text.</param>
 /// <param name="caption">The caption.</param>
 /// <param name="buttons">The buttons.</param>
 /// <param name="icon">The icon.</param>
 /// <param name="defaultButton">The default button.</param>
 /// <param name="linkClickedAction">optional handler if user clicks a hyperlink (as determined by RichTextBox). Set to
 /// <seealso cref="BasicLinkClickedEventHandler"/> to get basic handling. If <c>null</c>, URLs will not be detected or
 /// highlighted in the message.</param>
 /// <returns>The dialog result.</returns>
 /// <returns>The dialog result.</returns>
 /// <exception cref="Exception">Exceptions might be thrown by the <paramref name="linkClickedAction"/></exception>
 internal static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons,
                                   MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, LinkClickedEventHandler linkClickedAction = null)
 {
     // Create a new instance of the FlexibleMessageBox form
     using (var flexibleMessageBoxForm = new FlexibleMessageBoxForm(owner, text, caption, buttons,
                                                                    icon, defaultButton, linkClickedAction))
         return(flexibleMessageBoxForm.ShowDialog(owner));
 }
Пример #10
0
            /// <summary>
            /// Initializes a new instance of the <see cref="FlexibleMessageBoxForm"/> class.
            /// </summary>
            private FlexibleMessageBoxForm(IWin32Window owner, string text, string caption, MessageBoxButtons buttons,
                                           MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, LinkClickedEventHandler linkClickedAction)
            {
                InitializeComponent();
                KeyPreview = true;
                KeyUp     += FlexibleMessageBoxForm_KeyUp;

                ShowInTaskbar = false;
                // Bind the caption and the message text
                CaptionText = caption;
                MessageText = text;
                FlexibleMessageBoxFormBindingSource.DataSource = this;
                // Set the buttons visibilities and texts. Also set a default button.
                SetDialogButtons(buttons);
                //Set default button (used to set focus when the dialog is shown)
                _defaultButton = defaultButton;
                // Set the dialog's icon. When no icon is used, correct placement and width of rich text box.
                SetDialogIcon(icon);
                // Set the font for all controls
                richTextBoxMessage.Font = Font = FlexibleMessageBox.Font;
                if (linkClickedAction == null)
                {
                    richTextBoxMessage.DetectUrls = false;
                }
                else
                {
                    richTextBoxMessage.LinkClicked += linkClickedAction;
                }

                var screen = owner == null?Screen.FromPoint(Cursor.Position) : Screen.FromHandle(owner.Handle);

                // Calculate the dialog's start size (Try to auto-size width to show longest text row). Also set the maximum dialog size.
                SetDialogSize(text, caption, screen);
                // If an owning window was supplied, the message box dialog is initially displayed using the default "Center Parent".
                // Otherwise, we center it on the current screen.
                if (owner == null)
                {
                    CenterDialogOnScreen(screen);
                }
            }
Пример #11
0
 /// <summary>
 /// Shows the specified message box.
 /// </summary>
 /// <param name="owner">The owner.</param>
 /// <param name="text">The text.</param>
 /// <param name="caption">The caption.</param>
 /// <param name="buttons">The buttons.</param>
 /// <param name="icon">The icon.</param>
 /// <param name="defaultButton">The default button.</param>
 /// <param name="linkClickedAction">optional handler if user clicks a hyperlink (as determined by RichTextBox). Set to
 /// <seealso cref="BasicLinkClickedEventHandler"/> to get basic handling. If <c>null</c>, URLs will not be detected or
 /// highlighted in the message.</param>
 /// <returns>The dialog result.</returns>
 /// <returns>The dialog result.</returns>
 public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons,
                                 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, LinkClickedEventHandler linkClickedAction = null)
 {
     return(FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton, linkClickedAction));
 }
Пример #12
0
 /// <summary>
 /// Shows the specified message box.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="caption">The caption.</param>
 /// <param name="buttons">The buttons.</param>
 /// <param name="icon">The icon.</param>
 /// <param name="linkClickedAction">optional handler if user clicks a hyperlink (as determined by RichTextBox). Set to
 /// <seealso cref="BasicLinkClickedEventHandler"/> to get basic handling. If <c>null</c>, URLs will not be detected or
 /// highlighted in the message.</param>
 /// <returns>The dialog result.</returns>
 /// <returns></returns>
 public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, LinkClickedEventHandler linkClickedAction = null)
 {
     return(FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, linkClickedAction));
 }
Пример #13
0
 /// <summary>
 /// Shows the specified message box.
 /// </summary>
 /// <param name="owner">The owner.</param>
 /// <param name="text">The text.</param>
 /// <param name="linkClickedAction">optional handler if user clicks a hyperlink (as determined by RichTextBox). Set to
 /// <seealso cref="BasicLinkClickedEventHandler"/> to get basic handling. If <c>null</c>, URLs will not be detected or
 /// highlighted in the message.</param>
 /// <returns>The dialog result.</returns>
 /// <returns>The dialog result.</returns>
 public static DialogResult Show(IWin32Window owner, string text, LinkClickedEventHandler linkClickedAction = null)
 {
     return(FlexibleMessageBoxForm.Show(owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, linkClickedAction));
 }
Пример #14
0
        private bool ShowFlexibleMessageBox(int option, string msg, string caption, MessageBoxDefaultButton defaultButton, LinkClickedEventHandler handler)
        {
            switch (option)
            {
            case 0:
                FlexibleMessageBox.Show(this, msg, handler);
                break;

            case 1:
                FlexibleMessageBox.Show(this, msg, caption, handler);
                break;

            case 2:
                FlexibleMessageBox.Show(this, msg, caption, MessageBoxButtons.OKCancel, handler);
                break;

            case 3:
                msg += "\nClick Retry to display another version of the message box.";
                return(FlexibleMessageBox.Show(this, msg, caption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, handler) == DialogResult.Retry);

            case 4:
                msg += "\nWould you like to display another version of the message box?";
                return(FlexibleMessageBox.Show(this, msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                               defaultButton, handler) == DialogResult.Yes);

            case 5:
                FlexibleMessageBox.Show(msg, handler);
                break;

            case 6:
                FlexibleMessageBox.Show(msg, caption, handler);
                break;

            case 7:
                msg += "\nClick Retry to display another version of the message box.";
                return(FlexibleMessageBox.Show(msg, caption, MessageBoxButtons.RetryCancel, handler) == DialogResult.Retry);

            case 8:
                FlexibleMessageBox.Show(msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop, handler);
                break;

            default:
                msg += "\nWould you like to display another version of the message box?";
                return(FlexibleMessageBox.Show(msg, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
                                               defaultButton, handler) == DialogResult.Yes);
            }
            return(false);
        }
Пример #15
0
        public async Task RichTextBox_Click_On_Custom_Link_Preceeded_By_Hidden_Text_Provides_Displayed_Link_SpanAsync()
        {
            await RunTestAsync(async (form, richTextBox) =>
            {
                richTextBox.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang4105{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.17134}\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22\lang9
This is hidden text preceeding a \v #link1#\v0 custom link.\par
This is hidden text preceeding a \v #link2#\v0 custom link.\par
This is hidden text preceeding a \v #link3#\v0 custom link.\par
}";

                MakeLink(richTextBox, "#link1#custom link");
                MakeLink(richTextBox, "#link2#custom link");
                MakeLink(richTextBox, "#link3#custom link");

                Point previousPosition = new();
                BOOL setOldCursorPos   = GetPhysicalCursorPos(ref previousPosition);

                LinkClickedEventArgs?result     = null;
                LinkClickedEventHandler handler = (sender, e) => result = e;
                richTextBox.LinkClicked        += handler;
                try
                {
                    Point pt = richTextBox.PointToScreen(richTextBox.GetPositionFromCharIndex(richTextBox.Text.IndexOf("#link2#custom link")));

                    // Adjust point a bit to make sure we are clicking inside the character cell instead of on its edge.
                    pt.X += 2;
                    pt.Y += 2;
                    await MoveMouseAsync(form, pt);
                    await InputSimulator.SendAsync(
                        form,
                        inputSimulator => inputSimulator.Mouse.LeftButtonClick());
                    if (setOldCursorPos.IsTrue())
                    {
                        await MoveMouseAsync(form, previousPosition);
                    }

                    // Let the event queue drain after clicking, before moving the cursor back to the old position.
                    Application.DoEvents();
                }
                finally
                {
                    richTextBox.LinkClicked -= handler;

                    if (setOldCursorPos.IsTrue())
                    {
                        // Move cursor to the old position.
                        await InputSimulator.SendAsync(
                            form,
                            inputSimulator => inputSimulator.Mouse.MoveMouseTo(previousPosition.X, previousPosition.Y));
                        Application.DoEvents();
                    }
                }

                Assert.NotNull(result);

                Assert.True(result !.LinkStart + result.LinkLength <= richTextBox.Text.Length);
                Assert.Equal(result.LinkText, richTextBox.Text.Substring(result.LinkStart, result.LinkLength));

                // This assumes the input span is a custom link preceeded by hidden text.
                var hiddenText = GetTextFromRange(richTextBox, result.LinkStart, result.LinkLength, range =>
                {
                    // Move the cursor to the start of the link we are currently located in.
                    range.StartOf(TomLink, 0);

                    // Extend the cursor to the start of the hidden area preceeding the link.
                    range.StartOf(TomHidden, 1);
                });

                Assert.Equal("#link2#", hiddenText);
            });
        }
Пример #16
0
        public async Task RichTextBox_Click_On_Friendly_Name_Link_Provides_Hidden_Link_SpanAsync()
        {
            await RunTestAsync(async (form, richTextBox) =>
            {
                richTextBox.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang4105{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.17134}\viewkind4\uc1 
{\field{\*\fldinst { HYPERLINK ""http://www.google.com"" }}{\fldrslt {Click link #1}}}
\pard\sa200\sl276\slmult1\f0\fs22\lang9  for more information.\par
{\field{\*\fldinst { HYPERLINK ""http://www.google.com"" }}{\fldrslt {Click link #2}}}
\pard\sa200\sl276\slmult1\f0\fs22\lang9  for more information.\par
{\field{\*\fldinst { HYPERLINK ""http://www.google.com"" }}{\fldrslt {Click link #3}}}
\pard\sa200\sl276\slmult1\f0\fs22\lang9  for more information.\par
}";

                Point previousPosition = new();
                BOOL setOldCursorPos   = GetPhysicalCursorPos(ref previousPosition);

                LinkClickedEventArgs?result     = null;
                LinkClickedEventHandler handler = (sender, e) => result = e;
                richTextBox.LinkClicked        += handler;
                try
                {
                    Point pt = richTextBox.PointToScreen(richTextBox.GetPositionFromCharIndex(richTextBox.Text.IndexOf("Click link #2")));

                    // Adjust point a bit to make sure we are clicking inside the character cell instead of on its edge.
                    pt.X += 2;
                    pt.Y += 2;
                    await MoveMouseAsync(form, pt);
                    await InputSimulator.SendAsync(
                        form,
                        inputSimulator => inputSimulator.Mouse.LeftButtonClick());
                    if (setOldCursorPos.IsTrue())
                    {
                        await MoveMouseAsync(form, previousPosition);
                    }

                    // Let the event queue drain after clicking, before moving the cursor back to the old position.
                    Application.DoEvents();
                }
                finally
                {
                    richTextBox.LinkClicked -= handler;

                    if (setOldCursorPos.IsTrue())
                    {
                        // Move cursor to the old position.
                        await InputSimulator.SendAsync(
                            form,
                            inputSimulator => inputSimulator.Mouse.MoveMouseTo(previousPosition.X, previousPosition.Y));
                        Application.DoEvents();
                    }
                }

                Assert.NotNull(result);

                Assert.True(result !.LinkStart + result.LinkLength <= richTextBox.Text.Length);
                Assert.Equal(result.LinkText, richTextBox.Text.Substring(result.LinkStart, result.LinkLength));

                // This assumes the input span is the hidden text of a "friendly name" URL,
                // which is what the native control will pass to the LinkClicked event instead
                // of the actual span of the clicked display text.
                var displayText = GetTextFromRange(richTextBox, result.LinkStart, result.LinkLength, range =>
                {
                    // Move the cursor to the end of the hidden area we are currently located in.
                    range.EndOf(TomHidden, 0);

                    // Extend the cursor to the end of the display text of the link.
                    range.EndOf(TomLink, 1);
                });

                Assert.Equal("Click link #2", displayText);
            });
        }