|
-
Feb 11th, 2003, 05:59 PM
#1
Thread Starter
Junior Member
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.
-
Feb 11th, 2003, 10:39 PM
#2
Frenzied Member
Roughly what you're looking for.
VB Code:
Private Const WM_USER As Long = &H400
Private Const EM_AUTOURLDETECT As Long = (WM_USER + 91)
Private Const EM_GETSEL As Long = &HB0
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 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
Option Explicit
Private 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 Form_Load()
RichTextBox1.Text = "The URL to click is [url]http://www.something.com.[/url] Please click it."
End Sub
Private Sub RichTextBox1_Change()
DetectURL RichTextBox1, True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
End Sub
Private Sub RichTextBox1_Click()
Dim lngRetVal As Long
lngRetVal = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, 0)
Dim strBuffer As String, intInStr As Integer, intHi As Integer, intLo As Integer
intHi = HiWord(lngRetVal) + 1
intLo = LoWord(lngRetVal) + 1
intInStr = InStrRev(RichTextBox1.Text, " ", intLo)
If intInStr = 0 Then 'no space
strBuffer = Mid(RichTextBox1.Text, 1, intLo)
Else
strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
End If
strBuffer = Trim(strBuffer)
intInStr = InStr(1, strBuffer, " ")
If intInStr <> 0 Then
strBuffer = Mid(strBuffer, 1, intInStr - 1)
End If
If InStr(1, strBuffer, "http:") = 0 And _
InStr(1, strBuffer, "file:") = 0 And _
InStr(1, strBuffer, "mailto:") = 0 And _
InStr(1, strBuffer, "ftp:") = 0 And _
InStr(1, strBuffer, "https:") = 0 And _
InStr(1, strBuffer, "gopher:") = 0 And _
InStr(1, strBuffer, "nntp:") = 0 And _
InStr(1, strBuffer, "prospero:") = 0 And _
InStr(1, strBuffer, "telnet:") = 0 And _
InStr(1, strBuffer, "news:") = 0 And _
InStr(1, strBuffer, "wais:") = 0 Then Exit Sub
Debug.Print strBuffer
'Call ShellExecute(Me.hwnd, "OPEN", strBuffer, vbNullString, vbNullString, 5)
End Sub
Private Function LoWord(ByVal DWord As Long) As Long
If DWord And &H8000& Then
LoWord = DWord Or &HFFFF0000
Else
LoWord = DWord And &HFFFF&
End If
End Function
Private Function HiWord(ByVal DWord As Long) As Long
HiWord = (DWord And &HFFFF0000) \ &H10000
End Function
-
Feb 15th, 2004, 03:36 AM
#3
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|