Example #1
0
        internal Size GetPartSize(IDeviceContext dc, ThemeSizeType type, IntPtr hWnd)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            // valid values are 0x0 to 0x2
            if (!ClientUtils.IsEnumValid(type, (int)type, (int)ThemeSizeType.Minimum, (int)ThemeSizeType.Draw))
            {
                throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(ThemeSizeType));
            }

            NativeMethods.SIZE size = new NativeMethods.SIZE();

            using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties))
            {
                HandleRef hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc);
                if (DpiHelper.IsPerMonitorV2Awareness && (IntPtr.Zero != hWnd))
                {
                    using (ThemeHandle hTheme = ThemeHandle.Create(_class, true, new HandleRef(null, hWnd)))
                    {
                        lastHResult = SafeNativeMethods.GetThemePartSize(new HandleRef(this, hTheme.NativeHandle), hdc, part, state, null, type, size);
                    }
                }
                else
                {
                    lastHResult = SafeNativeMethods.GetThemePartSize(new HandleRef(this, Handle), hdc, part, state, null, type, size);
                }
            }

            return(new Size(size.cx, size.cy));
        }
Example #2
0
        /// <summary>
        ///  Retrieves a IntPtr theme handle for the given class from the themeHandle cache. If its not
        ///  present in the cache, it creates a new ThemeHandle object and stores it there.
        /// </summary>
        private static IntPtr GetHandle(string className, bool throwExceptionOnFail)
        {
            ThemeHandle tHandle;

            if (themeHandles == null)
            {
                CreateThemeHandleHashtable();
            }

            if (threadCacheVersion != globalCacheVersion)
            {
                RefreshCache();
                threadCacheVersion = globalCacheVersion;
            }

            if (!themeHandles.Contains(className))
            { // see if it is already in cache
                tHandle = ThemeHandle.Create(className, throwExceptionOnFail);
                if (tHandle == null)
                {
                    return(IntPtr.Zero);
                }
                themeHandles.Add(className, tHandle);
            }
            else
            {
                tHandle = (ThemeHandle)themeHandles[className];
            }

            return(tHandle.NativeHandle);
        }
Example #3
0
        internal void DrawBackground(IDeviceContext dc, Rectangle bounds, Rectangle clipRectangle, IntPtr hWnd)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }
            if (bounds.Width < 0 || bounds.Height < 0)
            {
                return;
            }
            if (clipRectangle.Width < 0 || clipRectangle.Height < 0)
            {
                return;
            }

            using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties))
            {
                HandleRef hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc);
                if (IntPtr.Zero != hWnd)
                {
                    using (ThemeHandle hTheme = ThemeHandle.Create(_class, true, new HandleRef(null, hWnd)))
                    {
                        lastHResult = SafeNativeMethods.DrawThemeBackground(new HandleRef(this, hTheme.NativeHandle), hdc, part, state, new NativeMethods.COMRECT(bounds), new NativeMethods.COMRECT(clipRectangle));
                    }
                }
                else
                {
                    lastHResult = SafeNativeMethods.DrawThemeBackground(new HandleRef(this, Handle), hdc, part, state, new NativeMethods.COMRECT(bounds), new NativeMethods.COMRECT(clipRectangle));
                }
            }
        }
Example #4
0
        /// <summary>
        ///  Retrieves a IntPtr theme handle for the given class from the themeHandle cache. If its not
        ///  present in the cache, it creates a new ThemeHandle object and stores it there.
        /// </summary>
        private static IntPtr GetHandle(string className, bool throwExceptionOnFail)
        {
            if (t_themeHandles is null)
            {
                CreateThemeHandleHashtable();
            }

            if (t_threadCacheVersion != s_globalCacheVersion)
            {
                RefreshCache();
                t_threadCacheVersion = s_globalCacheVersion;
            }

            if (!t_themeHandles !.Contains(className))
            {
                // See if it is already in cache
                ThemeHandle?tHandle = ThemeHandle.Create(className, throwExceptionOnFail);
                if (tHandle is null)
                {
                    return(IntPtr.Zero);
                }

                t_themeHandles.Add(className, tHandle);
                return(tHandle.Handle);
            }

            return(((ThemeHandle)t_themeHandles[className] !).Handle);
        }
Example #5
0
        /// <summary>
        ///  Refreshes this thread's theme handle cache.
        /// </summary>
        private static void RefreshCache()
        {
            ThemeHandle tHandle = null;

            if (themeHandles != null)
            {
                string[] classNames = new string[themeHandles.Keys.Count];
                themeHandles.Keys.CopyTo(classNames, 0);

                foreach (string className in classNames)
                {
                    tHandle = (ThemeHandle)themeHandles[className];
                    if (tHandle != null)
                    {
                        tHandle.Dispose();
                    }

                    // We don't call IsSupported here, since that could cause RefreshCache to be called again,
                    // leading to stack overflow.
                    if (AreClientAreaVisualStylesSupported)
                    {
                        tHandle = ThemeHandle.Create(className, false);
                        if (tHandle != null)
                        {
                            themeHandles[className] = tHandle;
                        }
                    }
                }
            }
        }
Example #6
0
        internal unsafe Size GetPartSize(IDeviceContext dc, ThemeSizeType type, IntPtr hWnd)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            // valid values are 0x0 to 0x2
            if (!ClientUtils.IsEnumValid(type, (int)type, (int)ThemeSizeType.Minimum, (int)ThemeSizeType.Draw))
            {
                throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(ThemeSizeType));
            }

            using var wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties);
            var hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc);

            if (DpiHelper.IsPerMonitorV2Awareness && hWnd != IntPtr.Zero)
            {
                using (ThemeHandle hTheme = ThemeHandle.Create(_class, true, hWnd) !)
                {
                    lastHResult = GetThemePartSize(new HandleRef(this, hTheme.Handle), hdc, part, state, null, type, out Size dpiSize);
                    return(dpiSize);
                }
            }

            lastHResult = GetThemePartSize(this, hdc, part, state, null, type, out Size size);
            return(size);
        }
        internal unsafe void DrawBackground(IDeviceContext dc, Rectangle bounds, Rectangle clipRectangle, IntPtr hWnd)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }
            if (bounds.Width < 0 || bounds.Height < 0)
            {
                return;
            }
            if (clipRectangle.Width < 0 || clipRectangle.Height < 0)
            {
                return;
            }

            using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties))
            {
                HandleRef hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc);
                if (IntPtr.Zero != hWnd)
                {
                    using (ThemeHandle hTheme = ThemeHandle.Create(_class, true, hWnd))
                    {
                        RECT rect     = bounds;
                        RECT clipRect = clipRectangle;
                        lastHResult = UxTheme.DrawThemeBackground(hTheme, hdc, part, state, ref rect, &clipRect);
                    }
                }
                else
                {
                    RECT rect     = bounds;
                    RECT clipRect = clipRectangle;
                    lastHResult = UxTheme.DrawThemeBackground(this, hdc, part, state, ref rect, &clipRect);
                }
            }
        }
Example #8
0
        private static void RefreshCache()
        {
            ThemeHandle handle = null;

            if (themeHandles != null)
            {
                string[] array = new string[themeHandles.Keys.Count];
                themeHandles.Keys.CopyTo(array, 0);
                bool flag = VisualStyleInformation.IsEnabledByUser && ((Application.VisualStyleState == VisualStyleState.ClientAreaEnabled) || (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled));
                foreach (string str in array)
                {
                    handle = (ThemeHandle)themeHandles[str];
                    if (handle != null)
                    {
                        handle.Dispose();
                    }
                    if (flag)
                    {
                        handle = ThemeHandle.Create(str, false);
                        if (handle != null)
                        {
                            themeHandles[str] = handle;
                        }
                    }
                }
            }
        }
Example #9
0
        internal static bool IsCombinationDefined(string className, int part)
        {
            bool returnVal = false;

            if (!IsSupported)
            {
                if (!VisualStyleInformation.IsEnabledByUser)
                {
                    throw new InvalidOperationException(SR.VisualStyleNotActive);
                }
                else
                {
                    throw new InvalidOperationException(SR.VisualStylesDisabledInClientArea);
                }
            }

            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            IntPtr hTheme = GetHandle(className, false);

            if (hTheme != IntPtr.Zero)
            {
                // IsThemePartDefined doesn't work for part = 0, although there are valid parts numbered 0. We
                // allow these explicitly here.
                if (part == 0)
                {
                    returnVal = true;
                }
                else
                {
                    returnVal = SafeNativeMethods.IsThemePartDefined(new HandleRef(null, hTheme), part, 0);
                }
            }

            //if the combo isn't defined, check the validity of our theme handle cache
            if (!returnVal)
            {
                using (ThemeHandle tHandle = ThemeHandle.Create(className, false))
                {
                    if (tHandle != null)
                    {
                        returnVal = SafeNativeMethods.IsThemePartDefined(new HandleRef(null, tHandle.NativeHandle), part, 0);
                    }

                    //if we did, in fact get a new correct theme handle, our cache is out of date -- update it now.
                    if (returnVal)
                    {
                        RefreshCache();
                    }
                }
            }

            return(returnVal);
        }
        internal static bool IsCombinationDefined(string className, int part)
        {
            bool result = false;

            if (!IsSupported)
            {
                throw new InvalidOperationException(VisualStyleInformation.IsEnabledByUser
                    ? SR.VisualStylesDisabledInClientArea
                    : SR.VisualStyleNotActive);
            }

            IntPtr hTheme = GetHandle(className, false);

            if (hTheme != IntPtr.Zero)
            {
                // IsThemePartDefined doesn't work for part = 0, although there are valid parts numbered 0. We
                // allow these explicitly here.
                if (part == 0)
                {
                    result = true;
                }
                else
                {
                    result = IsThemePartDefined(hTheme, part, 0).IsTrue();
                }
            }

            // If the combo isn't defined, check the validity of our theme handle cache.
            if (!result)
            {
                using ThemeHandle? handle = ThemeHandle.Create(className, false);

                if (handle is not null)
                {
                    result = IsThemePartDefined(handle, part, 0).IsTrue();
                }

                // If we did, in fact get a new correct theme handle, our cache is out of date -- update it now.
                if (result)
                {
                    RefreshCache();
                }
            }

            return(result);
        }
Example #11
0
        private static bool IsCombinationDefined(string className, int part)
        {
            bool flag = false;

            if (!IsSupported)
            {
                if (!VisualStyleInformation.IsEnabledByUser)
                {
                    throw new InvalidOperationException(System.Windows.Forms.SR.GetString("VisualStyleNotActive"));
                }
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("VisualStylesDisabledInClientArea"));
            }
            if (className == null)
            {
                throw new ArgumentNullException("className");
            }
            IntPtr ptr = GetHandle(className, false);

            if (ptr != IntPtr.Zero)
            {
                if (part == 0)
                {
                    flag = true;
                }
                else
                {
                    flag = System.Windows.Forms.SafeNativeMethods.IsThemePartDefined(new HandleRef(null, ptr), part, 0);
                }
            }
            if (!flag)
            {
                using (ThemeHandle handle = ThemeHandle.Create(className, false))
                {
                    if (handle != null)
                    {
                        flag = System.Windows.Forms.SafeNativeMethods.IsThemePartDefined(new HandleRef(null, handle.NativeHandle), part, 0);
                    }
                    if (flag)
                    {
                        RefreshCache();
                    }
                }
            }
            return(flag);
        }
        internal unsafe Size GetPartSize(Gdi32.HDC dc, ThemeSizeType type, IntPtr hWnd = default)
        {
            // Valid values are 0x0 to 0x2
            if (!ClientUtils.IsEnumValid(type, (int)type, (int)ThemeSizeType.Minimum, (int)ThemeSizeType.Draw))
            {
                throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(ThemeSizeType));
            }

            if (DpiHelper.IsPerMonitorV2Awareness && hWnd != IntPtr.Zero)
            {
                using ThemeHandle hTheme = ThemeHandle.Create(Class, true, hWnd) !;
                _lastHResult             = GetThemePartSize(hTheme, dc, Part, State, null, type, out Size dpiSize);
                return(dpiSize);
            }

            _lastHResult = GetThemePartSize(this, dc, Part, State, null, type, out Size size);
            return(size);
        }
        internal unsafe void DrawBackground(Gdi32.HDC dc, Rectangle bounds, IntPtr hWnd)
        {
            if (bounds.Width < 0 || bounds.Height < 0)
            {
                return;
            }

            if (IntPtr.Zero != hWnd)
            {
                using ThemeHandle hTheme = ThemeHandle.Create(Class, true, hWnd) !;
                RECT rect = bounds;
                _lastHResult = DrawThemeBackground(hTheme, dc, Part, State, ref rect, null);
            }
            else
            {
                RECT rect = bounds;
                _lastHResult = DrawThemeBackground(this, dc, Part, State, ref rect, null);
            }
        }