1 7 package test; 8 9 import org.eclipse.swt.*; 10 import org.eclipse.swt.layout.*; 11 import org.eclipse.swt.widgets.*; 12 import org.eclipse.swt.ole.win32.*; 13 import org.eclipse.swt.internal.ole.win32.*; 14 15 public class Ole { 16 17 public static void main(String [] args) { 18 19 final Display display = new Display(); 20 Shell shell = new Shell(display); 21 shell.setLayout(new FillLayout()); 22 shell.setText("Ole�����"); 23 OleControlSite controlSite; 24 try { 25 OleFrame frame = new OleFrame(shell, SWT.NONE); 26 controlSite = new OleControlSite(frame, SWT.NONE, "Shell.Explorer"); 27 controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); 28 } catch (SWTError e) { 29 System.out.println("Unable to open activeX control"); 30 return; 31 } 32 shell.open(); 33 34 final OleAutomation webBrowser = new OleAutomation(controlSite); 36 37 int DownloadComplete = 104; 39 controlSite.addEventListener(DownloadComplete, new OleListener() { 40 public void handleEvent(OleEvent event) { 41 int[] htmlDocumentID = webBrowser.getIDsOfNames(new String []{"Document"}); 42 if (htmlDocumentID == null) return; 43 Variant pVarResult = webBrowser.getProperty(htmlDocumentID[0]); 44 if (pVarResult == null || pVarResult.getType() == 0) return; 45 OleAutomation htmlDocument = pVarResult.getAutomation(); 47 48 EventDispatch myDispatch = new EventDispatch(EventDispatch.onclick); 50 IDispatch idispatch = new IDispatch(myDispatch.getAddress()); 51 Variant dispatch = new Variant(idispatch); 52 htmlDocument.setProperty(EventDispatch.onclick, dispatch); 53 54 myDispatch = new EventDispatch(EventDispatch.ondblclick); 55 idispatch = new IDispatch(myDispatch.getAddress()); 56 dispatch = new Variant(idispatch); 57 htmlDocument.setProperty(EventDispatch.ondblclick, dispatch); 58 59 myDispatch = new EventDispatch(EventDispatch.onkeydown); 60 idispatch = new IDispatch(myDispatch.getAddress()); 61 dispatch = new Variant(idispatch); 62 htmlDocument.setProperty(EventDispatch.onkeydown, dispatch); 63 64 htmlDocument.dispose(); 66 } 67 }); 68 69 int[] ids = webBrowser.getIDsOfNames(new String []{"Navigate", "URL"}); 71 Variant[] rgvarg = new Variant[] {new Variant(ClassLoader.getSystemResource("test2.pdf").toString())}; 72 int[] rgdispidNamedArgs = new int[]{ids[1]}; 73 webBrowser.invoke(ids[0], rgvarg, rgdispidNamedArgs); 74 75 while (!shell.isDisposed()) { 76 if (!display.readAndDispatch()) 77 display.sleep(); 78 } 79 webBrowser.dispose(); 81 display.dispose(); 82 83 } 84 } 85 class EventDispatch { 88 private COMObject iDispatch; 89 private int refCount = 0; 90 private int eventID; 91 92 final static int onhelp = 0x8001177d; 93 final static int onclick = 0x80011778; 94 final static int ondblclick = 0x80011779; 95 final static int onkeyup = 0x80011776; 96 final static int onkeydown = 0x80011775; 97 final static int onkeypress = 0x80011777; 98 final static int onmouseup = 0x80011773; 99 final static int onmousedown = 0x80011772; 100 final static int onmousemove = 0x80011774; 101 final static int onmouseout = 0x80011771; 102 final static int onmouseover = 0x80011770; 103 final static int onreadystatechange = 0x80011789; 104 final static int onafterupdate = 0x80011786; 105 final static int onrowexit= 0x80011782; 106 final static int onrowenter = 0x80011783; 107 final static int ondragstart = 0x80011793; 108 final static int onselectstart = 0x80011795; 109 110 EventDispatch(int eventID) { 111 this.eventID = eventID; 112 createCOMInterfaces(); 113 } 114 int getAddress() { 115 return iDispatch.getAddress(); 116 } 117 private void createCOMInterfaces() { 118 iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 4, 8}){ 119 public int method0(int[] args) {return QueryInterface(args[0], args[1]);} 120 public int method1(int[] args) {return AddRef();} 121 public int method2(int[] args) {return Release();} 122 public int method6(int[] args) {return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);} 126 }; 127 } 128 private void disposeCOMInterfaces() { 129 if (iDispatch != null) 130 iDispatch.dispose(); 131 iDispatch = null; 132 133 } 134 private int AddRef() { 135 refCount++; 136 return refCount; 137 } 138 private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult, int pExcepInfo, int pArgErr) { 139 switch (eventID) { 140 case onhelp: System.out.println("onhelp"); break; 141 case onclick: System.out.println("onclick"); break; 142 case ondblclick: System.out.println("ondblclick"); break; 143 case onkeyup: System.out.println("onkeyup"); break; 144 case onkeydown: System.out.println("onkeydown"); break; 145 case onkeypress: System.out.println("onkeypress"); break; 146 case onmouseup: System.out.println("onmouseup"); break; 147 case onmousedown: System.out.println("onmousedown"); break; 148 case onmousemove: System.out.println("onmousemove"); break; 149 case onmouseout: System.out.println("onmouseout"); break; 150 case onmouseover: System.out.println("onmouseover"); break; 151 case onreadystatechange: System.out.println("onreadystatechange"); break; 152 case onafterupdate: System.out.println("onafterupdate"); break; 153 case onrowexit: System.out.println("onrowexit"); break; 154 case onrowenter: System.out.println("onrowenter"); break; 155 case ondragstart: System.out.println("ondragstart"); break; 156 case onselectstart: System.out.println("onselectstart"); break; 157 } 158 return COM.S_OK; 159 } 160 private int QueryInterface(int riid, int ppvObject) { 161 if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG; 162 GUID guid = new GUID(); 163 COM.MoveMemory(guid, riid, GUID.sizeof); 164 165 if ( COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDispatch)) { 166 COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4); 167 AddRef(); 168 return COM.S_OK; 169 } 170 COM.MoveMemory(ppvObject, new int[] {0}, 4); 171 return COM.E_NOINTERFACE; 172 } 173 int Release() { 174 refCount--; 175 if (refCount == 0) { 176 disposeCOMInterfaces(); 177 } 178 return refCount; 179 } 180 }
| Popular Tags
|