Results 1 to 3 of 3

Thread: Detecting a URL in a RichTextBox (Resolved)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    Redondo Beach
    Posts
    25

    Detecting a URL in a RichTextBox (Resolved)

    Here is my code:

    Code:
    Option Explicit
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpoperation _ As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias _ 
    "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const SW_MINIMIZE = 6
    Private Const SW_MAXIMIZE = 3
    Private Const SW_RESTORE = 9
    Private Const SW_SHOWNORMAL = 1
    Private Const WM_USER = &H400
    Private Const EM_AUTOURLDETECT = (WM_USER + 91)
    
    Public Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
        Dim lngRet As Long
        Dim strText As String
        
        With p_RichText
            ' this line is needed because the function will not update the
            ' url if you had it before
            strText = .Text
            ' send message to detect urls
            ' notice the Abs function. This is needed to pass 0 or 1
            ' in VB true is -1, so we have to get the absolute value of that
            lngRet = SendMessage(RichTextBox1.hWnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
            ' rewrite the text into the RichText so it will change all URLs if you
            'had them before
            .Text = strText
        End With
    End Sub
    
    Private Sub RichTextBox1_Change()
        DetectURL RichTextBox1, True
        RichTextBox1.SelStart = Len(RichTextBox1.Text)
    End Sub
    
    Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
        End Sub
    First the user can type text and somewhere in that text they can have a url. My code makes it look like it is a hyperlink.

    Now all that I need is to figure out how to find when the mouse is over this link and when the user selects it then execute that URL. How do I do this?
    Last edited by Jacque; Feb 12th, 2003 at 11:41 AM.

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Roughly what you're looking for.

    VB Code:
    1. Private Const WM_USER As Long = &H400
    2. Private Const EM_AUTOURLDETECT As Long = (WM_USER + 91)
    3. Private Const EM_GETSEL As Long = &HB0
    4.  
    5. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    6.      ByVal hwnd As Long, _
    7.      ByVal wMsg As Long, _
    8.      ByVal wParam As Long, _
    9.      lParam As Any) As Long
    10.  
    11. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    12.      ByVal hwnd As Long, _
    13.      ByVal lpOperation As String, _
    14.      ByVal lpFile As String, _
    15.      ByVal lpParameters As String, _
    16.      ByVal lpDirectory As String, _
    17.      ByVal nShowCmd As Long) As Long
    18.  
    19.  
    20. Option Explicit
    21.  
    22. Private Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
    23.     Dim lngRet As Long
    24.     Dim strText As String
    25.    
    26.     With p_RichText
    27.         ' this line is needed because the function will not update the
    28.         ' url if you had it before
    29.         strText = .Text
    30.         ' send message to detect urls
    31.         ' notice the Abs function. This is needed to pass 0 or 1
    32.         ' in VB true is -1, so we have to get the absolute value of that
    33.         lngRet = SendMessage(RichTextBox1.hwnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
    34.         ' rewrite the text into the RichText so it will change all URLs if you
    35.         'had them before
    36.         .Text = strText
    37.     End With
    38. End Sub
    39.  
    40. Private Sub Form_Load()
    41.  
    42.     RichTextBox1.Text = "The URL to click is [url]http://www.something.com.[/url]  Please click it."
    43.    
    44. End Sub
    45.  
    46. Private Sub RichTextBox1_Change()
    47.     DetectURL RichTextBox1, True
    48.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    49. End Sub
    50.  
    51.  
    52. Private Sub RichTextBox1_Click()
    53.  
    54.     Dim lngRetVal As Long
    55.    
    56.     lngRetVal = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, 0)
    57.    
    58.     Dim strBuffer As String, intInStr As Integer, intHi As Integer, intLo As Integer
    59.    
    60.     intHi = HiWord(lngRetVal) + 1
    61.     intLo = LoWord(lngRetVal) + 1
    62.    
    63.     intInStr = InStrRev(RichTextBox1.Text, " ", intLo)
    64.    
    65.     If intInStr = 0 Then 'no space
    66.         strBuffer = Mid(RichTextBox1.Text, 1, intLo)
    67.     Else
    68.         strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
    69.     End If
    70.    
    71.     strBuffer = Trim(strBuffer)
    72.     intInStr = InStr(1, strBuffer, " ")
    73.    
    74.     If intInStr <> 0 Then
    75.         strBuffer = Mid(strBuffer, 1, intInStr - 1)
    76.     End If
    77.    
    78.     If InStr(1, strBuffer, "http:") = 0 And _
    79.         InStr(1, strBuffer, "file:") = 0 And _
    80.         InStr(1, strBuffer, "mailto:") = 0 And _
    81.         InStr(1, strBuffer, "ftp:") = 0 And _
    82.         InStr(1, strBuffer, "https:") = 0 And _
    83.         InStr(1, strBuffer, "gopher:") = 0 And _
    84.         InStr(1, strBuffer, "nntp:") = 0 And _
    85.         InStr(1, strBuffer, "prospero:") = 0 And _
    86.         InStr(1, strBuffer, "telnet:") = 0 And _
    87.         InStr(1, strBuffer, "news:") = 0 And _
    88.         InStr(1, strBuffer, "wais:") = 0 Then Exit Sub
    89.        
    90.     Debug.Print strBuffer
    91.    
    92.     'Call ShellExecute(Me.hwnd, "OPEN", strBuffer, vbNullString, vbNullString, 5)
    93.    
    94. End Sub
    95.  
    96. Private Function LoWord(ByVal DWord As Long) As Long
    97.   If DWord And &H8000& Then
    98.     LoWord = DWord Or &HFFFF0000
    99.   Else
    100.     LoWord = DWord And &HFFFF&
    101.   End If
    102. End Function
    103.  
    104. Private Function HiWord(ByVal DWord As Long) As Long
    105.   HiWord = (DWord And &HFFFF0000) \ &H10000
    106. End Function
    Please rate my post.

  3. #3
    Lively Member
    Join Date
    Feb 2000
    Location
    Tucson, AZ, USA
    Posts
    95
    I know this is resolved, but I have a question about the above code, and thought I might as well post here. Hopefully someone will look .

    I making a chat type program. So I am wanting to add the functionality for hyperlinks to show up. Just like the above code does. The only problem is this.

    I created a new program with just above code to do some testing. The initial text added to RichTextBox1 at runtime works fine. But when ever I add text to the text box it does not work correctly anymore. Is there an easy fix for this?

    Example. If I clear all the text and type in a URL it only prints out up to the point where I clicked. So if I type http://www.google.com and click on the O, debug.print shows http://www.goo

    Also, if I add a return or new lines then it seems to add the returns into the debug.print.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width