Beispiel #1
0
        public static void clip_set(clip c)
        {
            if (c == null)
            {
                return;
            }
            if (c.Count == 0)
            {
                return;
            }
            WinAPI.OpenClipboard(IntPtr.Zero);
            WinAPI.EmptyClipboard();
            for (int i = 0; i < c.Count; i++)
            {
                Logging.Log("[clip] Setting: " + c.f[i] + " format.");
//				IntPtr hglob = Marshal.AllocHGlobal(c.d[i].Length);
                IntPtr alloc = WinAPI.GlobalAlloc(WinAPI.GMEM_MOVEABLE | WinAPI.GMEM_DDESHARE, new UIntPtr(Convert.ToUInt32(c.d[i].GetLength(0))));
                IntPtr glock = WinAPI.GlobalLock(alloc);
                Marshal.Copy(c.d[i], 0, glock, c.d[i].Length);
                WinAPI.GlobalUnlock(glock);
                WinAPI.SetClipboardData(c.f[i], alloc);
                WinAPI.GlobalFree(alloc);
//				Marshal.FreeHGlobal(hglob);
            }
            WinAPI.CloseClipboard();
        }
Beispiel #2
0
        /// <summary>
        /// Gets clipboard text if clipboard data contains text(CF_UNICODETEXT).
        /// </summary>
        /// <returns>string</returns>
        public static string GetText() // Gets text data from clipboard
        {
            if (!WinAPI.IsClipboardFormatAvailable(WinAPI.CF_UNICODETEXT))
            {
                return(null);
            }
            int    Tries  = 0;
            var    opened = false;
            string data   = null;

            while (true)
            {
                ++Tries;
                opened = WinAPI.OpenClipboard(IntPtr.Zero);
                var hGlobal = WinAPI.GetClipboardData(WinAPI.CF_UNICODETEXT);
                var lpwcstr = WinAPI.GlobalLock(hGlobal);
                data = Marshal.PtrToStringUni(lpwcstr);
                if (opened)
                {
                    WinAPI.GlobalUnlock(hGlobal);
                    break;
                }
                System.Threading.Thread.Sleep(1);
            }
            WinAPI.CloseClipboard();
            Logging.Log("Clipboard text was get.");
            return(data);
        }
Beispiel #3
0
        /// <summary>
        /// 클립보드에서 텍스트를 가져옵니다. 클립보드 열기 요청 실패 시 ClipboardManager.CannotOpenException 예외가 발생하고, 만약 텍스트가 존재하지 않을 경우 null을 반환합니다.
        /// </summary>
        public static string GetText(IntPtr clipboardOwner)
        {
            string text = null;

            bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);

            if (!isClipboardOpen)
            {
                throw new CannotOpenException();
            }
            IntPtr pointer = WinAPI.GetClipboardData(WinAPI.CF_UNICODETEXT);

            if (pointer == IntPtr.Zero)
            {
                pointer = WinAPI.GetClipboardData(WinAPI.CF_TEXT);
                if (pointer != IntPtr.Zero)
                {
                    text = Marshal.PtrToStringAnsi(pointer);
                }
            }
            else
            {
                text = Marshal.PtrToStringUni(pointer);
            }
            WinAPI.CloseClipboard();

            return(text);
        }
Beispiel #4
0
        /// <summary>
        /// 클립보드에 텍스트를 저장합니다. 클립보드 열기 요청 실패 시 ClipboardManager.CannotOpenException 예외가 발생합니다.
        /// </summary>
        /// <param name="text">저장할 텍스트</param>
        public static void SetText(string text, IntPtr clipboardOwner)
        {
            bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);

            if (!isClipboardOpen)
            {
                throw new CannotOpenException();
            }
            WinAPI.EmptyClipboard();
            WinAPI.SetClipboardData(WinAPI.CF_TEXT, Marshal.StringToHGlobalAnsi(text));
            WinAPI.SetClipboardData(WinAPI.CF_UNICODETEXT, Marshal.StringToHGlobalUni(text));
            WinAPI.CloseClipboard();
        }
Beispiel #5
0
        /// <summary>
        /// 백업했던 클립보드 데이터를 복구합니다. 현재 텍스트와 이미지만 복구 기능을 지원하며, 클립보드 열기 요청 실패 시 ClipboardManager.CannotOpenException 예외가 발생합니다.
        /// </summary>
        public static void RestoreData(IntPtr clipboardOwner)
        {
            if (!HasDataToRestore)
            {
                return;
            }

            if (Format == WinAPI.CF_TEXT || Format == WinAPI.CF_UNICODETEXT)
            {
                bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);
                if (!isClipboardOpen)
                {
                    throw new CannotOpenException();
                }
            }

            switch (Format)
            {
            case WinAPI.CF_TEXT:
                WinAPI.SetClipboardData(Format, MemoryHandle);
                break;

            case WinAPI.CF_UNICODETEXT:
                WinAPI.SetClipboardData(Format, MemoryHandle);
                break;

            case WinAPI.CF_BITMAP:
            case WinAPI.CF_DIB:
                Format = WinAPI.CF_BITMAP;
                SetImage(MemoryHandle, clipboardOwner);
                (Data as Bitmap).Dispose();
                break;
            }
            if (Format == WinAPI.CF_TEXT || Format == WinAPI.CF_UNICODETEXT)
            {
                WinAPI.CloseClipboard();
            }

            WinAPI.DeleteObject(MemoryHandle);
            Data             = null;
            MemoryHandle     = IntPtr.Zero;
            HasDataToRestore = false;
        }
Beispiel #6
0
        private static void _SetImage(Bitmap image, IntPtr clipboardOwner)
        {
            Bitmap tempImage = new Bitmap(image.Width, image.Height);

            using (Graphics graphics = Graphics.FromImage(tempImage))
            {
                IntPtr hScreenDC       = WinAPI.GetWindowDC(IntPtr.Zero);                                     // 기본적인 Device Context의 속성들을 카피하기 위한 작업
                IntPtr hDestDC         = WinAPI.CreateCompatibleDC(hScreenDC);
                IntPtr hDestBitmap     = WinAPI.CreateCompatibleBitmap(hScreenDC, image.Width, image.Height); // destDC와 destBitmap 모두 반드시 screenDC의 속성들을 기반으로 해야 함.
                IntPtr hPrevDestObject = WinAPI.SelectObject(hDestDC, hDestBitmap);

                IntPtr hSourceDC         = graphics.GetHdc();
                IntPtr hSourceBitmap     = image.GetHbitmap();
                IntPtr hPrevSourceObject = WinAPI.SelectObject(hSourceDC, hSourceBitmap);

                WinAPI.BitBlt(hDestDC, 0, 0, image.Width, image.Height, hSourceDC, 0, 0, WinAPI.SRCCOPY);

                WinAPI.DeleteObject(WinAPI.SelectObject(hSourceDC, hPrevSourceObject));
                WinAPI.SelectObject(hDestDC, hPrevDestObject); // 리턴값 : hDestBitmap
                graphics.ReleaseHdc(hSourceDC);
                WinAPI.DeleteDC(hDestDC);

                bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);
                if (!isClipboardOpen)
                {
                    WinAPI.DeleteObject(hDestBitmap);
                    WinAPI.DeleteObject(hSourceDC);
                    WinAPI.DeleteObject(hSourceBitmap);
                    tempImage.Dispose();
                    throw new CannotOpenException();
                }
                WinAPI.EmptyClipboard();
                WinAPI.SetClipboardData(WinAPI.CF_BITMAP, hDestBitmap);
                WinAPI.CloseClipboard();

                WinAPI.DeleteObject(hDestBitmap);
                WinAPI.DeleteObject(hSourceDC);
                WinAPI.DeleteObject(hSourceBitmap);
            }
            tempImage.Dispose();
        }
Beispiel #7
0
        /// <summary>
        /// 현재 클립보드에 저장되어 있는 데이터를 백업합니다. 클립보드 열기 요청 실패 시 ClipboardManager.CannotOpenException 예외가 발생합니다.
        /// </summary>
        public static void BackupData(IntPtr clipboardOwner)
        {
            Format = 0;

            bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);

            if (!isClipboardOpen)
            {
                throw new CannotOpenException();
            }
            do
            {
                Format = WinAPI.EnumClipboardFormats(Format);
            }while (Format >= 0x200 || Format == 0);

            IntPtr pointer = WinAPI.GetClipboardData(Format);

            switch (Format)
            {
            case WinAPI.CF_TEXT:
                Data         = Marshal.PtrToStringAnsi(pointer);
                MemoryHandle = Marshal.StringToHGlobalAnsi((string)Data);
                break;

            case WinAPI.CF_UNICODETEXT:
                Data         = Marshal.PtrToStringUni(pointer);
                MemoryHandle = Marshal.StringToHGlobalUni((string)Data);
                break;

            case WinAPI.CF_BITMAP:
                Data         = Image.FromHbitmap(pointer);
                MemoryHandle = ((Bitmap)Data).GetHbitmap();
                break;
            }
            WinAPI.CloseClipboard();

            HasDataToRestore = true;
        }
Beispiel #8
0
 /// <summary>
 /// Clears clipboard.
 /// </summary>
 public static void Clear()
 {
     WinAPI.OpenClipboard(IntPtr.Zero);
     WinAPI.EmptyClipboard();
     WinAPI.CloseClipboard();
 }
Beispiel #9
0
        public static clip clip_get()
        {
            clip c;
            var  open = WinAPI.OpenClipboard(IntPtr.Zero);

            if (!open)
            {
                WinAPI.CloseClipboard();
                Logging.Log("[clip] Error can't open clipboard.");
                return(null);
            }
            int size = 0, all_size = 0;

            c = new clip();
            IntPtr hglob = IntPtr.Zero;            //, all;
            IntPtr glock = IntPtr.Zero;
            uint   format, dib_skip = 0;

//			all = new IntPtr((uint)Marshal.SizeOf(typeof(uint)));
            for (format = 0; (format = WinAPI.EnumClipboardFormats(format)) != 0;)
            {
                switch (format)
                {
                case WinAPI.CF_BITMAP:
                case WinAPI.CF_ENHMETAFILE:
                case WinAPI.CF_DSPENHMETAFILE:
                    continue;                             // unsafe formats for GlobalSize
                }
                if (format == WinAPI.CF_TEXT || format == WinAPI.CF_OEMTEXT ||              // calculate only CF_UNICODETEXT instead
                    format == dib_skip)                        // also only one of dib/dibv5 formats should be calculated
                {
                    continue;
                }
                hglob = WinAPI.GetClipboardData(format);
                if (hglob == IntPtr.Zero)
                {
                    Logging.Log("[clip] hglob Fail: " + format); continue;
                }                                                                                                   // GetClipboardData() failed: skip this format.
//				if (format == WinAPI.CF_HDROP) {
//					System.Diagnostics.Debug.WriteLine("HGLOGADDR:" +hglob.ToInt32());
//					glock = WinAPI.GlobalLock(hglob);
//					if (glock != IntPtr.Zero) {
////						System.Diagnostics.Debug.WriteLine("HGLOGADDR:" +glock.ToInt32() + " " +Marshal.GetLastWin32Error());
////						int fc = WinAPI.DragQueryFile(glock, 0xFFFFFFFF, null, 0);
////						System.Diagnostics.Debug.WriteLine("[clip] FC:" + fc);
////						if (fc != 0) {
////							size = ((fc - 1) * 2);  // Init; -1 if don't want a newline after last file.
////							for (uint i = 0; i < fc; ++i) {
////								var tsize = WinAPI.DragQueryFile(glock, i, null, 0);
////								Logging.Log("[clip] File:" + i+ ", size:" +tsize);
////								size+=tsize;
////							}
////						}
////						else
////							size = 0;
//						size = WinAPI.GlobalSize(glock).ToInt32();
//						if (size != 0) {
//							byte[] bin = new byte[size];
//							System.Diagnostics.Debug.WriteLine("[clip] CF_HDROP Marshal copy: size:" + size +", bin-len: " + bin.Length + " glock:" + glock);
//							Marshal.Copy(glock, bin, 0, Convert.ToInt32(size));
//							c[format] = bin;
//						}
////						fc = WinAPI.DragQueryFile(glock, 0xFFFFFFFF, new System.Text.StringBuilder(""), 0);
////						System.Diagnostics.Debug.WriteLine("[clip] FC:" + fc);
////						if (fc != 0) {
////							size = ((fc - 1) * 2);  // Init; -1 if don't want a newline after last file.
////							var fnb = new System.Text.StringBuilder(999);
////							for (uint i = 0; i < fc; ++i) {
////								var tsize = WinAPI.DragQueryFile(glock, i, fnb, 999);
////							System.Diagnostics.Debug.WriteLine("[clip] File:" + i+ ", size:" +tsize);
////								size+=tsize;
////							}
////							System.Diagnostics.Debug.WriteLine("[clip] Fnb:" +fnb);
////						}
////					}
//					WinAPI.GlobalUnlock(glock);
//					continue;
//				}
                glock = WinAPI.GlobalLock(hglob);
                if (glock != IntPtr.Zero)
                {
                    size      = WinAPI.GlobalSize(glock).ToInt32();
                    all_size += size;
                    byte[] bin = new byte[size];
                    Logging.Log("[clip] Marshal copy: size:" + size + ", bin-len: " + bin.Length + " glock:" + glock);
                    Marshal.Copy(glock, bin, 0, (int)size);
                    c[format] = bin;
                }
                else
                {
                    Logging.Log("[clip] glock Fail: " + format);
                }
                WinAPI.GlobalUnlock(glock);
                Logging.Log("[clip] hglob:" + hglob + " x fmt: " + format);
                if (dib_skip == 0)
                {
                    if (format == WinAPI.CF_DIB)
                    {
                        dib_skip = WinAPI.CF_DIBV5;
                    }
                    else if (format == WinAPI.CF_DIBV5)
                    {
                        dib_skip = WinAPI.CF_DIB;
                    }
                }
            }
            Logging.Log("[clip] formats_count:," + c.Count);
            WinAPI.CloseClipboard();
            return(c);
        }