Results 1 to 40 of 162

Thread: RichTextBox Tricks and Tips

Threaded View

  1. #10

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Auto Detect and respond to URLs

    The RichTextBox control has the ability to detect URLs as they are typed. It can convert this text into a hyperlink which can launch a browser when clicked.

    To turn on Auto URL detection simply send the RTB an EM_AUTOURLDETECT message.

    When the control detects that a URL is being entered, it reformats the text being entered so that it looks like a hyperlink and marks that text with a CFE_LINK effect.

    When the mouse pointer is over text with a CFE_LINK effect, the RTB can be configured to send a message to its parent. In order to respond to mouse events over the hyperlink text, the parent has to be subclassed or hooked.

    The following code shows how to setup Auto URL detection
    Code:
    Public Sub EnableAutoURLDetection(RTB As RichTextBox)
    
        'enable auto URL detection
        SendMessage RTB.hwnd, EM_AUTOURLDETECT, 1&, ByVal 0&
    
        'subclass the parent of the RTB to receive EN_LINK notifications
        Set FormSubClass = New clsSubClass
        FormSubClass.Enable RTB.Parent.hwnd
        
        'set RTB to notify parent when user has clicked hyperlink
        SendMessage RTB.hwnd, EM_SETEVENTMASK, 0&, ByVal ENM_LINK
    
    End Sub
    And to respond to a left mouse click you could do the following in your form's subclass routine.
    Code:
    Private Sub FormSubClass_WMArrival(hwnd As Long, uMsg As Long, wParam As Long, lParam As Long, lRetVal As Long)
    Dim notifyCode As nmhdr
    Dim LinkData As ENLINK
    Dim URL As String
    
        Select Case uMsg
        Case WM_NOTIFY
    
            CopyMemory notifyCode, ByVal lParam, LenB(notifyCode)
            If notifyCode.code = EN_LINK Then
            'A RTB sends EN_LINK notifications when it receives certain mouse messages
            'while the mouse pointer is over text that has the CFE_LINK effect:
            
            'To receive EN_LINK notifications, specify the ENM_LINK flag in the mask
            'sent with the EM_SETEVENTMASK message.
            
            'If you send the EM_AUTOURLDETECT message to enable automatic URL detection,
            'the RTB automatically sets the CFE_LINK effect for modified text that it
            'identifies as a URL.
            
                CopyMemory LinkData, ByVal lParam, Len(LinkData)
                If LinkData.Msg = WM_LBUTTONUP Then
                    'user clicked on a hyperlink
                    'get text with CFE_LINK effect that caused message to be sent
                    URL = Mid(RTB.Text, LinkData.chrg.cpMin + 1, LinkData.chrg.cpMax - LinkData.chrg.cpMin)
                    'launch the browser here
                    ShellExecute 0&, "OPEN", URL, vbNullString, "C:\", SW_SHOWNORMAL
                End If
    
            End If
            lRetVal = FormSubClass.callWindProc(hwnd, uMsg, wParam, lParam)
            
        Case Else
            lRetVal = FormSubClass.callWindProc(hwnd, uMsg, wParam, lParam)
        End Select
    
    End Sub
    Attached is a project that demonstrates the whole idea.
    Attached Files Attached Files
    Last edited by moeur; Mar 12th, 2007 at 08:46 PM.

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