结构体声明
[StructLayout(LayoutKind.Sequential)]
struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
};
[StructLayout(LayoutKind.Sequential, Pack=8)]
struct Point3D
{
public double x;
public double y;
public double z;
};
[StructLayout(LayoutKind.Sequential, Pack=8, CharSet = CharSet.Unicode)]
struct ClipboardInfo
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szTempFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szSourceFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string szSignature;
public int nFlags;
public Point3D dptInsert;
public Rect rectGDI;
public IntPtr mpView;
public int dwThreadId;
public int nLen;
public int nType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string chData;
};
class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
public static extern bool CloseClipboard();
[DllImport("user32.dll")]
public static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("user32.dll")]
public static extern uint RegisterClipboardFormat(string lpszFormat);
[DllImport("user32.dll")]
public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
public static extern bool EmptyClipboard();
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalAlloc(uint uFlags, int dwBytes);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalFree(IntPtr hMem);
};
读取
static void showClipboard(StringBuilder sb)
{
uint cf = NativeMethods.RegisterClipboardFormat("AutoCAD.r22");
if (NativeMethods.OpenClipboard(IntPtr.Zero)) {
IntPtr data = NativeMethods.GetClipboardData(cf);
IntPtr ptr = NativeMethods.GlobalLock(data);
ClipboardInfo ci = (ClipboardInfo)Marshal.PtrToStructure(ptr,
typeof(ClipboardInfo));
sb.AppendLine();
sb.AppendLine(string.Format("szTempFile={0}", ci.szTempFile));
sb.AppendLine(string.Format("dptInsert: {0}, {1}, {2}", ci.dptInsert.x, ci.dptInsert.y, ci.dptInsert.z));
NativeMethods.GlobalUnlock(data);
NativeMethods.CloseClipboard();
}
}
设置
public static int setClipboard()
{
uint cf = NativeMethods.RegisterClipboardFormat("AutoCAD.r22");
int size = Marshal.SizeOf(typeof(ClipboardInfo));
IntPtr hglobal = NativeMethods.GlobalAlloc(0x0002, size);
if (NativeMethods.OpenClipboard(IntPtr.Zero)) {
NativeMethods.EmptyClipboard();
IntPtr ptr = NativeMethods.GlobalLock(hglobal);
ClipboardInfo ci;
ci.szTempFile = "abcd.txt";
ci.szSourceFile = "efgh.txt";
ci.szSignature = "R15";
ci.nFlags = 0;
ci.dptInsert.x=ci.dptInsert.y=ci.dptInsert.z=1.0;
ci.rectGDI.left = 0;
ci.rectGDI.top = 0;
ci.rectGDI.right = 20;
ci.rectGDI.bottom = 30;
ci.mpView = IntPtr.Zero;
ci.dwThreadId = 124;
ci.nLen = 0;
ci.nType = 0;
ci.chData = string.Empty;
Marshal.StructureToPtr(ci, ptr, true);
NativeMethods.GlobalUnlock(hglobal);
NativeMethods.SetClipboardData(cf, hglobal);
NativeMethods.CloseClipboard();
}
NativeMethods.GlobalFree(hglobal);
return 1;
}