{"id":448,"date":"2012-05-03T10:44:03","date_gmt":"2012-05-03T01:44:03","guid":{"rendered":"http:\/\/astel-labs.net\/blog\/diary\/?p=448"},"modified":"2012-05-03T10:44:03","modified_gmt":"2012-05-03T01:44:03","slug":"03-1","status":"publish","type":"post","link":"https:\/\/astel-labs.net\/blog\/diary\/2012\/05\/03-1.html","title":{"rendered":"WPF &#8211; TextBlock\u3067\u306eURL\u30a2\u30c9\u30ec\u30b9\u306e\u691c\u51fa\u3068Hyperlink\u306e\u6539\u884c"},"content":{"rendered":"<p>DolChatMonitor \u3067\u5b9f\u88c5\u3057\u305f\u5f8c\u306b\u30b5\u30f3\u30d7\u30eb\u516c\u958b\u7528\u306b\u6e96\u5099\u3092\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u516c\u958b\u7528\u306b\u6a5f\u80fd\u306e\u307f\u3092\u5207\u308a\u8cbc\u308a\u3057\u3066\u304b\u3089\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3059\u308b\u3064\u3082\u308a\u304c\u3064\u3044<span style=\"color: #000000;\"><del>\u9762\u5012<\/del><\/span>\u306b\u306a\u3063\u3066\u305d\u306e\u307e\u307e\u653e\u7f6e\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3046\u3069 GW \u306b\u306a\u3063\u305f\u306e\u3067\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3057\u306a\u3044\u3067\uff08\uff01\uff09\u305d\u306e\u307e\u307e\u516c\u958b\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f<\/p>\n<p>WPF \u306e TextBlock \u306f\u304b\u306a\u308a\u9ad8\u6a5f\u80fd\u306a\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306a\u306e\u3067\u3059\u304c\u3001\u5185\u90e8\u7684\u306b\u305d\u308c\u306a\u308a\u306b\u9762\u5012\u306a\u51e6\u7406\u3092\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u306e\u3067\u3001\u610f\u5916\u3068\u5358\u7d14\u306a\u30c6\u30ad\u30b9\u30c8\u8868\u793a\u4ee5\u4e0a\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u898b\u304b\u3051\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u4eca\u56de\u306f\u30bf\u30a4\u30c8\u30eb\u306e\u901a\u308a TextBox \u306b\u5165\u529b\u3057\u305f\u6587\u5b57\u5217\u304b\u3089 URL \u30a2\u30c9\u30ec\u30b9\u306e\u691c\u51fa\u3068\u3001Hyperlink \u306e\u6539\u884c\u3092\u884c\u3044\u307e\u3059<br \/>\n\u5b8c\u6210\u3057\u305f\u30a4\u30e1\u30fc\u30b8\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059<\/p>\n<p><a href=\"http:\/\/astel-labs.net\/blog\/diary\/images\/2012\/05\/TextBlock_01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"TextBlock_01.jpg\" src=\"http:\/\/astel-labs.net\/blog\/diary\/images\/2012\/05\/TextBlock_01.jpg\" alt=\"\" width=\"525\" height=\"350\" \/><\/a><\/p>\n<p>\u4f8b\u3067\u306f TextBlock \u306e Hyperlink \u3092 MouseOver \u306e\u72b6\u614b\u3067\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8\u3092\u64ae\u3063\u305f\u305f\u3081\u8d64\u304f\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059<br \/>\n\u307e\u305f\u3001HyperLink \u306f URL \u30a2\u30c9\u30ec\u30b9\u3068\u3057\u3066\u691c\u77e5\u3057\u3066\u3044\u308b\u7bc4\u56f2\u3067\u3067\u3042\u308c\u3070\u3044\u304f\u3089\u3067\u3082\u6539\u884c\u3067\u304d\u307e\u3059<\/p>\n<p><!--more--><\/p>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u3092\u7c21\u5358\u306b\u30fb\u30fb\u30fb<\/p>\n<pre class=\"brush: xml; gutter: true\">&lt;Window x:Class=&quot;HyperlinkSample.MainWindow&quot;\r\n        xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\r\n        xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\r\n        Title=&quot;MainWindow&quot; Height=&quot;350&quot; Width=&quot;525&quot;&gt;\r\n    &lt;Grid&gt;\r\n        &lt;TextBox Name=&quot;SampleText&quot; Margin=&quot;30,30,30,0&quot; AcceptsReturn=&quot;True&quot;\r\n                 VerticalAlignment=&quot;Top&quot; VerticalScrollBarVisibility=&quot;Auto&quot; Height=&quot;110&quot;\r\n                 Text=&quot;\u30cf\u30a4\u30d1\u30fc\u30ea\u30f3\u30af\u3066\u3059\u3068http:\/\/astel-labs.net\/bl&amp;#xd;&amp;#xa;og\/diary\/2012\/05\/03-1.html\u30ea\u30f3\u30af\u306f\u30af\u30ea\u30c3\u30af\u3067&amp;#xd;&amp;#xa;\u30c7\u30d5\u30a9\u30eb\u30c8\u30d6\u30e9\u30a6\u30b6\u304c\u8d77\u52d5\u3057\u307e\u3059&quot; \/&gt;\r\n        &lt;TextBlock xmlns:link=&quot;clr-namespace:HyperlinkSample&quot;\r\n                   Margin=&quot;30,161,30,30&quot; Foreground=&quot;Green&quot;\r\n                   Text=&quot;{Binding ElementName=SampleText, Path=Text}&quot;&gt;\r\n            &lt;link:HyperlinkText.Inline&gt;\r\n                &lt;Binding ElementName=&quot;SampleText&quot; Path=&quot;Text&quot; \/&gt;\r\n            &lt;\/link:HyperlinkText.Inline&gt;\r\n        &lt;\/TextBlock&gt;\r\n    &lt;\/Grid&gt;\r\n&lt;\/Window&gt;<\/pre>\n<p>\u307e\u305a\u306f Xaml \u304b\u3089\u3067\u3059\u304c\u3001\u3053\u3061\u3089\u306f\u30c6\u30ad\u30b9\u30c8\u5165\u529b\u7528\u306e TextBox \u3068\u3001\u672c\u984c\u306e TextBlock \u3092\u914d\u7f6e\u3057\u3066\u3044\u307e\u3059<br \/>\n\u4e00\u5fdc\u8aac\u660e\u3057\u3066\u304a\u304f\u3068\u3001TextBox \u306e Text \u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u300c&amp;#xd;&amp;#xa;\u300d\u306f\u3001Xaml \u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u5185\u3067\u6539\u884c\u3092\u3057\u305f\u3044\u5834\u5408\u306b\u300c\\r\\n\u300d\u3092\u6587\u5b57\u30b3\u30fc\u30c9\u5316\u3057\u305f\u66f8\u304d\u65b9\u306b\u306a\u308a\u307e\u3059<br \/>\n\u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u306f\u306a\u304f InnerText \u3067\u6539\u884c\u3092\u884c\u3046\u5834\u5408\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b xml:space=&#8221;preserve&#8221;\u00a0\u8a18\u8ff0\u3057\u307e\u3059<\/p>\n<p>&lt;TextBlock xml:space=&#8221;preserve&#8221;&gt;\u6587\u5b57\u5217\u3092&amp;#xd;&amp;#xa;\u6539\u884c\u3059\u308b&lt;\/TextBlock&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>TextBlock \u3067\u306f\u3001TextBlock \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3067\u4f5c\u6210\u3057\u305f Inline \u30d7\u30ed\u30d1\u30c6\u30a3\u3067 Hyperlink \u306e\u8a2d\u5b9a\u3092\u884c\u3044\u307e\u3059<br \/>\nInline \u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059<\/p>\n<pre class=\"brush: csharp; gutter: true\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Diagnostics;\r\nusing System.Text;\r\nusing System.Text.RegularExpressions;\r\nusing System.Windows;\r\nusing System.Windows.Controls;\r\nusing System.Windows.Documents;\r\nusing System.Windows.Input;\r\nusing System.Windows.Media;\r\nusing System.Windows.Navigation;\r\n\r\nnamespace HyperlinkSample\r\n{\r\n    public class HyperlinkText : TextBlock\r\n    {\r\n        private static Encoding m_Enc = Encoding.GetEncoding( &quot;Shift_JIS&quot; );\r\n\r\n        public static readonly DependencyProperty ArticleContentProperty =\r\n            DependencyProperty.RegisterAttached(\r\n                &quot;Inline&quot;,\r\n                typeof( string ),\r\n                typeof( HyperlinkText ),\r\n                new PropertyMetadata( null, OnInlinePropertyChanged ) );\r\n\r\n        public static string GetInline( TextBlock element )\r\n        {\r\n            return (element != null) ? element.GetValue( ArticleContentProperty ) as string : string.Empty;\r\n        }\r\n\r\n        public static void SetInline( TextBlock element, string value )\r\n        {\r\n            if( element != null )\r\n                element.SetValue( ArticleContentProperty, value );\r\n        }\r\n\r\n        private static void OnInlinePropertyChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e )\r\n        {\r\n            var tb = obj as TextBlock;\r\n            var msg = e.NewValue as string;\r\n            if( tb == null || msg == null )\r\n                return;\r\n\r\n            \/\/ \u672b\u5c3e\u306e\u6539\u884c\u30b3\u30fc\u30c9\u3092\u53d6\u308a\u9664\u304f\r\n            msg = msg.TrimEnd( new char[] { &#039;\\n&#039;, &#039;\\r&#039; } );\r\n\r\n            \/\/ \u6539\u884c\u4f4d\u7f6e\u306e\u53d6\u5f97\r\n            var nl = new List&lt;int&gt;();\r\n            int i = 0;\r\n            while( (i = msg.IndexOf( &quot;\\r\\n&quot;, i )) &gt;= 0 ) {\r\n                nl.Add( i - (nl.Count * 2) );\r\n                i += 2;\r\n            }\r\n            nl.Sort();\r\n\r\n            \/\/ \u6b63\u898f\u8868\u73fe\u3067URL\u30a2\u30c9\u30ec\u30b9\u3092\u691c\u51fa\r\n            var regex = new Regex( @&quot;http(s)?:\/\/([\\w-]+\\.)+[\\w-]+(\/[A-Z0-9-.,_\/?%&amp;=]*)?&quot;,\r\n                RegexOptions.IgnoreCase | RegexOptions.Singleline );\r\n            var text = msg.Replace( &quot;\\r\\n&quot;, &quot;&quot; );\r\n            var matchs = regex.Matches( text );\r\n            if( matchs.Count &gt; 0 ) {\r\n                tb.Text = null;\r\n                tb.Inlines.Clear();\r\n\r\n                int pos = 0;\r\n                int l = 0;\r\n                foreach( Match match in matchs ) {\r\n                    var index = match.Groups[0].Index;\r\n                    var length = match.Groups[0].Length;\r\n                    var tag = match.Groups[0].Value;\r\n\r\n                    \/\/ \u6587\u7ae0\u524d\u90e8\u306e\u975e\u30ea\u30f3\u30af\u6587\u5b57\u5217\u3092\u633f\u5165\r\n                    if( pos &lt; index ) {\r\n                        while( pos &lt; text.Length ) {\r\n                            if( nl.Count - l &gt; 0 &amp;&amp; nl[l] &lt; index ) {\r\n                                var buff = text.Substring( pos, nl[l] - pos );\r\n                                tb.Inlines.Add( new Run( buff ) );\r\n                                tb.Inlines.Add( new LineBreak() );\r\n                                pos = nl[l];\r\n                                l++;\r\n                            }\r\n                            else {\r\n                                var buff = text.Substring( pos, index - pos );\r\n                                tb.Inlines.Add( new Run( buff ) );\r\n                                pos = index;\r\n                                break;\r\n                            }\r\n                        }\r\n                    }\r\n\r\n                    \/\/ \u30ea\u30f3\u30af\u306e\u4f5c\u6210\r\n                    var link = new Hyperlink();\r\n                    link.TextDecorations = null;\r\n                    link.Foreground = tb.Foreground;\r\n                    link.NavigateUri = new Uri( tag );\r\n                    link.RequestNavigate += new RequestNavigateEventHandler( RequestNavigate );\r\n                    link.MouseEnter += new MouseEventHandler( link_MouseEnter );\r\n                    link.MouseLeave += new MouseEventHandler( link_MouseLeave );\r\n\r\n                    while( pos &lt; text.Length ) {\r\n                        if( nl.Count - l &gt; 0 &amp;&amp; nl[l] &lt; index + length ) {\r\n                            var buff = text.Substring( pos, nl[l] - pos );\r\n                            link.Inlines.Add( new Run( buff ) );\r\n                            link.Inlines.Add( new LineBreak() );\r\n                            pos = nl[l];\r\n                            l++;\r\n                        }\r\n                        else {\r\n                            var buff = text.Substring( pos, index + length - pos );\r\n                            link.Inlines.Add( new Run( buff ) );\r\n                            pos = index + length;\r\n                            break;\r\n                        }\r\n                    }\r\n\r\n                    \/\/ Hyperlink\u306e\u8ffd\u52a0\r\n                    tb.Inlines.Add( link );\r\n                }\r\n                \/\/ \u6587\u7ae0\u5f8c\u90e8\u306e\u975e\u30ea\u30f3\u30af\u6587\u5b57\u5217\u3092\u633f\u5165\r\n                while( pos &lt; text.Length ) {\r\n                    if( nl.Count - l &gt; 0 ) {\r\n                        var buff = text.Substring( pos, nl[l] - pos );\r\n                        tb.Inlines.Add( new Run( buff ) );\r\n                        tb.Inlines.Add( new LineBreak() );\r\n                        pos = nl[l];\r\n                        l++;\r\n                    }\r\n                    else {\r\n                        var buff = text.Substring( pos, text.Length - pos );\r\n                        tb.Inlines.Add( new Run( buff ) );\r\n                        pos = text.Length;\r\n                        break;\r\n                    }\r\n                }\r\n            }\r\n            else\r\n                tb.Text = msg;\r\n        }\r\n\r\n        private static void RequestNavigate( object sender, RequestNavigateEventArgs e )\r\n        {\r\n            try {\r\n                \/\/ URL\u30ea\u30f3\u30af\u304c\u9078\u629e\u3055\u308c\u305f\u5834\u5408\u3001\u65e2\u5b9a\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3(\u30d6\u30e9\u30a6\u30b6)\u3092\u8d77\u52d5\u3059\u308b\r\n                Process.Start( new ProcessStartInfo( e.Uri.AbsoluteUri ) );\r\n                \/\/ \u51e6\u7406\u3057\u305f\u5834\u5408\u306ftrue\u3092\u8fd4\u3059\r\n                e.Handled = true;\r\n            }\r\n            catch {\r\n                \/\/ \u7279\u306b\u4f55\u3082\u3057\u306a\u3044\r\n            }\r\n        }\r\n\r\n        private static void link_MouseEnter( object sender, MouseEventArgs e )\r\n        {\r\n            var link = sender as Hyperlink;\r\n            if( link == null )\r\n                return;\r\n\r\n            \/\/ \u30ea\u30f3\u30af\u306b\u30ab\u30fc\u30bd\u30eb\u3092\u5f53\u3066\u305f\u3068\u304d\u306f\u6587\u5b57\u8272\u3092\u8d64\u304f\u3059\u308b\r\n            link.Foreground = Brushes.Red;\r\n        }\r\n\r\n        private static void link_MouseLeave( object sender, MouseEventArgs e )\r\n        {\r\n            var link = sender as Hyperlink;\r\n            var parent = link.Parent as TextBlock;\r\n            if( link == null || parent == null )\r\n                return;\r\n\r\n            \/\/ \u30ea\u30f3\u30af\u304b\u3089\u30ab\u30fc\u30bd\u30eb\u304c\u96e2\u308c\u305f\u3068\u304d\u306f\u6587\u5b57\u8272\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u30ab\u30e9\u30fc\u306b\u623b\u3059\r\n            link.Foreground = parent.Foreground;\r\n        }\r\n    }\r\n}<\/pre>\n<p>TextBox \u3067\u5165\u529b\u3057\u305f\u6587\u5b57\u5217\u306f TextBlock \u306b\u30c7\u30fc\u30bf\u30d0\u30a4\u30f3\u30c9\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u6587\u5b57\u304c\u5165\u529b\u3055\u308c\u308b\u305f\u3073\u306b Hyperlink \u306e\u691c\u51fa\u304c\u884c\u308f\u308c\u307e\u3059<br \/>\nHyperlink \u00a0\u306e\u691c\u51fa\u306f\u6b63\u898f\u8868\u73fe\u3067\u884c\u3063\u3066\u304a\u308a\u3001 \u4ee5\u4e0b\u306e\u66f8\u5f0f\u306b\u4e00\u81f4\u3059\u308b\u6587\u5b57\u5217\u3092 URL \u3068\u3057\u3066\u8a8d\u8b58\u3057\u307e\u3059<\/p>\n<pre>http(s)?:\/\/([\\w-]+\\.)+[\\w-]+(\/[A-Z0-9-.,_\/?%&amp;=]*)?<\/pre>\n<p>\u8a73\u3057\u304f\u306f\u30cd\u30c3\u30c8\u3067\u8abf\u3079\u3066\u304f\u3060\u3055\u3044\u30fb\u30fb\u30fb<\/p>\n<p>&nbsp;<\/p>\n<p>\u3068\u3044\u3046\u304b\u3001\u3044\u308d\u3044\u308d\u3068\u8aac\u660e\u304c\u9762\u5012\u306b\u306a\u3063\u3066\u304d\u305f\u306e\u3067\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u306f\u30b3\u30fc\u30c9\u3092\u898b\u3066\u611f\u3058\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DolChatMonitor \u3067\u5b9f\u88c5\u3057\u305f\u5f8c\u306b\u30b5\u30f3\u30d7\u30eb\u516c\u958b\u7528\u306b\u6e96\u5099\u3092\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u516c\u958b\u7528\u306b\u6a5f\u80fd\u306e\u307f\u3092\u5207\u308a\u8cbc\u308a\u3057\u3066\u304b\u3089\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3059\u308b\u3064\u3082\u308a\u304c\u3064\u3044\u9762\u5012\u306b\u306a\u3063\u3066\u305d\u306e\u307e\u307e\u653e\u7f6e\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3046\u3069 GW \u306b\u306a\u3063\u305f\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7,4],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","category-4"],"_links":{"self":[{"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":3,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/posts\/448\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/astel-labs.net\/blog\/diary\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}