?? clipboard.java
字號(hào):
* </ul> */public void setContents(Object[] data, Transfer[] dataTypes) { checkWidget(); if (data == null || dataTypes == null || data.length != dataTypes.length) { DND.error(SWT.ERROR_INVALID_ARGUMENT); } this.data = data; this.transferAgents = dataTypes; int result = COM.OleSetClipboard(this.iDataObject.getAddress()); /* * Bug in Windows. When a new application takes control * of the clipboard, other applications may open the * clipboard to determine if they want to record the * clipoard updates. When this happens, the clipboard * can not be flushed until the other application is * finished. To allow other applications to get the * data, use PeekMessage() to enable cross thread * message sends. */ int retryCount = 0; while (result != COM.S_OK && retryCount++ < 10) { try { Thread.sleep(50);} catch (Throwable t) {} MSG msg = new MSG(); COM.PeekMessage (msg, 0, 0, 0, OS.PM_NOREMOVE | OS.PM_NOYIELD); result = COM.OleSetClipboard(this.iDataObject.getAddress()); } if (result != COM.S_OK) { DND.error(DND.ERROR_CANNOT_SET_CLIPBOARD); }}private int AddRef() { refCount++; return refCount;}private void createCOMInterfaces() { // register each of the interfaces that this object implements iDataObject = new COMObject(new int[]{2, 0, 0, 2, 2, 1, 2, 3, 2, 4, 1, 1}){ public int method0(int[] args) {return QueryInterface(args[0], args[1]);} public int method1(int[] args) {return AddRef();} public int method2(int[] args) {return Release();} public int method3(int[] args) {return GetData(args[0], args[1]);} // method4 GetDataHere - not implemented public int method5(int[] args) {return QueryGetData(args[0]);} // method6 GetCanonicalFormatEtc - not implemented // method7 SetData - not implemented public int method8(int[] args) {return EnumFormatEtc(args[0], args[1]);} // method9 DAdvise - not implemented // method10 DUnadvise - not implemented // method11 EnumDAdvise - not implemented };}private void disposeCOMInterfaces() { if (iDataObject != null) iDataObject.dispose(); iDataObject = null;}private int EnumFormatEtc(int dwDirection, int ppenumFormatetc) { // only allow getting of data - SetData is not currently supported if (dwDirection == COM.DATADIR_SET) return COM.E_NOTIMPL; // what types have been registered? TransferData[] allowedDataTypes = new TransferData[0]; for (int i = 0; i < transferAgents.length; i++){ TransferData[] formats = transferAgents[i].getSupportedTypes(); TransferData[] newAllowedDataTypes = new TransferData[allowedDataTypes.length + formats.length]; System.arraycopy(allowedDataTypes, 0, newAllowedDataTypes, 0, allowedDataTypes.length); System.arraycopy(formats, 0, newAllowedDataTypes, allowedDataTypes.length, formats.length); allowedDataTypes = newAllowedDataTypes; } OleEnumFORMATETC enumFORMATETC = new OleEnumFORMATETC(); enumFORMATETC.AddRef(); FORMATETC[] formats = new FORMATETC[allowedDataTypes.length + 1]; for (int i = 0; i < allowedDataTypes.length; i++){ formats[i] = allowedDataTypes[i].formatetc; } // include the drop effect format to specify a copy operation FORMATETC dropeffect = new FORMATETC(); dropeffect.cfFormat = CFSTR_PREFERREDDROPEFFECT; dropeffect.dwAspect = COM.DVASPECT_CONTENT; dropeffect.lindex = -1; dropeffect.tymed = COM.TYMED_HGLOBAL; formats[formats.length -1] = dropeffect; enumFORMATETC.setFormats(formats); COM.MoveMemory(ppenumFormatetc, new int[] {enumFORMATETC.getAddress()}, 4); return COM.S_OK;}private int GetData(int pFormatetc, int pmedium) { /* Called by a data consumer to obtain data from a source data object. The GetData method renders the data described in the specified FORMATETC structure and transfers it through the specified STGMEDIUM structure. The caller then assumes responsibility for releasing the STGMEDIUM structure. */ if (pFormatetc == 0 || pmedium == 0) return COM.E_INVALIDARG; if (QueryGetData(pFormatetc) != COM.S_OK) return COM.DV_E_FORMATETC; TransferData transferData = new TransferData(); transferData.formatetc = new FORMATETC(); COM.MoveMemory(transferData.formatetc, pFormatetc, FORMATETC.sizeof); transferData.type = transferData.formatetc.cfFormat; transferData.stgmedium = new STGMEDIUM(); transferData.result = COM.E_FAIL; if (transferData.type == CFSTR_PREFERREDDROPEFFECT) { // specify that a copy operation is to be performed STGMEDIUM stgmedium = new STGMEDIUM(); stgmedium.tymed = COM.TYMED_HGLOBAL; stgmedium.unionField = COM.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, 4); COM.MoveMemory(stgmedium.unionField, new int[] {COM.DROPEFFECT_COPY}, 4); stgmedium.pUnkForRelease = 0; COM.MoveMemory(pmedium, stgmedium, STGMEDIUM.sizeof); return COM.S_OK; } // get matching transfer agent to perform conversion int transferIndex = -1; for (int i = 0; i < transferAgents.length; i++){ if (transferAgents[i].isSupportedType(transferData)){ transferIndex = i; break; } } if (transferIndex == -1) return COM.DV_E_FORMATETC; transferAgents[transferIndex].javaToNative(data[transferIndex], transferData); COM.MoveMemory(pmedium, transferData.stgmedium, STGMEDIUM.sizeof); return transferData.result;}private int QueryGetData(int pFormatetc) { if (transferAgents == null) return COM.E_FAIL; TransferData transferData = new TransferData(); transferData.formatetc = new FORMATETC(); COM.MoveMemory(transferData.formatetc, pFormatetc, FORMATETC.sizeof); transferData.type = transferData.formatetc.cfFormat; if (transferData.type == CFSTR_PREFERREDDROPEFFECT) return COM.S_OK; // is this type supported by the transfer agent? for (int i = 0; i < transferAgents.length; i++){ if (transferAgents[i].isSupportedType(transferData)) return COM.S_OK; } return COM.DV_E_FORMATETC;}private int QueryInterface(int riid, int ppvObject) { if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG; GUID guid = new GUID(); COM.MoveMemory(guid, riid, GUID.sizeof); if (COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDataObject) ) { COM.MoveMemory(ppvObject, new int[] {iDataObject.getAddress()}, 4); AddRef(); return COM.S_OK; } COM.MoveMemory(ppvObject, new int[] {0}, 4); return COM.E_NOINTERFACE;}private int Release() { refCount--; if (refCount == 0) { this.data = new Object[0]; this.transferAgents = new Transfer[0]; disposeCOMInterfaces(); COM.CoFreeUnusedLibraries(); } return refCount;}/** * Returns an array of the data types currently available on the system clipboard. Use * with Transfer.isSupportedType. * * @return array of TransferData * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see Transfer#isSupportedType * * @since 3.0 */public TransferData[] getAvailableTypes() { checkWidget(); FORMATETC[] types = _getAvailableTypes(); TransferData[] data = new TransferData[types.length]; for (int i = 0; i < types.length; i++) { data[i] = new TransferData(); data[i].type = types[i].cfFormat; data[i].formatetc = types[i]; } return data;}/** * Returns a platform specific list of the data types currently available on the * system clipboard. * * <p>Note: <code>getAvailableTypeNames</code> is a utility for writing a Transfer * sub-class. It should NOT be used within an application because it provides * platform specific information.</p> * * @return a platform specific list of the data types currently available on the * system clipboard * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public String[] getAvailableTypeNames() { checkWidget(); FORMATETC[] types = _getAvailableTypes(); String[] names = new String[types.length]; int maxSize = 128; for (int i = 0; i < types.length; i++){ TCHAR buffer = new TCHAR(0, maxSize); int size = COM.GetClipboardFormatName(types[i].cfFormat, buffer, maxSize); if (size != 0) { names[i] = buffer.toString(0, size); } else { switch (types[i].cfFormat) { case COM.CF_HDROP: names[i] = "CF_HDROP"; break; //$NON-NLS-1$ case COM.CF_TEXT: names[i] = "CF_TEXT"; break; //$NON-NLS-1$ case COM.CF_BITMAP: names[i] = "CF_BITMAP"; break; //$NON-NLS-1$ case COM.CF_METAFILEPICT: names[i] = "CF_METAFILEPICT"; break; //$NON-NLS-1$ case COM.CF_SYLK: names[i] = "CF_SYLK"; break; //$NON-NLS-1$ case COM.CF_DIF: names[i] = "CF_DIF"; break; //$NON-NLS-1$ case COM.CF_TIFF: names[i] = "CF_TIFF"; break; //$NON-NLS-1$ case COM.CF_OEMTEXT: names[i] = "CF_OEMTEXT"; break; //$NON-NLS-1$ case COM.CF_DIB: names[i] = "CF_DIB"; break; //$NON-NLS-1$ case COM.CF_PALETTE: names[i] = "CF_PALETTE"; break; //$NON-NLS-1$ case COM.CF_PENDATA: names[i] = "CF_PENDATA"; break; //$NON-NLS-1$ case COM.CF_RIFF: names[i] = "CF_RIFF"; break; //$NON-NLS-1$ case COM.CF_WAVE: names[i] = "CF_WAVE"; break; //$NON-NLS-1$ case COM.CF_UNICODETEXT: names[i] = "CF_UNICODETEXT"; break; //$NON-NLS-1$ case COM.CF_ENHMETAFILE: names[i] = "CF_ENHMETAFILE"; break; //$NON-NLS-1$ case COM.CF_LOCALE: names[i] = "CF_LOCALE"; break; //$NON-NLS-1$ case COM.CF_MAX: names[i] = "CF_MAX"; break; //$NON-NLS-1$ default: names[i] = "UNKNOWN"; } } } return names;}private FORMATETC[] _getAvailableTypes() { FORMATETC[] types = new FORMATETC[0]; int[] ppv = new int[1]; if (COM.OleGetClipboard(ppv) != COM.S_OK) return types; IDataObject dataObject = new IDataObject(ppv[0]); int[] ppFormatetc = new int[1]; int rc = dataObject.EnumFormatEtc(COM.DATADIR_GET, ppFormatetc); dataObject.Release(); if (rc != COM.S_OK)return types; IEnumFORMATETC enum = new IEnumFORMATETC(ppFormatetc[0]); // Loop over enumerator and save any types that match what we are looking for int rgelt = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, FORMATETC.sizeof); int[] pceltFetched = new int[1]; enum.Reset(); while (enum.Next(1, rgelt, pceltFetched) == COM.S_OK && pceltFetched[0] == 1) { FORMATETC formatetc = new FORMATETC(); COM.MoveMemory(formatetc, rgelt, FORMATETC.sizeof); FORMATETC[] newTypes = new FORMATETC[types.length + 1]; System.arraycopy(types, 0, newTypes, 0, types.length); newTypes[types.length] = formatetc; types = newTypes; } OS.GlobalFree(rgelt); enum.Release(); return types;}}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -