结构体声明
[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("kernel32.dll")] public static extern IntPtr GlobalLock(IntPtr hMem); [DllImport("kernel32.dll")] public static extern bool GlobalUnlock(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(); } }
标签:IntPtr,剪贴板,AutoCAD,string,int,static,NativeMethods,net,public From: https://www.cnblogs.com/Exaybachay/p/16721519.html