import java.awt.*; import java.io.*; import com.ms.wfc.ole32.*; import com.ms.wfc.win32.*; import com.ms.wfc.app.*; import com.ms.wfc.ui.Metafile; import com.ms.win32.*; import com.ms.com.*; import com.ms.awt.GraphicsX; /** * This class exposes a static method that allows you to paste * any awt heavyweight component to the clpboard as a metafile, * If used with swing, you must pass in a JFrame or JApplet, * * It needs a separate thread due to the fact that ole hangs if the * current thread is blocked - the thread ends when you paste! */ public class ClipPaster implements IDataObject, Serializable, Runnable { Component c; /** * This static method allows you to paste * any awt heavyweight component to the clpboard as a metafile, * If used with swing, you must pass in a JFrame or JApplet. */ public static void pasteToClipboard(Component c) { ClipPaster clip = new ClipPaster(c); (new Thread(clip)).start(); } public void run() { Clipboard.setDataObject(this); MSG msg = new MSG(); // must dispatch win32 messages until getData is called while (User32.GetMessage(msg, 0, 0, 0)) { User32.DispatchMessage(msg); } // this loop breaks when getData is called - need to add a timer //System.out.println("run exited"); } // this is not created by users - use pasteToClipboard(Component) public ClipPaster(Component comp) { c = comp; } // component MUST be a heavyweight component! protected int getMetaFile() { // in memory metafile to pass in data object int mfInt = Gdi32.CreateEnhMetaFile(0, null, null, null); // this is a Graphics object on an hDC GraphicsX g = new GraphicsX(mfInt); // this causes the component to draw its kids to the metafile c.printAll(g); // close and obtain a handle int hMF = Gdi32.CloseEnhMetaFile(mfInt); // must disconnect the graphics object from the hDC g.dispose(); // return the handle - Clipboard will delete return hMF; } // start IDataObject implementation public boolean getDataPresent(String s) { if (s.equals(DataFormats.CF_ENHMETAFILE)) return true; return false; } public boolean getDataPresent(Class c) { if (c.equals(Metafile.class)) return true; return false; } public Object getData(String s) { //System.out.println("getData"); try { if (s.equals(DataFormats.CF_ENHMETAFILE)) { return new Metafile(getMetaFile()); } } finally { // exit the thread User32.PostQuitMessage(0); } return null; } public Object getData(Class c) { if (c.equals(Metafile.class)) { return getData(DataFormats.CF_ENHMETAFILE); } return null; } public void setData(String s, Object o) {} public void setData(Class c, Object o) {} public void setData(Object o) {} public String[] getFormats() { //System.out.println("getFormats"); return new String[] { DataFormats.CF_ENHMETAFILE }; } // end IDataObject implementation /** * to run this, run the class in jview, go to excel or word and * just hit ^V (paste) - The line: ClipPaster.pasteToClipboard(f) * is the one that you should use in other apps. */ public static void main(String[] args) { Frame f = new Frame("test"); Button b = new Button("Hello World"); f.setLayout(new FlowLayout()); f.add(b); f.pack(); f.show(); ClipPaster.pasteToClipboard(f); } }