private void btnInsertHyperlink_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(txtUrl.Text) || string.IsNullOrEmpty(txtText.Text)) { return; } Uri uri = null; try { uri = new Uri(txtUrl.Text, UriKind.Absolute); } catch (FormatException) { try { uri = new Uri("http://" + txtUrl.Text, UriKind.Absolute); } catch (FormatException) { MessageDialog dialog = new MessageDialog(Strings.UnValidUrlMessage, Strings.Error); dialog.ShowAsync(); return; } } using (new DocumentHistoryGroup(RichTextBox.DocumentHistory)) { var text = txtText.Text.Replace("\r\n", "\n"); if (RichTextBox.Selection.Text != text) { RichTextBox.Selection.Delete(); var run = new C1Run() { Text = txtText.Text }; RichTextBox.Selection.Start.InsertInline(run); run.ContentRange.MakeHyperlink(uri); RichTextBox.Selection = run.ContentRange; } else { RichTextBox.Selection.MakeHyperlink(uri); } } Close(); }
private void ShowInsertHyperlinkDialog() { var dialog = new C1HyperlinkDialog { Text = rtb.Selection.Text }; window = new Window(); window.Content = dialog; window.ShowInTaskbar = false; window.WindowStartupLocation = WindowStartupLocation.CenterOwner; window.SizeToContent = SizeToContent.WidthAndHeight; window.WindowStyle = WindowStyle.ToolWindow; var elementSource = HwndSource.FromVisual(rtb) as HwndSource; if (elementSource != null) { int GA_ROOT = 2; IntPtr root = GetAncestor(elementSource.Handle, GA_ROOT); new WindowInteropHelper(window).Owner = root; } window.Closed += delegate { rtb.Focus(); }; dialog.Completed += delegate { window.Close(); if (string.IsNullOrEmpty(dialog.Text) || string.IsNullOrEmpty(dialog.Url)) return; Uri uri = null; try { uri = new Uri(dialog.Url, UriKind.Absolute); } catch (UriFormatException) { try { uri = new Uri("http://" + dialog.Url, UriKind.Absolute); } catch (UriFormatException) { return; } } using (new DocumentHistoryGroup(rtb.DocumentHistory)) { if (rtb.Selection.Text != dialog.Text) { rtb.Selection.Delete(); var run = new C1Run { Text = dialog.Text }; rtb.Selection.Start.InsertInline(run); run.ContentRange.MakeHyperlink(uri, dialog.TargetName); rtb.Selection = run.ContentRange; } else { rtb.Selection.MakeHyperlink(uri, dialog.TargetName); } } rtb.Focus(); }; window.ShowDialog(); }
private void ShowInsertHyperlinkDialog() { var dialog = new C1HyperlinkDialog { Text = rtb.Selection.Text }; window = new Window(); window.Content = dialog; window.ShowInTaskbar = false; window.WindowStartupLocation = WindowStartupLocation.CenterOwner; window.SizeToContent = SizeToContent.WidthAndHeight; window.WindowStyle = WindowStyle.ToolWindow; var elementSource = HwndSource.FromVisual(rtb) as HwndSource; if (elementSource != null) { int GA_ROOT = 2; IntPtr root = GetAncestor(elementSource.Handle, GA_ROOT); new WindowInteropHelper(window).Owner = root; } window.Closed += delegate { rtb.Focus(); }; dialog.Completed += delegate { window.Close(); if (string.IsNullOrEmpty(dialog.Text) || string.IsNullOrEmpty(dialog.Url)) { return; } Uri uri = null; try { uri = new Uri(dialog.Url, UriKind.Absolute); } catch (UriFormatException) { try { uri = new Uri("http://" + dialog.Url, UriKind.Absolute); } catch (UriFormatException) { return; } } using (new DocumentHistoryGroup(rtb.DocumentHistory)) { if (rtb.Selection.Text != dialog.Text) { rtb.Selection.Delete(); var run = new C1Run { Text = dialog.Text }; rtb.Selection.Start.InsertInline(run); run.ContentRange.MakeHyperlink(uri, dialog.TargetName); rtb.Selection = run.ContentRange; } else { rtb.Selection.MakeHyperlink(uri, dialog.TargetName); } } rtb.Focus(); }; window.ShowDialog(); }