delphi 嵌入网页中的相关操作

应用程序通过内嵌浏览器的方式,利用js的window.external可以达到控制窗体行为的效果,如控制窗体宽高大小,透明度,网页截屏,网页读写文件,网页控制桌面行为等,最近利用DELPHI就实现了这一点,一般软件可以是很小巧,2,3百K的样子,如果大家有谁需要的,我将无偿提供这方面的源代码,因为这方面的应用目前还不是很广(QQ Zone用的这样的技术),所以我就不在这里给出代码了。我扩展的window.external函数如下:

Pascal核心代码片段:

Java代码 
  1. unit CusExternal;  
  2.   
  3. {$WARN SYMBOL_PLATFORM OFF}  
  4.   
  5. interface  
  6.   
  7. uses  
  8.   Windows,ComObj, ActiveX, tlink_TLB, StdVcl,CoolTrayIcon,forms,SysUtils,Dialogs;  
  9.   
  10.   
  11.   
  12. type  
  13.   TTBrowserToDelphi = class(TAutoObject, ITBrowserToDelphi)  
  14.   protected  
  15.     procedure setWndTitle(title: OleVariant); safecall;  
  16.     procedure modifyWndSize(w, h: OleVariant); safecall;  
  17.     procedure flashIcon; safecall;  
  18.     procedure popHint(str, title: OleVariant); safecall;  
  19.     procedure closeWnd; safecall;  
  20.     procedure setIconTitle(title: OleVariant); safecall;  
  21.     procedure setSb(i: Integer; title: OleVariant); safecall;  
  22.     procedure setSbWidth(i, w: Integer); safecall;  
  23.     procedure popHintAdv(str, title: OleVariant; style, secs: Integer);  
  24.       safecall;  
  25.     procedure formTitleControl(flag: Integer); safecall;  
  26.     function showMessage(flag: Integer; hit: OleVariant;  
  27.       iconflag: Integer): OleVariant; safecall;  
  28.     procedure minTotray; safecall;  
  29.     procedure moveBtnPos(sleft, stop, swidth, sheight, isVisible: Integer);  
  30.       safecall;  
  31.     function getMinStatus: OleVariant; safecall;  
  32.     function cameraScreen(flag: Integer): OleVariant; safecall;  
  33.     procedure speak(str: OleVariant); safecall;  
  34.     function uploadToFtp(remoteAddr, localPath: OleVariant): OleVariant;  
  35.       safecall;  
  36.   end;  
  37.   
  38. implementation  
  39.   
  40.   
  41. uses ComServ,login;  
  42.   
  43. var  
  44.   iflag:boolean=true;  
  45.   
  46. procedure TTBrowserToDelphi.setWndTitle(title: OleVariant);  
  47. begin  
  48.    mainForm.Caption:=title;  
  49. end;  
  50.   
  51. procedure TTBrowserToDelphi.modifyWndSize(w, h: OleVariant);  
  52. begin  
  53.     mainForm.Width:=w;  
  54.     mainForm.Height:=h;  
  55.     mainForm.Position:=poScreenCenter;  
  56.     mainForm.Update;  
  57. end;  
  58.   
  59. procedure TTBrowserToDelphi.flashIcon;  
  60. begin  
  61.       mainForm.CoolTrayIcon1.IconList := mainForm.ImageList1;  
  62.       mainForm.CoolTrayIcon1.CycleInterval := 400;  
  63.       mainForm.CoolTrayIcon1.CycleIcons := True;  
  64. end;  
  65.   
  66. procedure TTBrowserToDelphi.popHint(str, title: OleVariant);  
  67. begin  
  68.      mainForm.CoolTrayIcon1.ShowBalloonHint(title,  
  69.         str,  
  70.         bitInfo, 10);  
  71. end;  
  72.   
  73. procedure TTBrowserToDelphi.closeWnd;  
  74. begin  
  75.     //mainForm.Close;  
  76.     Application.Terminate;  
  77. end;  
  78.   
  79. procedure TTBrowserToDelphi.setIconTitle(title: OleVariant);  
  80. begin  
  81.     mainForm.CoolTrayIcon1.Hint:=title;  
  82. end;  
  83.   
  84. procedure TTBrowserToDelphi.setSb(i: Integer; title: OleVariant);  
  85. begin  
  86.     if(i>1) then i:=1;  
  87.     mainForm.sb.Panels[i].Text:=title;  
  88. end;  
  89.   
  90. procedure TTBrowserToDelphi.setSbWidth(i, w: Integer);  
  91. begin  
  92.     if(i>1) then i:=1;  
  93.     mainForm.sb.Panels[i].Width:=w;  
  94. end;  
  95.   
  96. procedure TTBrowserToDelphi.popHintAdv(str, title: OleVariant; style,  
  97.   secs: Integer);  
  98. begin  
  99.      if secs<1 then secs:=1  
  100.      else if secs>100 then secs:=100  
  101.      else ;  
  102.   
  103.      mainForm.Timer1.Interval:= secs*1000;  
  104.      mainForm.Timer1.Enabled:=true;  
  105.   
  106.      if style = 1 then  
  107.        mainForm.CoolTrayIcon1.ShowBalloonHint(title,  
  108.         str,  
  109.         bitError, secs)  
  110.      else if style = 2 then  
  111.        mainForm.CoolTrayIcon1.ShowBalloonHint(title,  
  112.         str,  
  113.         bitWarning, secs)  
  114.      else if style=3 then  
  115.         mainForm.CoolTrayIcon1.ShowBalloonHint(title,  
  116.         str,  
  117.         bitNone, secs)  
  118.      else  
  119.      mainForm.CoolTrayIcon1.ShowBalloonHint(title,  
  120.         str,  
  121.         bitInfo, secs);  
  122.   
  123.      //mainForm.CoolTrayIcon1.HideBalloonHint;  
  124. end;  
  125.   
  126. procedure TTBrowserToDelphi.formTitleControl(flag: Integer);  
  127. begin  
  128.    if(flag=0) then  
  129.    begin  
  130.      with mainform do  
  131.      begin  
  132.        SetWindowLong(Handle,                   // 当前窗体句柄  
  133.                 GWL_STYLE,                // 表示当前是要设置新的窗体(普通)样式  
  134.                 // 得到指定窗体信息  
  135.                 GetWindowLong(Handle, GWL_STYLE)  
  136.                   and (not WS_CAPTION));  // 去掉样式(s)中的“标题”样式  
  137.        Height := ClientHeight;  
  138.        Width := ClientWidth;  
  139.      end;  
  140.    end  
  141.    else if(flag=2) then  
  142.        mainForm.BorderStyle:=bsDialog  
  143.    else if(flag=3) then  
  144.        mainForm.BorderStyle:=bsSingle  
  145.    else if(flag=4) then //窗口获得焦点  
  146.    begin  
  147.        if(not mainForm.IsMinimized) then  
  148.        begin  
  149.          SetForegroundWindow(mainForm.Handle);  
  150.        end  
  151.        else  
  152.        begin  
  153.          with mainForm do  
  154.          begin  
  155.            FloatRectangles(False, True);  
  156.            CoolTrayIcon1.ShowMainForm;  
  157.            IsMinimized := False;  
  158.          end;  
  159.        end;  
  160.    end  
  161.    else if(flag=5) then //停止闪烁  
  162.    begin  
  163.        mainForm.CoolTrayIcon1.Icon:=Application.Icon;  
  164.        mainForm.CoolTrayIcon1.CycleIcons:=false  
  165.    end  
  166.    else if(flag=6) then   //任务栏闪烁  
  167.      mainForm.FlashMe  
  168.    else  
  169.        ;             
  170. end;  
  171.   
  172. function TTBrowserToDelphi.showMessage(flag: Integer; hit: OleVariant;  
  173.   iconflag: Integer): OleVariant;  
  174. begin  
  175.   if(not iflag) then exit;  
  176.   
  177.   iflag:=false;//锁定函数  
  178.   
  179.   if(flag=0) then     //无返回值提醒  
  180.   begin  
  181.      if iconflag=1 then  
  182.        messagedlg(hit,mterror,[mbok],0)  
  183.      else if iconflag=2 then  
  184.        messagedlg(hit,mtwarning,[mbok],0)  
  185.      else  
  186.        messagedlg(hit,mtinformation,[mbok],0);  
  187.      result:=0;  
  188.   end  
  189.   else  
  190.   begin  
  191.     if(iconflag=1) then //yes+no+cancel  
  192.       result:=messagedlg(hit,mtConfirmation,[mbyes,mbno,mbcancel],0)  
  193.     else if(iconflag=2) then //ok+cancel  
  194.       result:=messagedlg(hit,mtConfirmation,[mbok,mbcancel],0)  
  195.     else if(iconflag=3) then //ignore+retry+cancel  
  196.       result:=messagedlg(hit,mtConfirmation,[mbIgnore,mbRetry,mbcancel],0)  
  197.     else  
  198.       result:=messagedlg(hit,mtConfirmation,[mbyes,mbno],0);  
  199.   end;  
  200.   
  201.   iflag:=true;//释放锁定  
  202.   //result:=1;  
  203. end;  
  204.   
  205.   
  206.   
  207. procedure TTBrowserToDelphi.minTotray;  
  208. begin  
  209.   with mainform do  
  210.   begin  
  211.         FloatRectangles(True, True);  
  212.         CoolTrayIcon1.HideMainForm;  
  213.         IsMinimized:=true;  
  214.   end;  
  215. end;  
  216.   
  217.   
  218.   
  219. procedure TTBrowserToDelphi.moveBtnPos(sleft, stop, swidth, sheight,  
  220.   isVisible: Integer);  
  221. begin  
  222.     with mainform do  
  223.     begin  
  224.       bitbtn1.Width:=swidth;  
  225.       bitbtn1.Height:=sheight;  
  226.       bitbtn1.Left:=sleft;  
  227.       bitbtn1.Top:=stop;  
  228.       if(isVisible=1) then  
  229.         bitbtn1.Visible:=true  
  230.       else  
  231.         bitbtn1.visible:=false;  
  232.     end;  
  233. end;  
  234.   
  235. function TTBrowserToDelphi.getMinStatus: OleVariant;  
  236. begin  
  237.     result:=mainform.IsMinimized;  
  238. end;  
  239.   
  240. function TTBrowserToDelphi.cameraScreen(flag: Integer): OleVariant;  
  241. begin  
  242.    result:=mainform.jp(flag);  
  243. end;  
  244.   
  245. procedure TTBrowserToDelphi.speak(str: OleVariant);  
  246. begin  
  247.    mainForm.Speak(str);  
  248. end;  
  249.   
  250. function TTBrowserToDelphi.uploadToFtp(remoteAddr,  
  251.   localPath: OleVariant): OleVariant;  
  252. var  
  253.   str1,str2:string;  
  254. begin  
  255.   str1:= remoteAddr;  
  256.   str2:= localPath;  
  257.   result:=mainForm.uploadToFtp2(pchar(str1),pchar(str2));  
  258. end;  
  259.   
  260. initialization  
  261.   TAutoObjectFactory.Create(ComServer, TTBrowserToDelphi, Class_TBrowserToDelphi,  
  262.     ciMultiInstance, tmApartment);  
  263. end.  
 

HTML接口代码:

Html代码 
  1. <HTML>  
  2. <HEAD>  
  3. <TITLE> New Document </TITLE>  
  4. <meta http-equiv="Content-Type" content="text/html;charset=gb2312">  
  5. <title> New Document </title>  
  6. <meta name="Generator" content="EditPlus">  
  7. <meta name="Author" content="Neil Chen(木野狐)">  
  8. <meta name="keywords" content="">  
  9. <meta name="description" content="">  
  10. <script>  
  11.   function pop(arg)  
  12.   {  
  13.    //返回值说明:6:点击yes  7:点击no 2:点击cancel  1:点击ok  4:点击retry  5:点击ignore  
  14.    var ret=window.external.showMessage(1,'询问1',arg);  
  15.    alert(ret);  
  16.    var str="pop('"+arg+"')";  
  17.    setTimeout(str,3000);  
  18.   }  
  19.   function isMin()  
  20.   {  
  21.     var ismin=window.external.getMinStatus();  
  22.     alert(ismin);  
  23.     setTimeout("isMin()",3000);  
  24.   }  
  25.   function winFocus()  
  26.   {  
  27.     setTimeout("aa()",3000);  
  28.   }  
  29.   function aa()  
  30.   {  
  31.     window.external.formTitleControl(4);    
  32.   }  
  33.   function jp(flag)  
  34.   {  
  35.     var ii=window.external.cameraScreen(flag);  
  36.     var str;  
  37.     if(ii!="")  
  38.         {  
  39.           str=window.external.uploadToFtp('/uploadfiles/jt/',ii);  
  40.           alert(str);  
  41.           //window.open('sendpic.html');    
  42.         }  
  43.     else  
  44.         alert("用户取消操作");    
  45.   }  
  46. </script>  
  47. </HEAD>  
  48. <BODY>  
  49. <BUTTON ONCLICK="window.external.modifyWndSize(800,600)">调整大小600*400</BUTTON>  
  50. <BUTTON ONCLICK="window.external.modifyWndSize(300,200)">调整大小300*200</BUTTON>  
  51. <BUTTON ONCLICK="window.external.setWndTitle('我是标题')">标题</BUTTON>  
  52. <BUTTON ONCLICK="window.external.flashIcon()">图标闪烁</BUTTON>  
  53. <BUTTON ONCLICK="window.external.popHint('我是内容','我是标题')">POP提示</BUTTON>  
  54. <BUTTON ONCLICK="window.external.popHintAdv('我是内容','我是标题',0,3)">信息图标POP提示3秒</BUTTON>  
  55. <BUTTON ONCLICK="window.external.popHintAdv('我是内容','我是标题',1,4)">错误图标POP提示4秒</BUTTON>  
  56. <BUTTON ONCLICK="window.external.popHintAdv('我是内容','我是标题',2,6)">警告图标POP提示6秒</BUTTON>  
  57. <BUTTON ONCLICK="window.external.popHintAdv('我是内容','我是标题',3,20)">无图标POP提示20秒</BUTTON>  
  58. <BUTTON ONCLICK="window.external.closeWnd()">关闭程序</BUTTON>  
  59. <BUTTON ONCLICK="window.external.setIconTitle('我是图标标题哦')">图标标题</BUTTON>  
  60. <BUTTON ONCLICK="window.external.setSb(0,'提示信息')">状态栏文字信息1</BUTTON>  
  61. <BUTTON ONCLICK="window.external.setSb(1,'设置第一个状态栏文字2222')">状态栏文字信息2</BUTTON>  
  62. <BUTTON ONCLICK="window.external.setSbWidth(0,200)">设置状态栏宽度</BUTTON>  
  63. <BUTTON ONCLICK="window.external.formTitleControl(0)">窗口无标题栏型</BUTTON>  
  64. <BUTTON ONCLICK="window.external.formTitleControl(3)">窗口普通型</BUTTON>  
  65. <BUTTON ONCLICK="window.external.formTitleControl(2)">窗口对话框型</BUTTON>  
  66.   
  67. <BUTTON ONCLICK="window.external.showMessage(0,'提示1',2)">无返回值提示对话框警告图标</BUTTON>  
  68. <BUTTON ONCLICK="window.external.showMessage(0,'提示2',1)">无返回值提示对话框错误图标</BUTTON>  
  69. <BUTTON ONCLICK="window.external.showMessage(0,'提示3',0)">无返回值提示对话框信息图标</BUTTON>  
  70. <BUTTON ONCLICK="pop(0)">yes+no询问对话框</BUTTON>  
  71. <BUTTON ONCLICK="pop(1)">yes+no+cancel询问对话框</BUTTON>  
  72. <BUTTON ONCLICK="pop(2)">ok+cancel询问对话框</BUTTON>  
  73. <BUTTON ONCLICK="pop(3)">ignore+retry+cancel询问对话框</BUTTON>  
  74.   
  75. <BUTTON ONCLICK="window.external.minTotray()">窗口最小化到托盘</BUTTON>  
  76.   
  77. <BUTTON ONCLICK="isMin()">获取最小化状态</BUTTON>  
  78.   
  79. <BUTTON ONCLICK="winFocus()">窗口获取焦点</BUTTON>  
  80.   
  81. <BUTTON ONCLICK="window.external.formTitleControl(5)">托盘图标停止闪烁</BUTTON>  
  82.   
  83. <BUTTON ONCLICK="window.external.formTitleControl(6)">任务栏闪烁</BUTTON>  
  84.   
  85.   
  86. <BUTTON ONCLICK="jp(0)">普通截屏</BUTTON>  
  87. <BUTTON ONCLICK="jp(1)">屏幕截屏</BUTTON>  
  88. <BUTTON ONCLICK="jp(2)">软件截屏</BUTTON>  
  89.   
  90. <BUTTON ONCLICK="window.external.speak('Welcome to you!')">我要说话(英文提示语)</BUTTON>  
  91. <BUTTON ONCLICK="window.external.speak('欢迎您使用!')">我要说话(中文提示语)</BUTTON>  
  92.   
  93. <BUTTON ONCLICK="window.external.speak('欢迎您使用!')">我要说话(中文提示语)</BUTTON>  
  94.   
  95. </BODY>  
  96. </HTML>  
posted @ 2009-12-02 09:27  azhai  阅读(2618)  评论(0编辑  收藏  举报