示例#1
0
        /// <summary>
        /// Retrieves the data that is currently on the system clipboard.
        /// </summary>
        /// <returns>An <see cref="IDataObject"/> that represents the data currently on the clipboard, or null if there is no data on the clipboard.</returns>
        public static IDataObject GetDataObject()
        {
            //data object to return
            DataObject dobj = new DataObject();

            if (!OpenClipboard(IntPtr.Zero))
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
            }

            //get number of clipboard types available
            int cfcount = CountClipboardFormats();

            if (cfcount == 0)
            {
                //throw new System.Runtime.InteropServices.ExternalException("No data on the Clipboard");
                return(null);
            }
            else
            {
                ClipboardFormats format = 0;

                DataFormats.Format df;

                for (int i = 0; i < cfcount; i++)
                {
                    //get the next format on the clipboard
                    format = EnumClipboardFormats(format);

                    try
                    {
                        //get the dotnet data format
                        df = DataFormats.GetFormat((int)format);
                    }
                    catch
                    {
                        //if format is not recognised ignore it
                        break;
                    }

                    switch ((ClipboardFormats)df.Id)
                    {
                    case ClipboardFormats.UnicodeText:
                        //unicode text formats
                        dobj.SetData(df.Name, Marshal.PtrToStringUni(GetClipboardData((ClipboardFormats)df.Id)));
                        break;
                    }
                }
            }

            if (!CloseClipboard())
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard");
            }

            return(dobj);
        }
示例#2
0
 /// <summary>
 /// Returns an OpenNETCF.Windows.Forms.DataFormats.Format with the Windows Clipboard numeric ID and name for the specified format.
 /// </summary>
 /// <param name="format">The format name.</param>
 /// <returns>An OpenNETCF.Windows.Forms.DataFormats.Format that has the Windows Clipboard numeric ID and the name of the format.</returns>
 public static Format GetFormat(string format)
 {
     try
     {
         ClipboardFormats cf = 0;
         //cf.GetType().GetField(format).GetValue(cf)
         return(new Format(format, (int)(cf.GetType().GetField(format, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).GetValue(cf))));
     }
     catch
     {
         throw new Exception("Resolving Clipboard typue failed");
     }
 }
示例#3
0
 private static extern ClipboardFormats EnumClipboardFormats(ClipboardFormats uFormat);
示例#4
0
 private static extern IntPtr SetClipboardData(ClipboardFormats uFormat, IntPtr hMem);
示例#5
0
 private static extern IntPtr GetClipboardData(ClipboardFormats uFormat);
 private static int SetClipboardData(ClipboardFormats type, IntPtr h)
 {
     return(DllImportCaller.lib.IntDualCall("coredll", "SetClipboardData", (int)type, h.ToInt32()));
 }
 public static extern int SetClipboardData(ClipboardFormats uFormat, IntPtr hMem);
示例#8
0
        /// <summary>
        /// Retrieves the data that is currently on the system clipboard.
        /// </summary>
        /// <returns>An <see cref="IDataObject"/> that represents the data currently on the clipboard, or null if there is no data on the clipboard.</returns>
        public static IDataObject GetDataObject()
        {
            //data object to return
            DataObject dobj = new DataObject();

            if (!OpenClipboard(IntPtr.Zero))
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
            }

            //get number of clipboard types available
            int cfcount = CountClipboardFormats();

            if (cfcount == 0)
            {
                //throw new System.Runtime.InteropServices.ExternalException("No data on the Clipboard");
                return(null);
            }
            else
            {
                ClipboardFormats format = 0;

                DataFormats.Format df;

                for (int i = 0; i < cfcount; i++)
                {
                    //get the next format on the clipboard
                    format = EnumClipboardFormats(format);

                    try
                    {
                        //get the dotnet data format
                        df = DataFormats.GetFormat((int)format);
                    }
                    catch
                    {
                        //if format is not recognised ignore it
                        break;
                    }

                    switch ((ClipboardFormats)df.Id)
                    {
                    case ClipboardFormats.Bitmap:
                        //device independant bitmap
                        IntPtr           hDib = GetClipboardData((ClipboardFormats)df.Id);
                        BitmapInfoHeader bih  = new BitmapInfoHeader();

                        //marshal thw bitmap information header
                        for (int iByte = 0; iByte < bih.Data.Length; iByte++)
                        {
                            bih.Data[iByte] = Marshal.ReadByte(hDib, iByte);
                        }

                        byte [] buf = MarshalEx.ReadByteArray(hDib, 0, (Math.Abs(bih.Width * bih.Height) * (bih.BitCount >> 3)) + bih.Size);

                        MemoryStream strm = new MemoryStream(buf.Length + 14);
                        BinaryWriter wr   = new BinaryWriter(strm);
                        wr.Write((short)0x4d42);                                  // BM
                        wr.Write((int)(buf.Length + 14));
                        wr.Write(0);
                        wr.Write((int)(0x36 + (BitConverter.ToInt32(buf, 0x20) * 4)));
                        wr.Write(buf);
                        wr.Flush();
                        strm.Seek((long)0, SeekOrigin.Begin);

                        dobj.SetData(df.Name, new System.Drawing.Bitmap(strm));
                        break;

                    case ClipboardFormats.UnicodeText:
                        //unicode text formats
                        dobj.SetData(df.Name, Marshal.PtrToStringUni(GetClipboardData((ClipboardFormats)df.Id)));
                        break;
                    }
                }
            }

            if (!CloseClipboard())
            {
                throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard");
            }

            return(dobj);
        }
示例#9
0
文件: Win32DnD.cs 项目: nlhepler/mono
		private static bool AddFormatAndMedium(ClipboardFormats cfFormat, object data) {
			STGMEDIUM	medium;
			FORMATETC	format;
			IntPtr		hmem;
			IntPtr		hmem_ptr;
			byte[]		b;

			switch(cfFormat) {
				case ClipboardFormats.CF_TEXT: {
					b = XplatUIWin32.StringToAnsi ((string)data);
					hmem = XplatUIWin32.CopyToMoveableMemory (b);
					break;
				}

				case ClipboardFormats.CF_UNICODETEXT: {
					b = XplatUIWin32.StringToUnicode ((string)data);
					hmem = XplatUIWin32.CopyToMoveableMemory (b);
					break;
				}

				case ClipboardFormats.CF_HDROP: {
					IEnumerator	e;
					StringBuilder	sb;
					long		hmem_string_ptr;
					IntPtr		string_buffer;
					int		string_buffer_size;

					sb = new StringBuilder();

					// Make sure object is enumerable; otherwise
					if ((data is string) || !(data is IEnumerable)) {
						sb.Append(data.ToString());
						sb.Append('\0');
						sb.Append('\0');
					} else {
						e = ((IEnumerable)data).GetEnumerator();
						while (e.MoveNext()) {
							sb.Append(e.Current.ToString());
							sb.Append('\0');
						}
						sb.Append('\0');
					}

					string_buffer = Marshal.StringToHGlobalUni(sb.ToString());
					string_buffer_size = (int)XplatUIWin32.Win32GlobalSize(string_buffer);

					// Write DROPFILES structure
					hmem = XplatUIWin32.Win32GlobalAlloc(XplatUIWin32.GAllocFlags.GMEM_MOVEABLE | XplatUIWin32.GAllocFlags.GMEM_DDESHARE, 0x14 + string_buffer_size);
					hmem_ptr = XplatUIWin32.Win32GlobalLock(hmem);
					Marshal.WriteInt32(hmem_ptr, 0x14);					// pFiles
					Marshal.WriteInt32(hmem_ptr, 1 * Marshal.SizeOf(typeof(uint)), 0);	// point.x
					Marshal.WriteInt32(hmem_ptr, 2 * Marshal.SizeOf(typeof(uint)), 0);	// point.y
					Marshal.WriteInt32(hmem_ptr, 3 * Marshal.SizeOf(typeof(uint)), 0);	// fNc
					Marshal.WriteInt32(hmem_ptr, 4 * Marshal.SizeOf(typeof(uint)), 1);	// fWide

					hmem_string_ptr = (long)hmem_ptr;
					hmem_string_ptr += 0x14;

					XplatUIWin32.Win32CopyMemory(new IntPtr(hmem_string_ptr), string_buffer, string_buffer_size);
					Marshal.FreeHGlobal(string_buffer);
					XplatUIWin32.Win32GlobalUnlock(hmem_ptr);

					break;
				}

				case ClipboardFormats.CF_DIB: {
					b = XplatUIWin32.ImageToDIB((Image)data);
					hmem = XplatUIWin32.CopyToMoveableMemory (b);
					break;
				}

				default: {
					hmem = IntPtr.Zero;
					break;
				}
			}

			if (hmem != IntPtr.Zero) {
				medium = new STGMEDIUM();
				medium.tymed = TYMED.TYMED_HGLOBAL;
				medium.hHandle = hmem;
				medium.pUnkForRelease = IntPtr.Zero;
				DragMediums.Add(medium);

				format = new FORMATETC();
				format.ptd = IntPtr.Zero;
				format.dwAspect = DVASPECT.DVASPECT_CONTENT;
				format.lindex = -1;
				format.tymed = TYMED.TYMED_HGLOBAL;
				format.cfFormat = cfFormat;
				DragFormats.Add(format);

				return true;
			}

			return false;
		}
示例#10
0
 /// <summary>
 /// Adds a representation of the information to be transferred in the specified data format. Flash Player requires a user event (such as a key press or mouse click) before using setData() . In AIR, this restriction only applies to content outside of the application security sandbox. 
 /// </summary>
 /// <param name="pFormat">The information to add.</param>
 /// <param name="pData">The format of the data.</param>
 /// <param name="pSerializable">Specify <see langword="true"/> for objects that can be serialized (and deserialized).</param>
 /// <returns><see langword="true"/> if the data was successfully set; <see langword="false"/> otherwise. In Flash Player, returns <see langword="false"/> when format is an unsupported member of <see cref="ClipboardFormats"/>. (Flash Player does not support <see cref="ClipboardFormats"/>.URL_FORMAT , <see cref="ClipboardFormats"/>.FILE_LIST_FORMAT , or <see cref="ClipboardFormats"/>.BITMAP_FORMAT ).</returns>
 /// <exception cref=" IllegalOperationError">The Clipboard object requested is no longer in scope (AIR only).</exception>
 /// <exception cref="SecurityError">Reading from or writing to the clipboard is not permitted in this context. In Flash Player, you can only call this method successfully during the processing of a user event (as in a key press or mouse click). In AIR, this restriction only applies to content outside of the application security sandbox.</exception>
 /// <exception cref="TypeError">format or data is <see langword="null"/>.</exception>
 public bool setData(ClipboardFormats pFormat, object pData, bool pSerializable)
 {
     return false;
 }
示例#11
0
 /// <summary>
 /// Adds a reference to a handler function that produces the data for the specified format on demand. Use this method to defer creation or rendering of the data until it is actually accessed. Flash Player requires a user event (such as a key press or mouse click) before using setDataHandler() . In AIR, this restriction only applies to content outside of the application security sandbox.
 /// </summary>
 /// <param name="pFormat">A function that returns the data to be transferred when called.</param>
 /// <param name="pHandler"></param>
 /// <param name="pSerializable">Specify <see langword="true"/> if the object returned by handler can be serialized (and deserialized).</param>
 /// <returns><see langword="true"/> if the handler was successfully set; <see langword="false"/> otherwise.</returns>
 /// <exception cref="TypeError">format or handler is <see langword="null"/>.</exception>
 /// <exception cref="IllegalOperationError">The Clipboard object requested is no longer in scope (AIR only).</exception>
 /// <exception cref="SecurityError">Reading from or writing to the clipboard is not permitted in this context. In Flash Player, you can only call this method successfully during the processing of a user event (such as a key press or mouse click). In AIR, this restriction only applies to content outside of the application security sandbox.</exception>
 public bool setDataHandler(ClipboardFormats pFormat, Func<object> pHandler, bool pSerializable)
 {
     return false;
 }
示例#12
0
 /// <summary>
 /// Checks whether data in the specified format exists in this Clipboard object. 
 /// </summary>
 /// <param name="pFormat">The format type to check.</param>
 /// <returns><see langword="true"/> , if data in the specified format is present.</returns>
 /// <exception cref="IllegalOperationError">The Clipboard object requested is no longer in scope (AIR only).</exception>
 /// <exception cref="SecurityError">Reading from or writing to the clipboard is not permitted in this context.</exception>
 public bool hasFormat(ClipboardFormats pFormat)
 {
     return false;
 }
示例#13
0
 /// <summary>
 /// Gets the clipboard data if data in the specified format is present. Flash Player requires a user event (such as a key press or mouse click) before using getData() . Call getData() within a user initiated paste event handler. In AIR, this restriction only applies to content outside of the application security sandbox. 
 /// </summary>
 /// <param name="pFormat">The data format to return</param>
 /// <param name="pTransferMode">Specifies whether to return a reference or serialized copy when an application-defined data format is accessed.</param>
 /// <returns>An object of the type corresponding to the data format.</returns>
 /// <exception cref="Error">transferMode is not one of the names defined in the <see cref="ClipboardTransferMode"/> class.</exception>
 /// <exception cref="IllegalOperationError">The Clipboard object requested is no longer in scope (AIR only).</exception>
 /// <exception cref="SecurityError">Reading from or writing to the clipboard is not permitted in this context. In Flash Player, you can only call this method successfully during the processing of a user event (as in a key press or mouse click). In AIR, this restriction only applies to content outside of the application security sandbox.</exception>
 public object getData(ClipboardFormats pFormat, ClipboardTransferMode pTransferMode)
 {
     return null;
 }
示例#14
0
 /// <summary>
 /// Deletes the data representation for the specified format.
 /// </summary>
 /// <param name="pFormat">The data format to remove.</param>
 public void clearData(ClipboardFormats pFormat)
 {
     return;
 }
示例#15
0
 public static Boolean IsTextFormat(ClipboardFormats format)
 {
     return IsTextFormat((UInt16)format);
 }
 public static extern int SetClipboardData(ClipboardFormats uFormat, IntPtr hMem);
示例#17
0
 public static String ExtractText(ClipboardFormats format, Byte[] data)
 {
     return ExtractText((UInt16)format, data);
 }
示例#18
0
 private static extern IntPtr DdeClientTransactionString(string pData, int cbData, IntPtr hConv, IntPtr hszItem, ClipboardFormats wFmt, TransactionType wType, int dwTimeout, int pdwResult);
示例#19
0
        private static bool AddFormatAndMedium(ClipboardFormats cfFormat, object data)
        {
            STGMEDIUM medium;
            FORMATETC format;
            IntPtr    hmem;
            IntPtr    hmem_ptr;

            byte[] b;

            switch (cfFormat)
            {
            case ClipboardFormats.CF_TEXT:
            {
                b    = XplatUIWin32.StringToAnsi((string)data);
                hmem = XplatUIWin32.CopyToMoveableMemory(b);
                break;
            }

            case ClipboardFormats.CF_UNICODETEXT:
            {
                b    = XplatUIWin32.StringToUnicode((string)data);
                hmem = XplatUIWin32.CopyToMoveableMemory(b);
                break;
            }

            case ClipboardFormats.CF_HDROP:
            {
                IEnumerator   e;
                StringBuilder sb;
                long          hmem_string_ptr;
                IntPtr        string_buffer;
                int           string_buffer_size;

                sb = new StringBuilder();

                // Make sure object is enumerable; otherwise
                if ((data is string) || !(data is IEnumerable))
                {
                    sb.Append(data.ToString());
                    sb.Append('\0');
                    sb.Append('\0');
                }
                else
                {
                    e = ((IEnumerable)data).GetEnumerator();
                    while (e.MoveNext())
                    {
                        sb.Append(e.Current.ToString());
                        sb.Append('\0');
                    }
                    sb.Append('\0');
                }

                string_buffer      = Marshal.StringToHGlobalUni(sb.ToString());
                string_buffer_size = (int)XplatUIWin32.Win32GlobalSize(string_buffer);

                // Write DROPFILES structure
                hmem     = XplatUIWin32.Win32GlobalAlloc(XplatUIWin32.GAllocFlags.GMEM_MOVEABLE | XplatUIWin32.GAllocFlags.GMEM_DDESHARE, 0x14 + string_buffer_size);
                hmem_ptr = XplatUIWin32.Win32GlobalLock(hmem);
                Marshal.WriteInt32(hmem_ptr, 0x14);                                // pFiles
                Marshal.WriteInt32(hmem_ptr, 1 * Marshal.SizeOf(typeof(uint)), 0); // point.x
                Marshal.WriteInt32(hmem_ptr, 2 * Marshal.SizeOf(typeof(uint)), 0); // point.y
                Marshal.WriteInt32(hmem_ptr, 3 * Marshal.SizeOf(typeof(uint)), 0); // fNc
                Marshal.WriteInt32(hmem_ptr, 4 * Marshal.SizeOf(typeof(uint)), 1); // fWide

                hmem_string_ptr  = (long)hmem_ptr;
                hmem_string_ptr += 0x14;

                XplatUIWin32.Win32CopyMemory(new IntPtr(hmem_string_ptr), string_buffer, string_buffer_size);
                Marshal.FreeHGlobal(string_buffer);
                XplatUIWin32.Win32GlobalUnlock(hmem_ptr);

                break;
            }

            case ClipboardFormats.CF_DIB:
            {
                b    = XplatUIWin32.ImageToDIB((Image)data);
                hmem = XplatUIWin32.CopyToMoveableMemory(b);
                break;
            }

            default:
            {
                hmem = IntPtr.Zero;
                break;
            }
            }

            if (hmem != IntPtr.Zero)
            {
                medium                = new STGMEDIUM();
                medium.tymed          = TYMED.TYMED_HGLOBAL;
                medium.hHandle        = hmem;
                medium.pUnkForRelease = IntPtr.Zero;
                DragMediums.Add(medium);

                format          = new FORMATETC();
                format.ptd      = IntPtr.Zero;
                format.dwAspect = DVASPECT.DVASPECT_CONTENT;
                format.lindex   = -1;
                format.tymed    = TYMED.TYMED_HGLOBAL;
                format.cfFormat = cfFormat;
                DragFormats.Add(format);

                return(true);
            }

            return(false);
        }
示例#20
0
 private static int SetClipboardData(ClipboardFormats type, IntPtr h)
 {
     return DllImportCaller.lib.IntDualCall("coredll", "SetClipboardData", (int)type, h.ToInt32());
 }