Results 1 to 2 of 2

Thread: Opening URLs in new browser window

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Opening URLs in new browser window

    I have subclassed a RichTextBox to open URLs. Well, it does open up a browser (and with the code below it opens in a new browser window), but atleast Mozilla Firebird doesn't open the given url, only itself and the default homepage. Does the code below work for anyone?

    VB Code:
    1. Private Const CREATE_NEW_CONSOLE As Long = &H10
    2. Private Const NORMAL_PRIORITY_CLASS As Long = &H20
    3. Private Const INFINITE As Long = -1
    4. Private Const STARTF_USESHOWWINDOW As Long = &H1
    5. Private Const SW_SHOWNORMAL As Long = 1
    6.  
    7. Private Const MAX_PATH As Long = 260
    8. Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
    9. Private Const ERROR_FILE_NOT_FOUND As Long = 2
    10. Private Const ERROR_PATH_NOT_FOUND As Long = 3
    11. Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
    12. Private Const ERROR_BAD_FORMAT As Long = 11
    13.  
    14. Private Type STARTUPINFO
    15.   cb As Long
    16.   lpReserved As String
    17.   lpDesktop As String
    18.   lpTitle As String
    19.   dwX As Long
    20.   dwY As Long
    21.   dwXSize As Long
    22.   dwYSize As Long
    23.   dwXCountChars As Long
    24.   dwYCountChars As Long
    25.   dwFillAttribute As Long
    26.   dwFlags As Long
    27.   wShowWindow As Integer
    28.   cbReserved2 As Integer
    29.   lpReserved2 As Long
    30.   hStdInput As Long
    31.   hStdOutput As Long
    32.   hStdError As Long
    33. End Type
    34.  
    35. Private Type PROCESS_INFORMATION
    36.   hProcess As Long
    37.   hThread As Long
    38.   dwProcessId As Long
    39.   dwThreadID As Long
    40. End Type
    41.  
    42. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpAppName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    43. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    44. Private Declare Function FindExecutable Lib "shell32" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal sResult As String) As Long
    45. Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nSize As Long, ByVal lpBuffer As String) As Long
    46.  
    47. Private Function StartNewBrowser(sURL As String) As Boolean
    48.     Dim success As Long
    49.     Dim hProcess As Long
    50.     Dim sBrowser As String
    51.     Dim start As STARTUPINFO
    52.     Dim proc As PROCESS_INFORMATION
    53.     sBrowser = GetBrowserName(success)
    54.     If success >= ERROR_FILE_SUCCESS Then
    55.         With start
    56.             .cb = Len(start)
    57.             .dwFlags = STARTF_USESHOWWINDOW
    58.             .wShowWindow = SW_SHOWNORMAL
    59.         End With
    60.         success = CreateProcess(sBrowser, " " & sURL, 0&, 0&, 0&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    61.         StartNewBrowser = proc.hProcess <> 0
    62.         Call CloseHandle(proc.hProcess)
    63.         Call CloseHandle(proc.hThread)
    64.     End If
    65. End Function
    66. Private Function GetBrowserName(dwFlagReturned As Long) As String
    67.     Dim hFile As Long
    68.     Dim sResult As String
    69.     Dim sTempFolder As String
    70.     sTempFolder = GetTempDir()
    71.     hFile = FreeFile
    72.     Open sTempFolder & "dummy.html" For Output As #hFile
    73.     Close #hFile
    74.     sResult = Space$(MAX_PATH)
    75.     dwFlagReturned = FindExecutable("dummy.html", sTempFolder, sResult)
    76.     Kill sTempFolder & "dummy.html"
    77.     GetBrowserName = TrimNull(sResult)
    78. End Function
    79. Private Function TrimNull(Item As String)
    80.     Dim pos As Integer
    81.     pos = InStr(Item, Chr$(0))
    82.     If pos Then
    83.         TrimNull = Left$(Item, pos - 1)
    84.     Else: TrimNull = Item
    85.     End If
    86. End Function
    87. Public Function GetTempDir() As String
    88.     Dim tmp As String
    89.     tmp = Space$(256)
    90.     Call GetTempPath(Len(tmp), tmp)
    91.     GetTempDir = TrimNull(tmp)
    92. End Function

    Code I use for RTB subclassing:
    VB Code:
    1. 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
    2. 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
    3.  
    4. Private Type NMHDR
    5.     hwndFrom As Long
    6.     idfrom As Long
    7.     code As Long
    8. End Type
    9.  
    10. Private Type NMHDR_RICHEDIT
    11.     hwndFrom As Long
    12.     wPad1 As Integer
    13.     idfrom As Integer
    14.     code As Integer
    15.     wPad2 As Integer
    16. End Type
    17.  
    18. Private Type CHARRANGE
    19.     cpMin As Long
    20.     cpMax As Long
    21. End Type
    22.  
    23. Private Type TEXTRANGE
    24.     chrg As CHARRANGE
    25.     lpstrText As Long
    26. End Type
    27.  
    28. Private Type ENLINK
    29.     NMHDR As NMHDR_RICHEDIT
    30.     msg As Integer
    31.     wPad1 As Integer
    32.     wParam As Integer
    33.     wPad2 As Integer
    34.     lParam As Integer
    35.     chrg As CHARRANGE
    36. End Type
    37.  
    38. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    39. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    40. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    41.  
    42. Private Const GWL_WNDPROC = (-4)
    43.  
    44. Private Const WM_USER = &H400
    45. Private Const WM_NOTIFY = &H4E
    46. Private Const WM_LBUTTONDBLCLK = &H203
    47. Private Const WM_LBUTTONDOWN = &H201
    48. Private Const WM_LBUTTONUP = &H202
    49. Private Const WM_RBUTTONDBLCLK = &H206
    50. Private Const WM_RBUTTONDOWN = &H204
    51. Private Const WM_RBUTTONUP = &H205
    52. Private Const WM_MBUTTONDBLCLK = &H209
    53. Private Const WM_MBUTTONDOWN = &H207
    54. Private Const WM_MBUTTONUP = &H208
    55.  
    56. Private Const EM_SETEVENTMASK = (WM_USER + 69)
    57. Private Const EM_GETTEXTRANGE = (WM_USER + 75)
    58. Private Const EM_AUTOURLDETECT = (WM_USER + 91)
    59. Private Const EM_EXSETSEL = (WM_USER + 55)
    60.  
    61. Private Const ENM_LINK = &H4000000
    62. Private Const ENM_NONE = &H0
    63.  
    64. Private Const EN_LINK = &H70B&
    65.  
    66. Private mlWndProc As Long
    67.  
    68. Public Sub EnableURLs(ByVal hWndParent As Long, ByVal hWndRTB As Long)
    69.     Call SendMessage(hWndRTB, EM_AUTOURLDETECT, 1, ByVal 0)
    70.     Call SendMessage(hWndRTB, EM_SETEVENTMASK, 0&, ByVal ENM_LINK)
    71.     mlWndProc = SetWindowLong(hWndParent, GWL_WNDPROC, AddressOf SubClassedWindowProc)
    72. End Sub
    73. Public Sub DisableURLs(ByVal hWndParent As Long, ByVal hWndRTB As Long)
    74.     Call SetWindowLong(hWndParent, GWL_WNDPROC, mlWndProc)
    75.     Call SendMessage(hWndRTB, EM_AUTOURLDETECT, 0&, ByVal 0&)
    76.     Call SendMessage(hWndRTB, EM_SETEVENTMASK, 0&, ByVal ENM_NONE)
    77. End Sub
    78. Private Function SubClassedWindowProc(ByVal hWnd As Long, ByVal nMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    79.     Dim tNMHDR As NMHDR
    80.     Dim tENLINK As ENLINK
    81.     Dim tTEXT As TEXTRANGE
    82.     Dim sBuffer As String
    83.     If nMsg = WM_NOTIFY Then
    84.         Call CopyMemory(tNMHDR, ByVal lParam, Len(tNMHDR))
    85.         If tNMHDR.code = EN_LINK Then
    86.             Call CopyMemory(tENLINK, ByVal lParam, Len(tENLINK))
    87.             If tENLINK.msg = WM_RBUTTONUP Then
    88.                 LSet tTEXT.chrg = tENLINK.chrg
    89.                 sBuffer = String(tTEXT.chrg.cpMax - tTEXT.chrg.cpMin, Chr(0))
    90.                 tTEXT.lpstrText = StrPtr(sBuffer)
    91.                 Call SendMessage(tNMHDR.hwndFrom, EM_GETTEXTRANGE, 0, tTEXT)
    92.                 sBuffer = Replace(StrConv(sBuffer, vbUnicode), Chr(0), "")
    93.                 'ShellExecute hWnd, "OPEN", sBuffer, "", "", 1
    94.                 StartNewBrowser sBuffer
    95.             End If
    96.         End If
    97.     End If
    98.     SubClassedWindowProc = CallWindowProc(mlWndProc, hWnd, nMsg, wParam, lParam)
    99. End Function

    Any help appreciated

  2. #2

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Well, I found more things behind this: the code does open up the link in a new browser, but only if there is no existing browser window. If there is, it just opens a new browser. I've tested only with Firebird, since I don't want to open IE

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