Пример #1
0
        public static IconHandleInfo GetIconInfo(IconHandle iconHandle)
        {
            if(iconHandle == null)
                return null;

            ICONINFO iconInfo;
            if(!PInvoke.GetIconInfo(iconHandle.Handle, out iconInfo))
                return null;
            return new IconHandleInfo(iconInfo);
        }
Пример #2
0
        private void SetOverlayIconCore(IconHandle iconHandle, string accessibilityText)
        {
            if (!this.currentOverlayIcon.IsInvalid && this.currentOverlayIcon != iconHandle)
            {
                this.currentOverlayIcon.Dispose();
            }

            this.currentOverlayIcon = iconHandle;
            this.currentOverlayIconAccessibilityText = accessibilityText;

            this.TaskbarList.SetOverlayIcon(
                this.MainWindowHandle,
                this.currentOverlayIcon,
                this.currentOverlayIconAccessibilityText);
        }
Пример #3
0
        public static IconHandleInfo GetIconInfo(IconHandle iconHandle)
        {
            if (iconHandle == null)
            {
                return(null);
            }

            ICONINFO iconInfo;

            if (!PInvoke.GetIconInfo(iconHandle.Handle, out iconInfo))
            {
                return(null);
            }
            return(new IconHandleInfo(iconInfo));
        }
Пример #4
0
        internal static IconHandle CreateIconCursor(byte[] colorArray, int width, int height, int xHotspot, int yHotspot, bool isIcon)
        {
            BitmapHandle colorBitmap = null;
            BitmapHandle maskBitmap  = null;

            try
            {
                var bi = new InteropValues.BITMAPINFO(width, -height, 32)
                {
                    biCompression = InteropValues.BI_RGB
                };

                var bits = IntPtr.Zero;
                colorBitmap = InteropMethods.CreateDIBSection(new HandleRef(null, IntPtr.Zero), ref bi, InteropValues.DIB_RGB_COLORS, ref bits, null, 0);

                if (colorBitmap.IsInvalid || bits == IntPtr.Zero)
                {
                    return(IconHandle.GetInvalidIcon());
                }

                Marshal.Copy(colorArray, 0, bits, colorArray.Length);
                var maskArray = GenerateMaskArray(width, height, colorArray);

                maskBitmap = InteropMethods.CreateBitmap(width, height, 1, 1, maskArray);
                if (maskBitmap.IsInvalid)
                {
                    return(IconHandle.GetInvalidIcon());
                }

                var iconInfo = new InteropValues.ICONINFO
                {
                    fIcon    = isIcon,
                    xHotspot = xHotspot,
                    yHotspot = yHotspot,
                    hbmMask  = maskBitmap,
                    hbmColor = colorBitmap
                };

                return(InteropMethods.CreateIconIndirect(iconInfo));
            }
            finally
            {
                colorBitmap?.Dispose();
                maskBitmap?.Dispose();
            }
        }
Пример #5
0
        /// <summary>
        /// vsqxのxmlドキュメントに埋め込まれた音源情報のテーブルを取得する
        /// </summary>
        /// <param name="xml">xmlドキュメント</param>
        /// <returns>バンクセレクト、プログラムチェンジをキーとしたIconHandleのマップ</returns>
        private static Dictionary <int, Dictionary <int, IconHandle> > getVoiceTable(XmlDocument xml)
        {
            var result = new Dictionary <int, Dictionary <int, IconHandle> >();

            foreach (XmlNode vVoice in xml.DocumentElement.GetElementsByTagName("vVoice"))
            {
                int bankSelect    = int.Parse(vVoice["vBS"].InnerText);
                int programChange = int.Parse(vVoice["vPC"].InnerText);
                var name          = vVoice["vVoiceName"].InnerText;
                var iconHandle    = new IconHandle();
                iconHandle.IDS      = name;
                iconHandle.Language = bankSelect;
                iconHandle.Program  = programChange;
                iconHandle.IconID   =
                    "$0701" + bankSelect.ToString("X2") + programChange.ToString("X2");
                if (false == result.ContainsKey(bankSelect))
                {
                    result.Add(bankSelect, new Dictionary <int, IconHandle>());
                }
                result[bankSelect].Add(programChange, iconHandle);
            }
            return(result);
        }
Пример #6
0
 public static void GetIconHandlesFromImageSource(ImageSource image, out IconHandle largeIconHandle, out IconHandle smallIconHandle)
 {
     EnsureSystemMetrics();
     largeIconHandle = CreateIconHandleFromImageSource(image, IconSize);
     smallIconHandle = CreateIconHandleFromImageSource(image, SmallIconSize);
 }
Пример #7
0
        public void Update()
        {
            CURSORINFO cursorInfo;

            cursorInfo.cbSize = cursorInfoSize;

            if (!PInvoke.GetCursorInfo(out cursorInfo))
            {
                return;
            }

            if (cursorInfo.flags != PInvoke.CURSOR_SHOWING)// same cursor as previous
            {
                return;
            }

            using (IconHandle hicon = IconHandle.GetCursorIcon(cursorInfo))
                using (IconHandleInfo iconInfo = IconHandleInfo.GetIconInfo(hicon))
                {
                    if (iconInfo == null)
                    {
                        return;
                    }
                    var tmpx = _Position.X;
                    var tmpy = _Position.Y;
                    _Position.X = cursorInfo.ptScreenPos.x - iconInfo.Hotspot.X;
                    _Position.Y = cursorInfo.ptScreenPos.y - iconInfo.Hotspot.Y;
                    if (LastCursor == cursorInfo.hCursor)
                    {
                        if (MousePositionChangedEvent != null && tmpx != _Position.X && tmpy != _Position.Y)
                        {
                            MousePositionChangedEvent(_Position);
                        }
                        return;                      //no more work here, the cursor is the same
                    }
                    LastCursor = cursorInfo.hCursor; //just a copy
                    Bitmap resultBitmap = null;

                    using (Bitmap bitmapMask = Bitmap.FromHbitmap(iconInfo.MaskBitmap.Handle))
                    {
                        // Here we have to determine if the current cursor is monochrome in order
                        // to do a proper processing. If we just extracted the cursor icon from
                        // the icon handle, monochrome cursors would appear garbled.

                        if (bitmapMask.Height == bitmapMask.Width * 2)
                        {
                            // Yes, this is a monochrome cursor. We will have to manually copy
                            // the bitmap and the bitmak layers of the cursor into the bitmap.

                            resultBitmap = new Bitmap(bitmapMask.Width, bitmapMask.Width);
                            PInvoke.SelectObject(mask.Handle, iconInfo.MaskBitmap.Handle);

                            using (Graphics resultGraphics = Graphics.FromImage(resultBitmap))
                            {
                                IntPtr resultHandle = resultGraphics.GetHdc();

                                // These two operation will result in a black cursor over a white background. Later
                                //   in the code, a call to MakeTransparent() will get rid of the white background.
                                PInvoke.BitBlt(resultHandle, 0, 0, 32, 32, mask.Handle, 0, 32, CopyPixelOperation.SourceCopy);
                                PInvoke.BitBlt(resultHandle, 0, 0, 32, 32, mask.Handle, 0, 0, CopyPixelOperation.SourceInvert);

                                resultGraphics.ReleaseHdc(resultHandle);
                            }

                            // Remove the white background from the BitBlt calls,
                            // resulting in a black cursor over a transparent background.
                            resultBitmap.MakeTransparent(Color.White);
                        }
                        else
                        {
                            // This isn't a monochrome cursor.
                            using (Icon icon = Icon.FromHandle(hicon.Handle))
                                resultBitmap = icon.ToBitmap();
                        }
                    }
                    _Cursor = resultBitmap;
                    if (MouseImageChangedEvent != null)
                    {
                        using (var ms = new MemoryStream())
                        {
                            _Cursor.Save(ms, ImageFormat.Png);//png to preserve the transparency
                            MouseImageChangedEvent(_Position, ms.ToArray());
                        }
                    }
                }
        }
Пример #8
0
        /// <summary>
        ///   Gets the current cursor bitmap, supporting
        ///   transparency and handling monochrome cursors.
        /// </summary>
        ///
        /// <returns>A <see cref="Bitmap"/> containing the cursor's bitmap image.</returns>
        ///
        public Bitmap GetBitmap()
        {
            // Based on answer from Tarsier in SO question "C# - Capturing the Mouse cursor image"
            // http://stackoverflow.com/questions/918990/c-sharp-capturing-the-mouse-cursor-image

            CursorInfo cursorInfo;

            cursorInfo.cbSize = cursorInfoSize;

            if (!NativeMethods.GetCursorInfo(out cursorInfo))
            {
                return(null);
            }

            if (cursorInfo.flags != CursorState.CURSOR_SHOWING)
            {
                return(null);
            }

            using (IconHandle hicon = SafeNativeMethods.GetCursorIcon(cursorInfo))
                using (IconHandleInfo iconInfo = SafeNativeMethods.GetIconInfo(hicon))
                {
                    if (iconInfo == null)
                    {
                        return(null);
                    }

                    position.X = cursorInfo.ptScreenPos.X - iconInfo.Hotspot.X;
                    position.Y = cursorInfo.ptScreenPos.Y - iconInfo.Hotspot.Y;

                    if (!CaptureRegion.Contains(position))
                    {
                        return(null);
                    }

                    position.X -= CaptureRegion.X;
                    position.Y -= CaptureRegion.Y;

                    // Note: an alternative way would be to just return
                    //
                    //   Icon icon = Icon.FromHandle(hicon);
                    //
                    // However, this seems to fail for monochrome cursors such as
                    // the I-Beam cursor (text cursor). The following takes care
                    // of returning the correct bitmap.

                    Bitmap resultBitmap = null;

                    using (Bitmap bitmapMask = Bitmap.FromHbitmap(iconInfo.MaskBitmap.Handle))
                    {
                        // Here we have to determine if the current cursor is monochrome in order
                        // to do a proper processing. If we just extracted the cursor icon from
                        // the icon handle, monochrome cursors would appear garbled.

                        if (bitmapMask.Height == bitmapMask.Width * 2)
                        {
                            // Yes, this is a monochrome cursor. We will have to manually copy
                            // the bitmap and the bitmak layers of the cursor into the bitmap.

                            resultBitmap = new Bitmap(bitmapMask.Width, bitmapMask.Width);
                            NativeMethods.SelectObject(mask.Handle, iconInfo.MaskBitmap.Handle);

                            using (Graphics resultGraphics = Graphics.FromImage(resultBitmap))
                            {
                                IntPtr resultHandle = resultGraphics.GetHdc();

                                // These two operation will result in a black cursor over a white background. Later
                                //   in the code, a call to MakeTransparent() will get rid of the white background.
                                NativeMethods.BitBlt(resultHandle, 0, 0, 32, 32, mask.Handle, 0, 32, CopyPixelOperation.SourceCopy);
                                NativeMethods.BitBlt(resultHandle, 0, 0, 32, 32, mask.Handle, 0, 0, CopyPixelOperation.SourceInvert);

                                resultGraphics.ReleaseHdc(resultHandle);
                            }

                            // Remove the white background from the BitBlt calls,
                            // resulting in a black cursor over a transparent background.
                            resultBitmap.MakeTransparent(Color.White);
                        }
                        else
                        {
                            // This isn't a monochrome cursor.
                            using (Icon icon = Icon.FromHandle(hicon.Handle))
                                resultBitmap = icon.ToBitmap();
                        }
                    }

                    return(resultBitmap);
                }
        }
Пример #9
0
 internal static extern int ExtractIconEx(string szExeFileName, int nIconIndex, out IconHandle phiconLarge,
                                          out IconHandle phiconSmall, int nIcons);