Beispiel #1
0
        /// <summary>
        /// Gets an Icon based upon the progId of a particular application
        /// </summary>
        /// <param name="progId">The progId for which is locate the Icon</param>
        /// <returns>The appropriate default Icon</returns>
        public static Icon GetIconFromProgId(string progId)
        {
            if (progId == null || !m_icons.ContainsKey(progId))
            {
                IntPtr hInstance = Marshal.GetHINSTANCE(typeof(AppIconCache).Module);

                string IconPath = GetDefaultIconPath(progId);
                if (IconPath != null)
                {
                    string[] iconInfo = IconPath.Split(',');
                    IntPtr   hIcon    = Shell32.ExtractIcon(hInstance, iconInfo[0], Convert.ToInt32(iconInfo[1]));
                    Icon     icon     = Icon.FromHandle(hIcon);
                    if (progId == null)
                    {
                        return(icon);
                    }
                    m_icons[progId] = icon;
                }
                else
                {
                    if (progId == null)
                    {
                        return(null);
                    }
                    m_icons[progId] = null;
                }
            }
            return((Icon)m_icons[progId]);
        }
Beispiel #2
0
        public static Icon ShellExtractIcon(string path, int iconIndex = 0)
        {
            IntPtr iconHandle = IntPtr.Zero;

            try
            {
                return(Icon.FromHandle(Shell32.ExtractIcon(Process.GetCurrentProcess().Handle, path, iconIndex)));
            }
            finally
            {
                User32.DestroyIcon(iconHandle);
            }
        }
Beispiel #3
0
        public static ImageSource GetIconAsImageSourceFromFile(string path, int iconIndex = 0)
        {
            IntPtr iconHandle = IntPtr.Zero;

            try
            {
                iconHandle = Shell32.ExtractIcon(Process.GetCurrentProcess().Handle, path, iconIndex);
                return(Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
            }
            finally
            {
                User32.DestroyIcon(iconHandle);
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="RenderForm" /> class.
        /// </summary>
        /// <param name="windowTitle"> (Optional) The window title. </param>
        /// <exception cref="Win32Exception"> Thrown when a Window 32 error condition occurs. </exception>
        public RenderForm(string windowTitle = "RenderForm")
        {
            _windowTitle       = windowTitle;
            _rawKeyPipe        = new Pipe <RawKeyEventHandler>();
            _keyUpPipe         = new Pipe <KeyEventHandler>();
            _keyDownPipe       = new Pipe <KeyEventHandler>();
            _keyPressPipe      = new Pipe <KeyPressEventHandler>();
            _mouseMovePipe     = new Pipe <MouseEventHandler>();
            _mouseUpPipe       = new Pipe <MouseEventHandler>();
            _mouseDownPipe     = new Pipe <MouseEventHandler>();
            _mouseClickPipe    = new Pipe <MouseEventHandler>();
            _mouseWheelPipe    = new Pipe <MouseEventHandler>();
            _mouseRawInputPipe = new Pipe <MouseEventHandler>();

            _wndClassEx = new WndClassEx
            {
                cbSize = Marshal.SizeOf <WndClassEx>(),
                style  = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw |
                         ClassStyles.DoubleClicks | ClassStyles.OwnDC,
                hbrBackground = (IntPtr)COLOR_WINDOW + 1, //null,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = Kernel32.GetModuleHandle(null !),
                hIcon         = Shell32.ExtractIcon(IntPtr.Zero, Assembly.GetExecutingAssembly().Location, 0),
                hCursor       = User32.LoadCursor(IntPtr.Zero, IDC_ARROW),
                lpszMenuName  = null !,
                lpszClassName = LP_CLASS_NAME,
                lpfnWndProc   = WndProc,
                hIconSm       = IntPtr.Zero
            };

            ushort regResult = User32.RegisterClassEx(ref _wndClassEx);

            if (regResult == 0)
            {
                throw new Win32Exception(
                          Kernel32.GetLastError(), $"{nameof(User32.RegisterClassEx)} failed with code {regResult}!");
            }
        }