Пример #1
0
        /// <summary>
        /// Sets the drag image.
        /// </summary>
        /// <param name="dataObject">The DataObject to set the drag image on.</param>
        /// <param name="image">The drag image.</param>
        /// <param name="cursorOffset">The location of the cursor relative to the image.</param>
        public static void SetDragImage(this System.Windows.Forms.IDataObject dataObject, Image image, Point cursorOffset)
        {
            var shdi = new ShDragImage();

            Win32Size size;

            size.cx            = image.Width;
            size.cy            = image.Height;
            shdi.sizeDragImage = size;

            Win32Point wpt;

            wpt.x         = cursorOffset.X;
            wpt.y         = cursorOffset.Y;
            shdi.ptOffset = wpt;

            shdi.crColorKey = Color.Magenta.ToArgb();

            // This HBITMAP will be managed by the DragDropHelper
            // as soon as we pass it to InitializeFromBitmap. If we fail
            // to make the hand off, we'll delete it to prevent a mem leak.
            IntPtr hbmp = GetHbitmapFromImage(image);

            shdi.hbmpDragImage = hbmp;

            try {
                var sourceHelper = (IDragSourceHelper) new DragDropHelper();

                try {
                    sourceHelper.InitializeFromBitmap(ref shdi, (ComIDataObject)dataObject);
                }
                catch (NotImplementedException ex) {
                    throw new Exception(
                              "A NotImplementedException was caught. This could be because you forgot to construct your DataObject using a DragDropLib.DataObject",
                              ex);
                }
            }
            catch {
                DeleteObject(hbmp);
            }
        }
		/// <summary>
		/// Sets the drag image.
		/// </summary>
		/// <param name="dataObject">The DataObject to set the drag image on.</param>
		/// <param name="image">The drag image.</param>
		/// <param name="cursorOffset">The location of the cursor relative to the image.</param>
		public static void SetDragImage(this System.Windows.Forms.IDataObject dataObject, Image image, Point cursorOffset) {
			var shdi = new ShDragImage();

			Win32Size size;
			size.cx = image.Width;
			size.cy = image.Height;
			shdi.sizeDragImage = size;

			Win32Point wpt;
			wpt.x = cursorOffset.X;
			wpt.y = cursorOffset.Y;
			shdi.ptOffset = wpt;

			shdi.crColorKey = Color.Magenta.ToArgb();

			// This HBITMAP will be managed by the DragDropHelper
			// as soon as we pass it to InitializeFromBitmap. If we fail
			// to make the hand off, we'll delete it to prevent a mem leak.
			IntPtr hbmp = GetHbitmapFromImage(image);
			shdi.hbmpDragImage = hbmp;

			try {
				var sourceHelper = (IDragSourceHelper) new DragDropHelper();

				try {
					sourceHelper.InitializeFromBitmap(ref shdi, (ComIDataObject) dataObject);
				}
				catch (NotImplementedException ex) {
					throw new Exception(
						"A NotImplementedException was caught. This could be because you forgot to construct your DataObject using a DragDropLib.DataObject",
						ex);
				}
			}
			catch {
				DeleteObject(hbmp);
			}
		}