home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / View_HTML_47064172002.psc / mod_HTMLRender.bas < prev    next >
Encoding:
BASIC Source File  |  2002-01-06  |  8.7 KB  |  355 lines

  1. Attribute VB_Name = "mod_HTMLRender"
  2. Dim isInLink As Boolean
  3. Dim OldColor As Long
  4. Dim bUnderLinned As Boolean
  5. Dim LLastLink As Long
  6. Public Sub DisplayWebPage(Obj As PictureBox)
  7. 'This is where we parse the HTML and render the web page
  8. Dim Ipos As Single
  9. Dim Epos As Single
  10. Dim sTAG As String
  11. Dim sTXT As String
  12. Dim rc As RECT
  13. Dim lWidth As Long
  14. Dim lHeight As Long
  15.  
  16. 'Clear our links array
  17. ReDim AllLinks(0)
  18.  
  19. sHTML = Replace(sHTML, vbCrLf, "")
  20.  
  21.  
  22. Form1.Label1.Move ViewMargin, ViewMargin
  23.  
  24. TOldY = ViewMargin
  25. TOldX = ViewMargin
  26.  
  27. rc.Top = TOldY
  28. rc.Left = TOldX
  29. rc.Bottom = (Obj.Height / Screen.TwipsPerPixelY) - rc.Top
  30. rc.Right = (Obj.Width / Screen.TwipsPerPixelX) - rc.Left
  31.  
  32. Obj.Visible = False
  33. Obj.Cls
  34. Epos = 1
  35. If sBGImage <> "" Then
  36. TileBitmap Obj, Form1.PicImage
  37. TileBitmap Form1.PicBacking, Form1.PicImage
  38. End If
  39.  
  40. Ipos = InStr(1, sHTML, "<BODY")
  41. If Ipos = 0 Then Exit Sub
  42. Epos = InStr(Ipos, sHTML, ">")
  43. If Epos = 0 Then Epos = 1
  44. Do
  45. DoEvents
  46. 'Get the start and end pos of the tag
  47. Ipos = InStr(Epos, sHTML, "<", vbBinaryCompare)
  48. If Ipos = 0 Then Exit Do
  49.  
  50. 'Get the plain text before the tag
  51.  
  52. sTXT = Mid(sHTML, Epos + 1, (Ipos - Epos))
  53. sTXT = Replace(sTXT, "<", "")
  54.  
  55.  
  56. 'Get the Ending Pos of the tag
  57. Epos = InStr(Ipos, sHTML, ">", vbBinaryCompare)
  58. If Epos = 0 Then Exit Do
  59.  
  60. sTAG = Mid(sHTML, Ipos, (Epos - Ipos) + 1)
  61. 'AddText Obj, sTag
  62. AddText Obj, sTXT
  63. 'format the view to match the html
  64. DoHTMLFormatting Obj, sTAG
  65. 'print the text
  66. 'If Trim(sTXT) <> "" Then
  67.  
  68. 'End If
  69. Loop
  70. Obj.Visible = True
  71.  
  72.  
  73. End Sub
  74.  
  75. Private Sub DoHTMLFormatting(Obj As PictureBox, sTAG As String)
  76. Dim sTXT As String
  77. Dim Ipos As Long
  78. sTXT = LCase(sTAG)
  79.  
  80. Select Case sTXT
  81. Case Is = "<br>", "<p>", "</p>" ' add new line
  82.     AddNewLine Obj
  83. Case Is = "<b>" ' start bold face
  84.     SetBold Obj, True
  85. Case Is = "</b>" ' stop bold face
  86.     SetBold Obj, False
  87. Case Is = "<i>" 'start italics
  88.     SetItalic Obj, True
  89. Case Is = "</i>" 'stop italics
  90.     SetItalic Obj, False
  91. Case Is = "<u>" 'start uderline
  92.     SetUnderLine Obj, True
  93. Case Is = "</u>" 'stop underline
  94.     SetUnderLine Obj, False
  95. Case Is = "</a>" 'end link
  96.     EndLink Obj
  97. Case Is = "</font>" 'font reset
  98.     SetDefaultColors True
  99.     SetBold Obj, False
  100.     SetItalic Obj, False
  101.     SetUnderLine Obj, False
  102.     SetFontColor Obj, lTextColor
  103.     SetFontSize Obj, lFontSize
  104.     SetFontFace Obj, sFontFace
  105. Case Else
  106.     'here we process all other types of complex tags like links, fonts, and pictures
  107.     If InStr(1, sTXT, "<font") <> 0 Then
  108.     'it is a font command
  109.     ProcessFont Obj, sTXT
  110.     End If
  111.     If InStr(1, sTXT, "<a ") <> 0 Then
  112.     'it is a link command
  113.     ProcessLink Obj, sTXT
  114.     End If
  115.     If InStr(1, sTXT, "<img ") <> 0 Then
  116.     'it is a image command
  117.     
  118.     End If
  119. End Select
  120.  
  121. End Sub
  122.  
  123. Sub EndLink(Obj As PictureBox)
  124. 'End the Hyperlink
  125.     isInLink = False
  126.     SetUnderLine Obj, bUnderLinned
  127.     SetFontColor Obj, OldColor
  128. End Sub
  129.  
  130. Sub ProcessLink(Obj As PictureBox, sTXT As String)
  131. Dim ILink As Long
  132. Dim sLink As String
  133. Dim rc As RECT
  134. 'Set the link flag
  135. isInLink = True
  136.  
  137. 'Underline the link and change the color to the link color
  138. OldColor = Obj.ForeColor
  139. bUnderLinned = Obj.Font.Bold
  140. SetUnderLine Obj, True
  141. SetFontColor Obj, lLinkColor
  142.     
  143. 'Add the new link
  144. ILink = UBound(AllLinks) + 1
  145. ReDim Preserve AllLinks(ILink)
  146.  
  147. 'Get Link location
  148. sTAG = GetHTMLEle(sTXT, "href=" & Chr(34), Chr(34))
  149. sTAG = Replace(sTAG, Chr(34) & ">", "")
  150. If sTAG <> "" Then
  151. sLink = sTAG
  152. End If
  153.  
  154. LLastLink = ILink
  155. With AllLinks(ILink)
  156. .sLink = sLink
  157. .rBounds.Left = TOldX * Screen.TwipsPerPixelX
  158. .rBounds.Top = TOldY * Screen.TwipsPerPixelY
  159. End With
  160.  
  161. End Sub
  162. Private Sub ProcessFont(Obj As PictureBox, sTXT As String)
  163. Dim sTAG As String
  164.  
  165. 'Get the Font Color
  166. sTAG = GetHTMLEle(sTXT, "color=" & Chr(34), Chr(34))
  167. sTAG = Replace(sTAG, "#", "")
  168. If sTAG <> "" Then
  169. Obj.ForeColor = MakeHexRGB(sTAG)
  170. End If
  171.  
  172. 'Get the Font Face
  173. sTAG = GetHTMLEle(sTXT, "face=" & Chr(34), Chr(34))
  174. If sTAG <> "" Then
  175. Obj.Font.Name = sTAG
  176. End If
  177.  
  178. 'Get the Font Size
  179. sTAG = GetHTMLEle(sTXT, "size=" & Chr(34), Chr(34))
  180. sTAG = StripNonNumeric(sTAG)
  181. If sTAG <> "" Then
  182. Obj.Font.Size = GetHTMLFontSize(CLng(sTAG))
  183. End If
  184. End Sub
  185.  
  186. Private Function GetHTMLFontSize(lNUm As Long) As Long
  187. Select Case lNUm
  188. Case Is = 1
  189.     GetHTMLFontSize = 8
  190. Case Is = 2
  191.     GetHTMLFontSize = 10
  192. Case Is = 3
  193.     GetHTMLFontSize = 12
  194. Case Is = 4
  195.     GetHTMLFontSize = 14
  196. Case Is = 5
  197.     GetHTMLFontSize = 18
  198. Case Is = 6
  199.     GetHTMLFontSize = 24
  200. Case Is = 7
  201.     GetHTMLFontSize = 36
  202. Case Else
  203. GetHTMLFontSize = lFontSize
  204. End Select
  205. End Function
  206.  
  207. Private Function StripNonNumeric(sTXT As String) As Long
  208. Dim sFinal As String
  209. Dim I As Long
  210. Dim S As String
  211. For I = 1 To Len(sTXT)
  212. S = Mid(sTXT, I, 1)
  213. If IsNumeric(S) = True Then
  214. sFinal = sFinal & S
  215. End If
  216. Next I
  217. If Len(sFinal) = 0 Then sFinal = "0"
  218. StripNonNumeric = CLng(sFinal)
  219. End Function
  220.  
  221. Private Sub AddText(Obj As PictureBox, sTXT As String)
  222. 'Adds plain text to the HTML View
  223. Dim rc As RECT
  224. Dim lWidth As Long
  225. Dim lHeight As Long
  226.  
  227. AlignFonts Obj, Form1.Label1
  228. Form1.Label1.Caption = sTXT
  229. lWidth = (Form1.Label1.Width / Screen.TwipsPerPixelX) + TOldX 'ViewMargin
  230. lHeight = TOldY + (Form1.Label1.Height / Screen.TwipsPerPixelY) + ViewMargin
  231. rc.Top = TOldY
  232. rc.Left = TOldX
  233. rc.Right = lWidth
  234. rc.Bottom = lHeight
  235. If (TOldX * Screen.TwipsPerPixelX) + Form1.Label1.Width >= Obj.Width Then
  236. Obj.Width = (TOldX * Screen.TwipsPerPixelX) + Form1.Label1.Width + ((ViewMargin * Screen.TwipsPerPixelX) * 2)
  237. End If
  238. TCurrentY = DrawText(Obj.hdc, sTXT, -1, rc, DT_LEFT)
  239. 'TOldY = TOldY + LineBreakHeight
  240. TOldX = rc.Right
  241. 'Obj.Line (TOldX * Screen.TwipsPerPixelX, TOldY * Screen.TwipsPerPixelY)-(lWidth * Screen.TwipsPerPixelX, TOldY * Screen.TwipsPerPixelY), vbRed
  242. Obj.Refresh
  243. If isInLink = True Then
  244. With AllLinks(LLastLink)
  245. .rBounds.Right = .rBounds.Right + Form1.Label1.Width
  246. .rBounds.Bottom = Form1.Label1.Height
  247. End With
  248. End If
  249. End Sub
  250.  
  251. Private Sub AlignFonts(sOBJ As PictureBox, sLBL As Label)
  252. 'Makes the label the same font as  the picturebox
  253.  
  254. With sLBL
  255. .AutoSize = True
  256. .Font.Bold = sOBJ.Font.Bold
  257. .Font.Charset = sOBJ.Font.Charset
  258. .Font.Italic = sOBJ.Font.Italic
  259. .Font.Name = sOBJ.Font.Name
  260. .Font.Size = sOBJ.Font.Size
  261. .Font.Strikethrough = sOBJ.Font.Strikethrough
  262. .Font.Underline = sOBJ.Font.Underline
  263. .Font.Weight = sOBJ.Font.Weight
  264. End With
  265. End Sub
  266.  
  267.  
  268.  
  269. Private Sub AddNewLine(Obj As PictureBox)
  270. 'Adds a vbCrlf / <BR> to the HTML View
  271. TOldY = TOldY + LineBreakHeight * 2
  272. TOldX = ViewMargin
  273. If TOldY * Screen.TwipsPerPixelY >= Obj.Height Then
  274. Obj.Height = (TOldY * Screen.TwipsPerPixelY)
  275. End If
  276. 'obj.Height = obj.Height + LineBreakHeight + ViewMargin
  277. End Sub
  278.  
  279. Public Sub RenderHTML(Obj As PictureBox)
  280. 'Windows 95/98/Me: len(sHTML) = This number may not exceed 8192.
  281. Dim rc As RECT
  282. Dim result As Long
  283.  
  284.  
  285. rc.Top = ViewMargin / Screen.TwipsPerPixelY
  286. rc.Left = ViewMargin / Screen.TwipsPerPixelX
  287. rc.Bottom = (Obj.Height / Screen.TwipsPerPixelY) - rc.Top
  288. rc.Right = (Obj.Width / Screen.TwipsPerPixelX) - rc.Left
  289.  
  290. 'get the current height, if it is bigger then the canvas, then resize canvas
  291. TCurrentY = CSng(DrawText(Obj.hdc, sHTML, -1, rc, DT_CALCRECT)) * Screen.TwipsPerPixelY
  292. If TCurrentY >= Obj.Height Then AddNewLine Obj
  293. 'draw the text
  294. TCurrentY = CSng(DrawText(Obj.hdc, sHTML, -1, rc, DT_CHARSTREAM)) * Screen.TwipsPerPixelY
  295.  
  296.  
  297. End Sub
  298.  
  299. Public Sub ProcessBODY(Obj As PictureBox)
  300. 'Find the <BODY> tag and get all the colors and the background
  301. Dim Ipos As Long
  302. Dim Epos As Long
  303. Dim sBODY As String
  304. Dim sTAG As String
  305. sHTML = Replace(sHTML, "<Body", "<BODY")
  306. sHTML = Replace(sHTML, "<body", "<BODY")
  307. Ipos = InStr(1, sHTML, "<BODY")
  308. If Ipos = 0 Then Exit Sub
  309. Epos = InStr(Ipos, sHTML, ">")
  310. If Epos = 0 Then Exit Sub
  311.  
  312. sBODY = Mid(sHTML, Ipos, Epos - Ipos + 1)
  313. sBODY = Replace(sBODY, Chr(34), "")
  314. sBODY = LCase(sBODY)
  315. sTAG = GetHTMLEle(sBODY, "background=", " ")
  316.  
  317. If CheckFile(sDir & sTAG) = True Then
  318. sBGImage = sDir & sTAG
  319. Form1.PicImage.Picture = LoadPicture(sDir & sTAG)
  320. End If
  321.  
  322. sTAG = GetHTMLEle(sBODY, "bgcolor=", " ")
  323. sTAG = Replace(sTAG, "#", "")
  324. If sTAG <> "" Then
  325. lBGCOLOR = MakeHexRGB(sTAG)
  326. Obj.BackColor = lBGCOLOR
  327. End If
  328.  
  329. sTAG = GetHTMLEle(sBODY, "text=", " ")
  330. sTAG = Replace(sTAG, "#", "")
  331. If sTAG <> "" Then
  332. lTextColor = MakeHexRGB(sTAG)
  333. Obj.ForeColor = lTextColor
  334. End If
  335.  
  336. sTAG = GetHTMLEle(sBODY, "link=", " ")
  337. sTAG = Replace(sTAG, "#", "")
  338. If sTAG <> "" Then
  339. lLinkColor = MakeHexRGB(sTAG)
  340. End If
  341.  
  342. sTAG = GetHTMLEle(sBODY, "vlink=", " ")
  343. sTAG = Replace(sTAG, "#", "")
  344. If sTAG <> "" Then
  345. lVisitedColor = MakeHexRGB(sTAG)
  346. End If
  347.  
  348. sTAG = GetHTMLEle(sBODY, "alink=", " ")
  349. sTAG = Replace(sTAG, "#", "")
  350. If sTAG <> "" Then
  351. lActiveColor = MakeHexRGB(sTAG)
  352. End If
  353. End Sub
  354.  
  355.