Exemplo n.º 1
0
        public static Cursor InternalCreateCursor(System.Drawing.Bitmap bmp,
                                                  int xHotSpot, int yHotSpot)
        {
            //IconInfo tmp = new IconInfo();
            //GetIconInfo(bmp.GetHicon(), ref tmp);
            //tmp.xHotspot = xHotSpot;
            //tmp.yHotspot = yHotSpot;
            //tmp.fIcon = false;

            //IntPtr ptr = CreateIconIndirect(ref tmp);
            //SafeFileHandle handle = new SafeFileHandle(ptr, true);
            //return CursorInteropHelper.Create(handle);



            var iconInfo = new IconInfo();

            GetIconInfo(bmp.GetHicon(), ref iconInfo);
            iconInfo.xHotspot = xHotSpot;
            iconInfo.yHotspot = yHotSpot;
            iconInfo.fIcon    = false;

            SafeIconHandle cursorHandle = CreateIconIndirect(ref iconInfo);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 2
0
        private static Cursor createCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            User32.IconInfo iconInfo = new User32.IconInfo();
            User32.GetIconInfo(bmp.GetHicon(), ref iconInfo);
            iconInfo.xHotspot = xHotSpot;
            iconInfo.yHotspot = yHotSpot;
            iconInfo.fIcon    = false;
            SafeIconHandle cursorHandle = User32.CreateIconIndirect(ref iconInfo);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Create a cursor from the supplied bitmap & hotspot coordinates
 /// </summary>
 /// <param name="bitmap">Bitmap to create an icon from</param>
 /// <param name="hotspotX">Hotspot X coordinate</param>
 /// <param name="hotspotY">Hotspot Y coordinate</param>
 /// <returns>Cursor</returns>
 private static Cursor CreateCursor(Bitmap bitmap, int hotspotX, int hotspotY)
 {
     using (var iconHandle = new SafeIconHandle(bitmap.GetHicon()))
     {
         User32Api.GetIconInfo(iconHandle, out var iconInfo);
         iconInfo.Hotspot = new NativePoint(hotspotX, hotspotY);
         iconInfo.IsIcon  = false;
         var icon = User32Api.CreateIconIndirect(ref iconInfo);
         return(new Cursor(icon));
     }
 }
        private static Cursor InternalCreateCursor(Bitmap bitmap, int xHotSpot, int yHotSpot)
        {
            NativeMethods.IconInfo icon = default(NativeMethods.IconInfo);
            NativeMethods.GetIconInfo(bitmap.GetHicon(), ref icon);
            icon.xHotspot = xHotSpot;
            icon.yHotspot = yHotSpot;
            icon.fIcon    = false;
            SafeIconHandle cursorHandle = NativeMethods.CreateIconIndirect(ref icon);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Create a cursor from the supplied bitmap & hotspot coordinates
 /// </summary>
 /// <param name="bitmap">Bitmap to create an icon from</param>
 /// <param name="hotspotX">Hotspot X coordinate</param>
 /// <param name="hotspotY">Hotspot Y coordinate</param>
 /// <returns>Cursor</returns>
 private static Cursor CreateCursor(Bitmap bitmap, int hotspotX, int hotspotY)
 {
     using (SafeIconHandle iconHandle = new SafeIconHandle(bitmap.GetHicon())) {
         IconInfo iconInfo;
         User32.GetIconInfo(iconHandle, out iconInfo);
         iconInfo.xHotspot = hotspotX;
         iconInfo.yHotspot = hotspotY;
         iconInfo.fIcon    = false;
         var icon = User32.CreateIconIndirect(ref iconInfo);
         return(new Cursor(icon));
     }
 }
Exemplo n.º 6
0
        private static KeyValuePair <SafeIconHandle, Icon> ImageSourceToIcon(ImageSource source)
        {
            var image = source as BitmapSource;

            if (image == null)
            {
                throw new InvalidOperationException();
            }
            IntPtr hIcon      = ImageHelper.BitmapFromBitmapSource(image).GetHicon();
            var    iconHandle = new SafeIconHandle(hIcon);

            return(new KeyValuePair <SafeIconHandle, Icon>(iconHandle, Icon.FromHandle(hIcon)));
        }
Exemplo n.º 7
0
        private static Cursor InternalCreateCursor(System.Drawing.Bitmap bmp)
        {
            NativeMethods.IconInfo iconInfo = new();
            NativeMethods.GetIconInfo(bmp.GetHicon(), ref iconInfo);

            iconInfo.xHotspot = 0;
            iconInfo.yHotspot = 0;
            iconInfo.fIcon    = false;

            SafeIconHandle cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 8
0
        //保护:

        /// <summary>
        /// 创建鼠标(本方法不允许public,避免内存泄漏)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="xHotSpot"></param>
        /// <param name="yHotSpot"></param>
        /// <returns></returns>
        protected static Cursor InternalCreateCursor(Bitmap bitmap, uint xHotSpot, uint yHotSpot)
        {
            var iconInfo = new NativeMethods.IconInfo();

            NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo);

            iconInfo.xHotspot = xHotSpot; //焦点x轴坐标
            iconInfo.yHotspot = yHotSpot; //焦点y轴坐标
            iconInfo.fIcon    = false;    //设置鼠标

            SafeIconHandle cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 透過Bitmap建立圖形游標
        /// </summary>
        /// <param name="bitmap">要當成游標的Bitmap</param>
        /// <param name="xHotSpot">游標頂點的X軸位移</param>
        /// <param name="yHotSpot">游標頂點的Y軸位移</param>
        /// <returns>自訂的游標物件</returns>
        private static Cursor InternalCreateCursor(System.Drawing.Bitmap bitmap, int xHotSpot, int yHotSpot)
        {
            var iconInfo = new NativeMethods.IconInfo();

            NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo);

            iconInfo.xHotspot = xHotSpot;
            iconInfo.yHotspot = yHotSpot;
            iconInfo.fIcon    = false;

            SafeIconHandle cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);

            return(CursorInteropHelper.Create(cursorHandle));
        }
Exemplo n.º 10
0
        /// <summary>
        /// This method will capture the current Cursor by using User32 Code
        /// </summary>
        /// <returns>A Capture Object with the Mouse Cursor information in it.</returns>
        public static ICapture CaptureCursor(ICapture capture)
        {
            LOG.Debug("Capturing the mouse cursor.");
            if (capture == null)
            {
                capture = new Capture();
            }
            int        x, y;
            CursorInfo cursorInfo = new CursorInfo();
            IconInfo   iconInfo;

            cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
            if (User32.GetCursorInfo(out cursorInfo))
            {
                if (cursorInfo.flags == User32.CURSOR_SHOWING)
                {
                    using (SafeIconHandle safeIcon = User32.CopyIcon(cursorInfo.hCursor))
                    {
                        if (User32.GetIconInfo(safeIcon, out iconInfo))
                        {
                            Point cursorLocation = GetCursorLocation();
                            // Allign cursor location to Bitmap coordinates (instead of Screen coordinates)
                            x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X;
                            y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y;
                            // Set the location
                            capture.CursorLocation = new Point(x, y);

                            using (Icon icon = Icon.FromHandle(safeIcon.DangerousGetHandle()))
                            {
                                capture.Cursor = icon;
                            }

                            if (iconInfo.hbmMask != IntPtr.Zero)
                            {
                                DeleteObject(iconInfo.hbmMask);
                            }
                            if (iconInfo.hbmColor != IntPtr.Zero)
                            {
                                DeleteObject(iconInfo.hbmColor);
                            }
                        }
                    }
                }
            }
            return(capture);
        }
Exemplo n.º 11
0
 internal static extern int ExtractIconEx(
     string szExeFileName, int nIconIndex,
     out SafeIconHandle phiconLarge, out SafeIconHandle phiconSmall, int nIcons);
Exemplo n.º 12
0
 public static extern bool GetIconInfo(SafeIconHandle iconHandle, out IconInfo iconInfo);