Detect URLs in RichTextBox (using EM_AUTOURLDETECT)
This code snippet makes use of RichEdit 2.0's new EM_AUTOURLDETECT message. When you type in a valid webiste address e.g. www.planetsourcecode.com, it will be coloured in blue, then underlined. When the mouse pointer is over it, it will change to a hand icon, and when you click it, it will open a new browser an navigate to the link.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
'ADD THE FOLLOWING TO YOUR FORM
Private Sub Form_Load()
'Subclass the our main window so we can track when a link is hit
SubClassWnd hwnd
IDC_RICHEDIT = 4096
'Load the richedit 2 library
hModule = LoadLibrary("Riched20.dll")
If hModule Then
'Create the richedit window
hwndRichEdit = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASSA, "(Type in a URL)", WS_CHILD Or WS_VISIBLE Or ES_MULTILINE, 32, 32, 200, 200, hwnd, IDC_RICHEDIT, App.hInstance, 0)
'Set it up, such that it can automatically detect URLs
SendMessage hwndRichEdit, EM_SETEVENTMASK, 0, ByVal ENM_LINK
Call SendMessage(hwndRichEdit, EM_AUTOURLDETECT, 1, ByVal 0)
'Change to a more appropiate font
SetFont 12, "MS Sans Serif"
Else
MsgBox "Cannot initialize RichEdit."
Unload Me
End If
End Sub
Private Sub Form_Terminate()
'Free the library from memory
FreeLibrary hModule
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Unsubclass the window
UnSubclassWnd hwnd
End Sub
Original Comments (3)
Recovered from Wayback Machine