public ClipboardActions GetClipboardAction(ref Message m, ref string text, ref string format) { const int WM_PASTE = 0x0302; // defined in winuser.h const int WM_DRAWCLIPBOARD = 0x308; const int WM_CHANGECBCHAIN = 0x030D; switch (m.Msg) { case WM_DRAWCLIPBOARD: DisplayClipboardData(ref text, ref format); ClipboardNativeMethods.SendMessage(NextClipboardViewer, m.Msg, m.WParam, m.LParam); return(ClipboardActions.DrawClipboard); case WM_CHANGECBCHAIN: if (m.WParam == NextClipboardViewer) { NextClipboardViewer = m.LParam; } else { ClipboardNativeMethods.SendMessage(NextClipboardViewer, m.Msg, m.WParam, m.LParam); } return(ClipboardActions.ChangeChain); case WM_PASTE: return(ClipboardActions.Paste); default: return(ClipboardActions.None); } }
public static string GetClipboardText() { IntPtr hInstance = IntPtr.Zero; ClipboardNativeMethods.OpenClipboard(hInstance); IntPtr buffer = ClipboardNativeMethods.GetClipboardData(13); string text = Marshal.PtrToStringUni(buffer); ClipboardNativeMethods.CloseClipboard(); return(text); }
public static bool SetClipboardText(string text) { if (!ClipboardNativeMethods.OpenClipboard(IntPtr.Zero)) { return(false); } ClipboardNativeMethods.EmptyClipboard(); IntPtr alloc = MemoryNativeMethods.GlobalAlloc(MemoryNativeMethods.GMEM_MOVEABLE | MemoryNativeMethods.GMEM_DDESHARE, (UIntPtr)(sizeof(char) * (text.Length + 1))); IntPtr gLock = MemoryNativeMethods.GlobalLock(alloc); if ((int)text.Length > 0) { Marshal.Copy(text.ToCharArray(), 0, gLock, text.Length); } MemoryNativeMethods.GlobalUnlock(alloc); ClipboardNativeMethods.SetClipboardData(13, alloc); ClipboardNativeMethods.CloseClipboard(); return(true); }