<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    如鵬網(wǎng) 大學(xué)生計(jì)算機(jī)學(xué)習(xí)社區(qū)

    CowNew開(kāi)源團(tuán)隊(duì)

    http://www.cownew.com 郵件請(qǐng)聯(lián)系 about521 at 163.com

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      363 隨筆 :: 2 文章 :: 808 評(píng)論 :: 0 Trackbacks
    SWT中調(diào)用Automation的方式 這篇文章中我們介紹了SWT中通過(guò)Automation訪問(wèn)IE的方式,但是只是簡(jiǎn)單的URL導(dǎo)航,沒(méi)有自動(dòng)填表單、自動(dòng)提交等功能。我們對(duì)其進(jìn)行了升級(jí),采用了新的操作方式,充分利用了SWT對(duì)OLE的支持,裁減掉大量代碼。現(xiàn)在可以實(shí)現(xiàn)自動(dòng)填表單、自動(dòng)提交等功能。不過(guò)暫時(shí)還無(wú)法響應(yīng)IE的事件。
    核心代碼如下:

    package test;

    import org.eclipse.swt.SWTException;
    import org.eclipse.swt.internal.ole.win32.COM;
    import org.eclipse.swt.internal.ole.win32.GUID;
    import org.eclipse.swt.internal.ole.win32.IUnknown;
    import org.eclipse.swt.ole.win32.OLE;
    import org.eclipse.swt.ole.win32.OleClientSite;
    import org.eclipse.swt.widgets.Composite;

    /**
     *
     * @author 楊中科
     *
     */
    public class AutomationClientSite extends OleClientSite
    {
     public AutomationClientSite(Composite parent, int style, String progId)
     {
      super(parent, style);
      try
      {
       appClsid = getClassID(progId);
       if (appClsid == null)
        OLE.error(OLE.ERROR_INVALID_CLASSID);

       //使用CoCreateInstance創(chuàng)建一個(gè)進(jìn)程外Automation服務(wù)器
       int[] address = new int[1];
       int result = COM.CoCreateInstance(getClassID(progId), 0,
         COM.CLSCTX_INPROC_SERVER | COM.CLSCTX_LOCAL_SERVER,
         COM.IIDIUnknown, address);
       if (result != COM.S_OK)
        OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);

       objIUnknown = new IUnknown(address[0]);

      } catch (SWTException e)
      {
       dispose();
       disposeCOMInterfaces();
       throw e;
      }
     }

     protected GUID getClassID(String progId)
     {
      GUID guid = new GUID();
      // create a null terminated array of char
      char[] buffer = null;
      if (progId != null)
      {
       int count = progId.length();
       buffer = new char[count + 1];
       progId.getChars(0, count, buffer, 0);
      }
      if (COM.CLSIDFromProgID(buffer, guid) != COM.S_OK)
      {
       int result = COM.CLSIDFromString(buffer, guid);
       if (result != COM.S_OK)
        OLE.error(result);
      }
      return guid;
     }

    }
    public class OleUtils
    {
     public static int getIdOfName(OleAutomation auto, String name)
     {
      int[] ret = auto.getIDsOfNames(new String[]{name});
      return ret[0];
     }
     
     public static void setProperty(OleAutomation auto, String name,Variant value)
     {
      int id = getIdOfName(auto, name);
      auto.setProperty(id, new Variant[]{value});
     }
     
     public static Variant getProperty(OleAutomation auto, String name)
     {
      int id = getIdOfName(auto, name);
      return auto.getProperty(id);
     }
     
     public static Variant invoke(OleAutomation auto, String name)
     {
      return invoke(auto,name,new Variant[0]);
     }
     
     public static Variant invoke(OleAutomation auto, String name,Variant... params)
     {
      int id = getIdOfName(auto, name);
      return auto.invoke(id,params);
     }
    }



    package test;

    import org.eclipse.swt.ole.win32.OleAutomation;
    import org.eclipse.swt.ole.win32.Variant;

    /**
     *
     * @author 楊中科
     *
     */
    public class HtmlElement
    {
     private OleAutomation auto;

     public HtmlElement(OleAutomation auto)
     {
      this.auto = auto;
     }

     protected OleAutomation getOleAutomation()
     {
      return auto;
     }

     public void setProperty(String name, Variant value)
     {
      OleUtils.setProperty(auto, name, value);
     }

     public Variant getPropertyAsVariant(String name)
     {
      Variant value = OleUtils.getProperty(auto, name);
      return value;
     }

     public void setProperty(String name, int value)
     {
      OleUtils.setProperty(auto, name, new Variant(value));
     }

     public int getPropertyAsInt(String name)
     {
      Variant value = OleUtils.getProperty(auto, name);
      return value.getInt();
     }

     public void setProperty(String name, boolean value)
     {
      OleUtils.setProperty(auto, name, new Variant(value));
     }

     public boolean getPropertyAsBool(String name)
     {
      Variant value = OleUtils.getProperty(auto, name);
      return value.getBoolean();
     }

     public void setProperty(String name, String value)
     {
      OleUtils.setProperty(auto, name, new Variant(value));
     }

     public String getPropertyAsString(String name)
     {
      Variant value = OleUtils.getProperty(auto, name);
      return value.getString();
     }

     public HtmlElement getPropertyAsHtmlElement(String name)
     {
      Variant value = OleUtils.getProperty(auto, name);
      return new HtmlElement(value.getAutomation());
     }

     public Variant invoke(String name,Variant... params)
     {
      return OleUtils.invoke(auto, name,params);
     }

     public int invoke_Int(String name,Variant... params)
     {
      return invoke(name,params).getInt();
     }

     public boolean invoke_Bool(String name,Variant... params)
     {
      return invoke(name,params).getBoolean();
     }

     public String invoke_String(String name,Variant... params)
     {
      return invoke(name,params).getString();
     }

     public HtmlElement invoke_HtmlElement(String name,Variant... params)
     {
      return new HtmlElement(invoke(name,params).getAutomation());
     }
    }




    package test;

    import org.eclipse.swt.ole.win32.OleAutomation;
    import org.eclipse.swt.ole.win32.OleClientSite;
    import org.eclipse.swt.ole.win32.Variant;

    /**
     * 更多方法參考MSDN“InternetExplorer Object”文檔
     *
     * @author 楊中科
     *
     */
    public class IEAutomation extends HtmlElement
    {

     public IEAutomation(OleClientSite clientSite)
     {
      super(new OleAutomation(clientSite));
     }

     public void setVisible(boolean value)
     {
      setProperty("Visible", value);
     }

     public boolean isVisible()
     {
      return getPropertyAsBool("Visible");
     }

     public void setMenuBar(boolean value)
     {
      setProperty("MenuBar", value);
     }

     public boolean isMenuBar()
     {
      return getPropertyAsBool("MenuBar");
     }

     public void setStatusBar(boolean value)
     {
      setProperty("StatusBar", value);
     }

     public boolean isStatusBar()
     {
      return getPropertyAsBool("StatusBar");
     }
     
     public void setToolBar(boolean value)
     {
      setProperty("ToolBar", value);
     }

     public boolean isToolBar()
     {
      return getPropertyAsBool("ToolBar");
     }
     
     public int getHWND()
     {
      return getPropertyAsInt("HWND");
     }
     
     public String getReadyState()
     {
      return getPropertyAsString("ReadyState");
     }
     
     public String getLocationURL()
     {
      return getPropertyAsString("LocationURL");
     }
     
     public boolean getBusy()
     {
      return getPropertyAsBool("Busy");
     }

     public void navigate(String url)
     {
      invoke("Navigate", new Variant(url));
     }

     public HtmlElement getDocument()
     {
      return getPropertyAsHtmlElement("Document");
     }

     public String getInnerHtml()
     {
      HtmlElement document = getPropertyAsHtmlElement("Document");
      HtmlElement body = document.getPropertyAsHtmlElement("body");
      return body.getPropertyAsString("innerHtml");
     }

     public HtmlElement getElementById(String id)
     {
      HtmlElement document = getDocument();
      return document.invoke_HtmlElement("getElementById", new Variant(id));
     }

     public void quit()
     {
      invoke("Quit");
     }
    }




    使用范例:

     protected void 自動(dòng)填表()
     {
      //"kw"為關(guān)鍵字輸入框
      HtmlElement txtKW = ie.getElementById("kw");
      //自動(dòng)填表
      txtKW.setProperty("value", "楊中科");
      HtmlElement btnSB = ie.getElementById("sb");
      //自動(dòng)點(diǎn)擊【百度一下】按鈕自動(dòng)提交查詢
      btnSB.invoke("click");
     }
     private void automation() throws Exception
     {
      OleFrame frame = new OleFrame(composite, SWT.NONE);
      AutomationClientSite client =
       new AutomationClientSite(frame,SWT.NONE,"InternetExplorer.Application");   
     
      ie = new IEAutomation(client);
      ie.setVisible(true);
      ie.setMenuBar(false);
      ie.setToolBar(false);
      ie.setStatusBar(false);
      
      int hwnd = ie.getHWND();
      OS.SetParent(hwnd, composite.handle);
      // 窗口最大化
      OS.SendMessage(hwnd, OS.WM_SYSCOMMAND, OS.SC_MAXIMIZE, 0);
      OS.SendMessage(hwnd, OS.WM_ACTIVATE, 0, 0);
      
      ie.navigate("http://www.baidu.com");
      //等待加載完畢,正確的方式應(yīng)該是在網(wǎng)頁(yè)onComplete的時(shí)候繼續(xù)執(zhí)行,但是沒(méi)弄明白OLE 的EventSink機(jī)制怎么搞到SWT中來(lái)
      //所以先湊合著Sleep循環(huán)檢測(cè)getBusy()的值,當(dāng)不busy的時(shí)候再進(jìn)行后續(xù)處理
      while(ie.getBusy())
      {
       Thread.sleep(10);
      }
      msgBox(ie.getInnerHtml());
      //"sb"為【百度一下】這個(gè)按鈕的id
      HtmlElement btnSB = ie.getElementById("sb");
      //取value屬性
      String txt = btnSB.getPropertyAsString("value");
      msgBox("按鈕上的文字:"+txt);
      msgBox("網(wǎng)址:"+ie.getLocationURL());
      
      composite.addDisposeListener(new DisposeListener() {
       public void widgetDisposed(DisposeEvent e)
       {
        //必須手動(dòng)指定退出,否則會(huì)報(bào)異常
        ie.quit();
       }
      });
     }

    posted on 2008-06-30 21:08 CowNew開(kāi)源團(tuán)隊(duì) 閱讀(1409) 評(píng)論(2)  編輯  收藏

    評(píng)論

    # re: SWT中通過(guò)Automatioin的方式訪問(wèn)IE(升級(jí)版) 2010-10-19 17:01 apple
    寫得很好,不過(guò)我調(diào)試了,怎么有些問(wèn)題,樓主能否把 源碼發(fā)一份給我,
    調(diào)試看看效果。 非常感謝 gongyg at gmail.com.  回復(fù)  更多評(píng)論
      

    # re: SWT中通過(guò)Automatioin的方式訪問(wèn)IE(升級(jí)版) 2011-09-10 15:25 科哥
    科哥, 你的例子很好,但是請(qǐng)問(wèn)一下怎樣知道一個(gè)IE平時(shí)發(fā)出哪些事件,在哪里有得查,
    client.addListener(new Listener()
    {
    public void handleEvent(Event event)
    {

    System.out.println(event.type);
    System.out.println();
    }
    });


    發(fā)出來(lái)的Type,有沒(méi)有手冊(cè)可以查

    我的郵箱414149609@qq.com  回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 污污视频免费观看网站| 亚洲一区二区三区在线| 黄色毛片免费网站| 免费一看一级毛片| 亚洲永久在线观看| 黄色大片免费网站| 免费成人av电影| eeuss草民免费| 国产亚洲3p无码一区二区| 亚洲欧美国产国产一区二区三区 | 免费黄色网址入口| 精品亚洲成在人线AV无码| 午夜宅男在线永久免费观看网| 亚洲综合一区国产精品| 四虎成人免费影院网址| 一本色道久久综合亚洲精品高清| 亚洲一级黄色大片| 成年女人毛片免费播放视频m| 怡红院亚洲红怡院在线观看| 亚洲国产综合无码一区二区二三区 | 看免费毛片天天看| 亚洲一区无码精品色| a视频在线观看免费| 亚洲av再在线观看| 亚洲欧美日韩久久精品| 日韩精品成人亚洲专区| 亚洲精品理论电影在线观看| 精品福利一区二区三区免费视频| 亚洲日韩涩涩成人午夜私人影院| 国产精品黄页免费高清在线观看| 亚洲AV午夜成人片| 成年私人影院免费视频网站| 黄页网站在线视频免费| 亚洲成AV人在线观看天堂无码| 永久免费精品影视网站| 亚洲国产综合精品中文第一区 | 50岁老女人的毛片免费观看| 91精品国产亚洲爽啪在线影院 | 一级毛片成人免费看免费不卡| 亚洲乱码一区av春药高潮| 91精品国产免费久久国语麻豆|