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

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

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

    如鵬網 大學生計算機學習社區

    CowNew開源團隊

    http://www.cownew.com 郵件請聯系 about521 at 163.com

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      363 隨筆 :: 2 文章 :: 808 評論 :: 0 Trackbacks
    SWT中調用Automation的方式 這篇文章中我們介紹了SWT中通過Automation訪問IE的方式,但是只是簡單的URL導航,沒有自動填表單、自動提交等功能。我們對其進行了升級,采用了新的操作方式,充分利用了SWT對OLE的支持,裁減掉大量代碼。現在可以實現自動填表單、自動提交等功能。不過暫時還無法響應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創建一個進程外Automation服務器
       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 自動填表()
     {
      //"kw"為關鍵字輸入框
      HtmlElement txtKW = ie.getElementById("kw");
      //自動填表
      txtKW.setProperty("value", "楊中科");
      HtmlElement btnSB = ie.getElementById("sb");
      //自動點擊【百度一下】按鈕自動提交查詢
      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");
      //等待加載完畢,正確的方式應該是在網頁onComplete的時候繼續執行,但是沒弄明白OLE 的EventSink機制怎么搞到SWT中來
      //所以先湊合著Sleep循環檢測getBusy()的值,當不busy的時候再進行后續處理
      while(ie.getBusy())
      {
       Thread.sleep(10);
      }
      msgBox(ie.getInnerHtml());
      //"sb"為【百度一下】這個按鈕的id
      HtmlElement btnSB = ie.getElementById("sb");
      //取value屬性
      String txt = btnSB.getPropertyAsString("value");
      msgBox("按鈕上的文字:"+txt);
      msgBox("網址:"+ie.getLocationURL());
      
      composite.addDisposeListener(new DisposeListener() {
       public void widgetDisposed(DisposeEvent e)
       {
        //必須手動指定退出,否則會報異常
        ie.quit();
       }
      });
     }

    posted on 2008-06-30 21:08 CowNew開源團隊 閱讀(1408) 評論(2)  編輯  收藏

    評論

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

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

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


    發出來的Type,有沒有手冊可以查

    我的郵箱414149609@qq.com  回復  更多評論
      


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产色爽女小说免费看| 四虎影视久久久免费| 黄页网站在线看免费| 亚洲色偷偷偷网站色偷一区| 久久久久久免费一区二区三区| 亚洲综合另类小说色区色噜噜| 2022国内精品免费福利视频 | 免费观看久久精彩视频| 亚洲欧洲日产国码无码网站 | 国产特黄一级一片免费| 中文字幕亚洲天堂| 国产白丝无码免费视频| 亚洲黄网在线观看| 猫咪社区免费资源在线观看 | 91青青国产在线观看免费| 亚洲第一二三四区| 在线免费视频一区| 免费人成网站永久| 国产av天堂亚洲国产av天堂 | 亚洲最大的成人网| 四虎永久免费观看| 成人性生交大片免费看好| 亚洲成年人电影在线观看| 四虎成人免费网址在线| 国产成人精品免费视频大全| 亚洲AV福利天堂一区二区三| 2021久久精品免费观看| 色吊丝免费观看网站| 亚洲国产a∨无码中文777| 久久久久国产精品免费免费搜索| 国产亚洲女在线线精品| 亚洲人成网站在线播放影院在线 | 污污的视频在线免费观看| 亚洲成色WWW久久网站| 91在线品视觉盛宴免费| 91av免费在线视频| 亚洲AV无码精品蜜桃| 亚洲精品专区在线观看| 最近免费字幕中文大全视频| 无码天堂亚洲国产AV| 久久亚洲精品中文字幕|