HtmlService.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. unit HtmlService;
  2. interface
  3. uses
  4. Classes, SysUtils, Forms, SHDocVw, Graphics, MSHTML, StrUtils, Variants, ActiveX;
  5. type
  6. THtmlService = class
  7. private
  8. procedure InsertHTML(Webbrowser: TWebbrowser; HTML: string);
  9. procedure GoBottom(Webbrowser: TWebbrowser);
  10. public
  11. procedure ShowSendMessageTooQuickly(Webbrowser: TWebbrowser);
  12. procedure AddMessageToWebBrowser(SenderID: String;
  13. SenderName, FontStr, MessageStr: String;
  14. SendDateTime: TDateTime;
  15. var NoFoundFaces: TStringList;
  16. Webbrowser: TWebbrowser;
  17. ShowSendFailed: Boolean = False;
  18. IsHistory: Boolean = False);
  19. procedure SetDOMStyle(Doc: IHTMLDocument2; AWindowColor: TColor);
  20. function FilterHTMLCode(HTML: string; AllowURL: Boolean = True): string;
  21. class function GetService: THtmlService; static;
  22. end;
  23. implementation
  24. uses
  25. Windows, xFonts, DateUtils, Types, UsersService, FaceService, RealICQColors;
  26. var
  27. AHtmlService: THtmlService;
  28. const
  29. InfomationPicture: String = 'Images\Info.gif';
  30. procedure THtmlService.GoBottom(Webbrowser: TWebbrowser);
  31. var
  32. MaxScrollHeight: Integer;
  33. DoC: IHTMLDocument2;
  34. begin
  35. Doc := Webbrowser.Document as IHTMLDocument2;
  36. Application.ProcessMessages;
  37. Sleep(10);
  38. Application.ProcessMessages;
  39. try
  40. MaxScrollHeight := Doc.body.getAttribute('ScrollHeight', 0) + 10000; //获得滚动条最大高度
  41. Webbrowser.OleObject.Document.ParentWindow.ScrollBy(0, MaxScrollHeight); //滚动到最右最下
  42. Application.ProcessMessages;
  43. Webbrowser.OleObject.Document.ParentWindow.ScrollBy(0, MaxScrollHeight); //滚动到最右最下
  44. except
  45. end;
  46. end;
  47. function THtmlService.FilterHTMLCode(HTML:String; AllowURL: Boolean = True):String;
  48. var
  49. UrlStart,UrlEnd,iLoop:Integer;
  50. TempStr:String;
  51. begin
  52. HTML := AnsiReplaceStr(HTML,'&', '&');
  53. HTML := AnsiReplaceStr(HTML,'<', '&lt;');
  54. HTML := AnsiReplaceStr(HTML,'>', '&gt;');
  55. HTML := AnsiReplaceStr(HTML,#13, '&nbsp;<br>');
  56. HTML := AnsiReplaceStr(HTML,#32, '&nbsp;');
  57. if AllowURL then
  58. begin
  59. TempStr := '';
  60. UrlStart := AnsiPos('http://',HTML);
  61. while UrlStart>0 do
  62. begin
  63. TempStr := TempStr + Copy(HTML, 1, UrlStart - 1);
  64. HTML := Copy(HTML, UrlStart, Length(HTML));
  65. UrlEnd := AnsiPos('&nbsp;', HTML);
  66. if UrlEnd <= 0 then
  67. begin
  68. for iLoop:=1 to length(HTML) do
  69. begin
  70. if ByteType(HTML,iLoop) <> mbSingleByte then
  71. begin
  72. UrlEnd := iLoop;
  73. Break;
  74. end;
  75. end;
  76. end;
  77. if UrlEnd <= 0 then UrlEnd := Length(HTML)+1;
  78. TempStr := TempStr + '<a href="' + Copy(HTML,1,UrlEnd-1) + '" target="_blank"><font color="#0000ff">' + Copy(HTML,1,UrlEnd-1) + '</font></a>';
  79. HTML := Copy(HTML, UrlEnd, Length(HTML));
  80. UrlStart := AnsiPos('http://', HTML);
  81. end;
  82. TempStr := TempStr + Copy(HTML, 1, Length(HTML));
  83. Result := TempStr;
  84. end
  85. else
  86. begin
  87. Result := HTML;
  88. end;
  89. end;
  90. class function THtmlService.GetService: THtmlService;
  91. begin
  92. if AHtmlService = nil then
  93. AHtmlService := THtmlService.Create;
  94. Result := AHtmlService;
  95. end;
  96. procedure THtmlService.ShowSendMessageTooQuickly(Webbrowser: TWebbrowser);
  97. var
  98. AHtml: string;
  99. begin
  100. AHtml := '<table width="100%" style="font-size:9pt;border:0px; padding:2px; color:#990000; margin-top:2px;margin-bottom:5px;"><tr><td>';
  101. AHtml := AHtml+'<img src="' + ExtractFilePath(Application.ExeName) + InfomationPicture + '" align="texttop"> ';
  102. AHtml := AHtml+'<span>';
  103. AHtml := AHtml + '抱歉,您发送消息的速度太快了!';
  104. AHtml := AHtml + '</span>';
  105. AHtml := AHtml + '</td></tr></table>';
  106. InsertHTML(Webbrowser, AHtml);
  107. end;
  108. procedure THtmlService.InsertHTML(Webbrowser: TWebbrowser; HTML: String);
  109. var
  110. DoC: IHTMLDocument2;
  111. begin
  112. Doc := Webbrowser.Document as IHTMLDocument2;
  113. Doc.body.innerHTML:=Doc.body.innerHTML + HTML;
  114. GoBottom(Webbrowser);
  115. end;
  116. procedure THtmlService.AddMessageToWebBrowser(SenderID: String;
  117. SenderName, FontStr, MessageStr: String;
  118. SendDateTime: TDateTime;
  119. var NoFoundFaces: TStringList;
  120. Webbrowser: TWebbrowser;
  121. ShowSendFailed: Boolean = False;
  122. IsHistory: Boolean = False);
  123. var
  124. MsgContent,
  125. HexString,
  126. HTML,
  127. SenderColor: String;
  128. TextFont: TFont;
  129. ID:String;
  130. begin
  131. ID := IntToStr(GetTickCount);
  132. TextFont := TFont.Create;
  133. StringToFont(FontStr, TextFont);
  134. MsgContent :=FilterHTMLCode(SenderName);
  135. MsgContent := MsgContent +'(<a href="OpenRightMenu,'+SenderId+'">个人信息</a>)';
  136. if CompareDate(Now, SendDateTime) = EqualsValue then
  137. MsgContent := MsgContent +' '+ TimeToStr(SendDateTime)
  138. else
  139. MsgContent := MsgContent +' '+DateTimeToStr(SendDateTime);
  140. if ShowSendFailed then
  141. MsgContent := MsgContent + '(消息发送超时)';
  142. if not IsHistory then
  143. SenderColor := '#009900'
  144. else
  145. SenderColor := '#686868';
  146. HTML := '<DIV style="padding-bottom:2px; padding-top:2px; color:' + SenderColor + '">' + MsgContent +'</DIV>';
  147. HTML := HTML + '<DIV style="padding-left:9px; padding-bottom:2px;';
  148. //设置字体
  149. HTML := HTML + ';font-family:' + TextFont.Name;
  150. HexString := IntToHex(TextFont.Color, 6); //获取颜色的16进制格式
  151. HTML := HTML + ';color:#' + Copy(HexString,5,2) + Copy(HexString,3,2) + Copy(HexString,1,2); //将BGR颜色转换为RGB颜色
  152. HTML := HTML + ';font-size:' + IntToStr(TextFont.Size) + 'pt';
  153. if fsBold in TextFont.Style then HTML := HTML + ';font-weight:bold';
  154. if fsItalic in TextFont.Style then HTML := HTML + ';font-style:italic';
  155. HTML := HTML + ';text-decoration:';
  156. if fsUnderline in TextFont.Style then HTML := HTML + ' underline ';
  157. if fsStrikeOut in TextFont.Style then HTML := HTML + ' line-through ';
  158. MsgContent := FilterHTMLCode(MessageStr); //过滤HTML代码
  159. MsgContent := TFaceService.GetService.ParseToHtml(MsgContent, SenderID, NoFoundFaces);
  160. HTML := HTML + '"><span id="' + ID + '">' + MsgContent + '</span> </DIV>';
  161. InsertHTML(WebBrowser, HTML);
  162. end;
  163. procedure THtmlService.SetDOMStyle(Doc:IHTMLDocument2; AWindowColor: TColor);
  164. var
  165. v: Variant;
  166. CurrentColor,
  167. CssColor: String;
  168. begin
  169. v := VarArrayCreate([0, 0], varVariant);
  170. v[0] := '<html dir="ltr" lang="zh">'
  171. + '<head>'
  172. + '<META http-equiv="Content-Type" content="text/html; charset=gb2312">'
  173. + '<body link="#0000FF" vlink="#0000FF" alink="#0000FF" hlink="#0000FF" bgcolor="#FFFFFF" oncontextmenu="location.href=''PopMenu'';return false;" >'
  174. + '</body>'
  175. + '</head>';
  176. doc.write(PSafeArray(TVarData(v).VArray));
  177. try
  178. CurrentColor := IntToHex(ConvertColorToColor($00CDCDCD, AWindowColor), 6);
  179. CssColor := '#' + Copy(CurrentColor,5,2) + Copy(CurrentColor,3,2) + Copy(CurrentColor,1,2);
  180. except
  181. end;
  182. Doc.body.language := 'gb2312';
  183. Doc.body.style.cssText:='SCROLLBAR-FACE-COLOR:' + CssColor + ';' +
  184. 'SCROLLBAR-HIGHLIGHT-COLOR: ButtonHighLight;' +
  185. 'SCROLLBAR-SHADOW-COLOR: ButtonShadow;' +
  186. 'SCROLLBAR-ARROW-COLOR: #333333;' +
  187. 'SCROLLBAR-3DLIGHT-COLOR:' + CssColor + ';' +
  188. 'SCROLLBAR-TRACK-COLOR:' + CssColor + ';' +
  189. 'SCROLLBAR-DARKSHADOW-COLOR:' + CssColor + ';' +
  190. 'word-break: break-all;' +
  191. 'background-attachment: fixed;' +
  192. 'background-repeat: no-repeat;' +
  193. 'background-position: left top;';
  194. end;
  195. initialization
  196. finalization
  197. if AHtmlService <> nil then
  198. FreeAndNil(AHtmlService);
  199. end.