Results 1 to 10 of 10

Thread: Mouse over a link

  1. #1

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

    Mouse over a link

    In my RichEdit Text box the user is able to type in a link where I use SendMessage and EM_AUTOURLDETECT. When the user puts their mouse over the link and clicks on it I would like to captue that mouse position. How do I do this?

    I looked in the MSDN library and found that I could use en_link but I don't understand how to do this?

    Any suggestions?

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    I thought I answered this one...
    Please rate my post.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    Redondo Beach
    Posts
    25
    You did give me an option. After I had posted (Resolved) I did extensive testing and found that it wasn't working correctly. If you type in a URL in the RichEdit text box and then click it, it will sometimes work and sometimes not. It will depend on where your mouse is on the link. Most of the time it will give the URL from where the mouse is clicked to the beginning of the URL.

    For example, "The URL to click is http://www.something.com. Please click it." is added when the form is loaded. If you type in http://www.newurl.com after that and then click on the 'n' in "http://www.newurl.com", debug.print returns: http://www.n

    Also if you press the enter key before you type http://www.newurl.com it won't return the correct url. After searching for more ways of doing what I need I found that you can use en_link but I just don't understand how to do that.

    How would I use en_link with wm_notify?

    For those of you who are interested in the earlier post here is a link.

    http://www.vbforums.com/showthread.p...hreadid=230073

  4. #4
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    I originally tried subclassing the form and RichTextBox for WM_NOTIFY but for whatever reason, it wasn't raised when the URL was clicked.

    Even the examples I found on the 'net didn't work.
    Please rate my post.

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Try this:

    In a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    4.      ByVal hWnd As Long, _
    5.      ByVal wMsg As Long, _
    6.      ByVal wParam As Long, _
    7.      lParam As Any) As Long
    8.  
    9. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    10.      ByVal hWnd As Long, _
    11.      ByVal lpOperation As String, _
    12.      ByVal lpFile As String, _
    13.      ByVal lpParameters As String, _
    14.      ByVal lpDirectory As String, _
    15.      ByVal nShowCmd As Long) As Long
    16.  
    17.  
    18. Private Type NMHDR
    19.     hwndFrom As Long
    20.     idfrom As Long
    21.     code As Long
    22. End Type
    23.  
    24. Private Type NMHDR_RICHEDIT
    25.     hwndFrom As Long
    26.     wPad1 As Integer
    27.     idfrom As Integer
    28.     code As Integer
    29.     wPad2 As Integer
    30. End Type
    31.  
    32. Private Type CHARRANGE
    33.     cpMin As Long
    34.     cpMax As Long
    35. End Type
    36.  
    37. Private Type TEXTRANGE
    38.     chrg As CHARRANGE
    39.     lpstrText As Long
    40. End Type
    41.  
    42. Private Type ENLINK
    43.     NMHDR As NMHDR_RICHEDIT
    44.     msg As Integer
    45.     wPad1 As Integer
    46.     wParam As Integer
    47.     wPad2 As Integer
    48.     lParam As Integer
    49.     chrg As CHARRANGE
    50. End Type
    51.  
    52. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    53. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    54. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    55.  
    56. Private Const GWL_WNDPROC = (-4)
    57.  
    58. Private Const WM_USER = &H400
    59. Private Const WM_NOTIFY = &H4E
    60. Private Const WM_LBUTTONDBLCLK = &H203
    61. Private Const WM_LBUTTONDOWN = &H201
    62. Private Const WM_LBUTTONUP = &H202
    63.  
    64. Private Const EM_SETEVENTMASK = (WM_USER + 69)
    65. Private Const EM_GETTEXTRANGE = (WM_USER + 75)
    66. Private Const EM_AUTOURLDETECT = (WM_USER + 91)
    67. Private Const EM_EXSETSEL = (WM_USER + 55)
    68.  
    69. Private Const ENM_LINK = &H4000000
    70. Private Const ENM_NONE = &H0
    71.  
    72. Private Const EN_LINK = &H70B&
    73.  
    74. Private mlWndProc As Long
    75.  
    76. Public Sub EnableURLs(ByVal hWndParent As Long, ByVal hWndRTB As Long)
    77.   ' Turn on URL Detection
    78.   Call SendMessage(hWndRTB, EM_AUTOURLDETECT, 1, ByVal 0)
    79.   ' Tell it to send the EN_LINK notification message
    80.   Call SendMessage(hWndRTB, EM_SETEVENTMASK, 0&, ByVal ENM_LINK)
    81.   ' Subclass the RTB Parent
    82.   mlWndProc = SetWindowLong(hWndParent, GWL_WNDPROC, AddressOf SubClassedWindowProc)
    83. End Sub
    84.  
    85. Public Sub DisableURLs(ByVal hWndParent As Long, ByVal hWndRTB As Long)
    86.   ' Turn off subclassing
    87.   Call SetWindowLong(hWndParent, GWL_WNDPROC, mlWndProc)
    88.   ' Turn off URL Detection
    89.   Call SendMessage(hWndRTB, EM_AUTOURLDETECT, 0&, ByVal 0&)
    90.   ' Turn off EN_LINK notifications
    91.   Call SendMessage(hWndRTB, EM_SETEVENTMASK, 0&, ByVal ENM_NONE)
    92. End Sub
    93.  
    94. Private Function SubClassedWindowProc(ByVal hWnd As Long, ByVal nMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    95.   Dim tNMHDR As NMHDR
    96.   Dim tENLINK As ENLINK
    97.   Dim tTEXT As TEXTRANGE
    98.   Dim sBuffer As String
    99.  
    100.   ' Look for Notification Messages (WM_NOTIFY)
    101.   If nMsg = WM_NOTIFY Then
    102.     Call CopyMemory(tNMHDR, ByVal lParam, Len(tNMHDR))
    103.    
    104.     ' If it's the Link Notification...
    105.     If tNMHDR.code = EN_LINK Then
    106.       ' Copy the Notification into a Link Notification Structure
    107.       Call CopyMemory(tENLINK, ByVal lParam, Len(tENLINK))
    108.      
    109.       ' See what event caused this notification,
    110.       ' If it was a Left Mouse Button Down event, process it..
    111.       If tENLINK.msg = WM_LBUTTONDOWN Then
    112.      
    113.         ' Transfer the character range contianing the URL
    114.         ' to the TEXTRANGE structure
    115.         LSet tTEXT.chrg = tENLINK.chrg
    116.        
    117.         ' Create a Buffer to hold the URL text
    118.         sBuffer = String(tTEXT.chrg.cpMax - tTEXT.chrg.cpMin, Chr(0))
    119.        
    120.         ' Assign the buffer to the TEXTRANGE structure
    121.         tTEXT.lpstrText = StrPtr(sBuffer)
    122.        
    123.         ' Tell the RTB to give us the text in the given range
    124.         Call SendMessage(tNMHDR.hwndFrom, EM_GETTEXTRANGE, 0, tTEXT)
    125.        
    126.         ' Strip out the null characters
    127.         sBuffer = Replace(StrConv(sBuffer, vbUnicode), Chr(0), "")
    128.        
    129.         ' Launch the URL
    130.         ShellExecute hWnd, "OPEN", sBuffer, "", "", 1
    131.      
    132.       End If
    133.    
    134.     End If
    135.    
    136.   End If
    137.  
    138.   SubClassedWindowProc = CallWindowProc(mlWndProc, hWnd, nMsg, wParam, lParam)
    139. End Function
    In a Form with a RichTextBox
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     EnableURLs hWnd, RichTextBox1.hWnd
    5.     RichTextBox1.Text = "The URL to click is [url]http://www.something.com.[/url]  Please click it."
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.   DisableURLs hWnd, RichTextBox1.hWnd
    10. End Sub

  6. #6
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Nice.
    Please rate my post.

  7. #7
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    WOW!

    That Module Rocks ;o) Execellent work to anyone involved in that code.


    (performance is above standard)
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  8. #8
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185
    Hi Aaron

    I've just been playing with the code that enables URL's to be automatically detected in a rich text box and have noticed that the SelChange event no longer gets raised.

    Please help, as I would like to use the AutoDetect feature in the Rich Text Box, but I also need the SelChange event to be raised correctly. Or is there some other way I can detect the SelChange event using subclassing?

    Thanks in advance

  9. #9
    Lively Member
    Join Date
    Jul 2004
    Posts
    110
    When I try and use that code it says invalid use of AddressOf operator. What should I do?

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by ChR0NiC
    When I try and use that code it says invalid use of AddressOf operator. What should I do?
    Can you post your project?


    Has someone helped you? Then you can Rate their helpful post.

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