Ejemplo n.º 1
0
        public Rectangle DrawEdge(IDeviceContext dc, Rectangle bounds, Edges edges, EdgeStyle style, EdgeEffects effects)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            if (!ClientUtils.IsEnumValid_Masked(edges, (int)edges, (uint)(Edges.Left | Edges.Top | Edges.Right | Edges.Bottom | Edges.Diagonal)))
            {
                throw new InvalidEnumArgumentException(nameof(edges), (int)edges, typeof(Edges));
            }

            if (!ClientUtils.IsEnumValid_NotSequential(style, (int)style, (int)EdgeStyle.Raised, (int)EdgeStyle.Sunken, (int)EdgeStyle.Etched, (int)EdgeStyle.Bump))
            {
                throw new InvalidEnumArgumentException(nameof(style), (int)style, typeof(EdgeStyle));
            }

            if (!ClientUtils.IsEnumValid_Masked(effects, (int)effects, (uint)(EdgeEffects.FillInterior | EdgeEffects.Flat | EdgeEffects.Soft | EdgeEffects.Mono)))
            {
                throw new InvalidEnumArgumentException(nameof(effects), (int)effects, typeof(EdgeEffects));
            }

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

            lastHResult = UxTheme.DrawThemeEdge(this, hdc, part, state, ref destRect, (User32.EDGE)style, (User32.BF)edges | (User32.BF)effects | User32.BF.ADJUST, ref contentRect);
            return(contentRect);
        }
Ejemplo n.º 2
0
        public static bool SetAppMode(PreferredAppMode appMode)
        {
            var build = Environment.OSVersion.Version.Build;

            if (build >= 18362)
            {
                // memo: Workaround for the problem of not switching from dark theme to light theme.
                if (appMode == PreferredAppMode.APPMODE_DEFAULT || appMode == PreferredAppMode.APPMODE_FORCELIGHT)
                {
                    UxTheme.SetPreferredAppMode(PreferredAppMode.APPMODE_ALLOWDARK);
                    UxTheme.RefreshImmersiveColorPolicyState();
                }

                var ret = UxTheme.SetPreferredAppMode(appMode) == appMode;
                UxTheme.RefreshImmersiveColorPolicyState();
                return(ret);
            }
            else if (build >= 17763)
            {
                if (appMode == PreferredAppMode.APPMODE_DEFAULT || appMode == PreferredAppMode.APPMODE_FORCELIGHT)
                {
                    return(UxTheme.AllowDarkModeForApp(false));
                }
                else
                {
                    return(UxTheme.AllowDarkModeForApp(true));
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        public Color this[string colorName] {
            get {
                IntPtr name = IntPtr.Zero;
                UInt32 colorType;

                try
                {
                    name      = Marshal.StringToHGlobalUni("Immersive" + colorName);
                    colorType = UxTheme.GetImmersiveColorTypeFromName(name);
                    if (colorType == 0xFFFFFFFF)
                    {
                        throw new InvalidOperationException();
                    }
                }
                finally
                {
                    if (name != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(name);
                        name = IntPtr.Zero;
                    }
                }

                return(this[colorType]);
            }
        }
Ejemplo n.º 4
0
        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);
                }
            }
        }
Ejemplo n.º 5
0
            internal static ThemeHandle Create(string className, bool throwExceptionOnFail, IntPtr hWndRef)
            {
                // HThemes require an HWND when display scaling is different between monitors.
                IntPtr hTheme = IntPtr.Zero;

                try
                {
                    hTheme = UxTheme.OpenThemeData(hWndRef, className);
                }
                catch (Exception e) when(!ClientUtils.IsSecurityOrCriticalException(e))
                {
                    if (throwExceptionOnFail)
                    {
                        throw new InvalidOperationException(SR.VisualStyleHandleCreationFailed, e);
                    }

                    return(null);
                }

                if (hTheme == IntPtr.Zero)
                {
                    if (throwExceptionOnFail)
                    {
                        throw new InvalidOperationException(SR.VisualStyleHandleCreationFailed);
                    }

                    return(null);
                }

                return(new ThemeHandle(hTheme));
            }
Ejemplo n.º 6
0
        /// <inheritdoc />
        /// <summary>Raises the <see cref="M:System.Windows.Forms.Control.CreateControl" /> method.</summary>
        protected override void OnCreateControl()
        {
            // remove border
            BorderStyle = BorderStyle.None;

            if (!DesignMode)
            {
                // set native look for list view items
                UxTheme.SetWindowTheme(Handle, "explorer", null);

                // remove focus cues
                User32.SendMessage(Handle,
                                   (uint)User32.WindowMessage.WM_CHANGEUISTATE,
                                   new IntPtr(BitHelper.MakeLong((int)User32.UIStateFlags.UIS_SET,
                                                                 (int)User32.UIStateFlags.UISF_HIDEFOCUS)),
                                   IntPtr.Zero);

                // enable double buffer to reduce flicker and display a traslucent selection box
                User32.SendMessage(Handle,
                                   (uint)User32.WindowMessage.LVM_SETEXTENDEDLISTVIEWSTYLE,
                                   new IntPtr((int)User32.WindowStylesEx.LVS_EX_DOUBLEBUFFER),
                                   new IntPtr((int)User32.WindowStylesEx.LVS_EX_DOUBLEBUFFER));

                // reset hot cursor
                User32.SendMessage(Handle,
                                   (uint)User32.WindowMessage.LVM_SETHOTCURSOR,
                                   IntPtr.Zero,
                                   Cursors.Arrow.Handle);
            }

            base.OnCreateControl();
        }
        public static string GetCurrentThemePath()
        {
            var sbFilename = new StringBuilder(0x200);
            var s          = UxTheme.GetCurrentThemeName(sbFilename, 0x200, null, 0, null, 0);

            return(sbFilename.ToString());
        }
Ejemplo n.º 8
0
 protected override void OnHandleCreated(EventArgs e)
 {
     base.OnHandleCreated(e);
     SetDoubleBuffer();
     SetExpandoesStyle();
     SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_AUTOHSCROLL, (IntPtr)TVS_EX_AUTOHSCROLL);
     UxTheme.SetWindowTheme(this.Handle, "Explorer", 0);
 }
Ejemplo n.º 9
0
 public override void DrawParentBackground(Graphics graphics, Rectangle bounds, Control childControl)
 {
     using (var mh = ManagedHdc.FromGraphics(graphics))
     {
         var rect = Win32.RECT.FromRectangle(bounds);
         UxTheme.DrawThemeParentBackground(_theme, mh.Hdc, ref rect);
     }
 }
Ejemplo n.º 10
0
 public override void DrawBackground(Graphics graphics, Rectangle bounds)
 {
     using (var mh = ManagedHdc.FromGraphics(graphics))
     {
         var rect = Win32.RECT.FromRectangle(bounds);
         UxTheme.DrawThemeBackground(_theme, mh.Hdc, _partId, _stateId, ref rect, IntPtr.Zero);
     }
 }
Ejemplo n.º 11
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 = UxTheme.IsThemePartDefined(hTheme, part, 0).IsTrue();
                }
            }

            //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 = UxTheme.IsThemePartDefined(tHandle, part, 0).IsTrue();
                    }

                    //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);
        }
Ejemplo n.º 12
0
 public override void DrawText(Graphics graphics, Rectangle bounds, string text)
 {
     using (var mh = ManagedHdc.FromGraphics(graphics))
     {
         var  rect  = Win32.RECT.FromRectangle(bounds);
         uint flags = (int)(TextFormatFlags.EndEllipsis);
         UxTheme.DrawThemeText(_theme, mh.Hdc, _partId, _stateId, text, text.Length, flags, 0, ref rect);
     }
 }
Ejemplo n.º 13
0
 protected override void OnHandleCreated(EventArgs e)
 {
     if (!IsDesignMode)
     {
         UxTheme.SetWindowTheme(this.Handle, string.Empty, string.Empty);
         User32.DisableProcessWindowsGhosting();
     }
     base.OnHandleCreated(e);
 }
Ejemplo n.º 14
0
            public void Dispose()
            {
                if (Handle != IntPtr.Zero)
                {
                    UxTheme.CloseThemeData(Handle);
                    Handle = IntPtr.Zero;
                }

                GC.SuppressFinalize(this);
            }
Ejemplo n.º 15
0
 public NativeStyleRenderer(IntPtr handle, VisualStyleElement element)
 {
     if (element == null)
     {
         throw new ArgumentNullException("element", "element is null.");
     }
     _theme   = UxTheme.OpenTheme(handle, element.ClassName);
     _partId  = element.Part;
     _stateId = element.State;
 }
Ejemplo n.º 16
0
        public bool GetBoolean(BooleanProperty prop)
        {
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)BooleanProperty.Transparent, (int)BooleanProperty.SourceShrink))
            {
                throw new InvalidEnumArgumentException(nameof(prop), (int)prop, typeof(BooleanProperty));
            }

            BOOL val = BOOL.FALSE;

            lastHResult = UxTheme.GetThemeBool(this, part, state, (int)prop, ref val);
            return(val.IsTrue());
        }
Ejemplo n.º 17
0
        internal static void SetWindowTheme(Control window, String SubAppName, String SubIdList)
        {
            if (!Application.RenderWithVisualStyles)
            {
                return;
            }
            int result;

            if ((result = UxTheme.SetWindowTheme(window.Handle, SubAppName, SubIdList)) < 0)
            {
                throw new InvalidOperationException("SetWindowTheme failed with error " + result.ToString());
            }
        }
Ejemplo n.º 18
0
 public Color this[UInt32 colorType] {
     get {
         UInt32 nativeColor = UxTheme.GetImmersiveColorFromColorSetEx(this._colorSet, colorType, false, 0);
         //if (nativeColor == 0)
         //    throw new InvalidOperationException();
         return(Color.FromArgb(
                    (Byte)((0xFF000000 & nativeColor) >> 24),
                    (Byte)((0x000000FF & nativeColor) >> 0),
                    (Byte)((0x0000FF00 & nativeColor) >> 8),
                    (Byte)((0x00FF0000 & nativeColor) >> 16)
                    ));
     }
 }
Ejemplo n.º 19
0
        public int GetInteger(IntegerProperty prop)
        {
            //valid values are 0x961 to 0x978
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)IntegerProperty.ImageCount, (int)IntegerProperty.MinDpi5))
            {
                throw new InvalidEnumArgumentException(nameof(prop), (int)prop, typeof(IntegerProperty));
            }

            int val = 0;

            lastHResult = UxTheme.GetThemeInt(this, part, state, (int)prop, ref val);
            return(val);
        }
Ejemplo n.º 20
0
        public int GetEnumValue(EnumProperty prop)
        {
            //valid values are 0xfa1 to 0xfaf
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)EnumProperty.BackgroundType, (int)EnumProperty.TrueSizeScalingType))
            {
                throw new InvalidEnumArgumentException(nameof(prop), (int)prop, typeof(EnumProperty));
            }

            int val = 0;

            lastHResult = UxTheme.GetThemeEnumValue(this, part, state, (int)prop, ref val);
            return(val);
        }
Ejemplo n.º 21
0
        public Color GetColor(ColorProperty prop)
        {
            //valid values are 0xed9 to 0xeef
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)ColorProperty.BorderColor, (int)ColorProperty.AccentColorHint))
            {
                throw new InvalidEnumArgumentException(nameof(prop), (int)prop, typeof(ColorProperty));
            }

            int color = 0;

            lastHResult = UxTheme.GetThemeColor(this, part, state, (int)prop, ref color);
            return(ColorTranslator.FromWin32(color));
        }
Ejemplo n.º 22
0
 public void GetThemeStreamTest()
 {
     Application.EnableVisualStyles();
     using (var h = OpenThemeData(HWND.NULL, "DWMWINDOW"))
     {
         Assert.That(h.IsInvalid, Is.False);
         using (var hInstance = Kernel32.LoadLibraryEx(@"C:\Windows\resources\themes\Aero\Aero.msstyles", Kernel32.LoadLibraryExFlags.LOAD_LIBRARY_AS_DATAFILE))
         {
             var hr = UxTheme.GetThemeStream(h, 0, 0, 213, out var themeStream, out var streamSize, hInstance);
             Assert.That(hr.Succeeded);
             Assert.That(streamSize, Is.GreaterThan(0));
             Assert.That(() => themeStream.ToArray <byte>((int)streamSize), Throws.Nothing);
         }
     }
 }
Ejemplo n.º 23
0
        public ArchiveDetailView(string pathIconLibrary, string pathArchive)
        {
            InitializeComponent();


            //_iconReader = new IconReader();
            //archiveTree.ImageList = new ImageList();
            //_iconReader.ReadIcons(pathIconLibrary).ForEach(o => archiveTree.ImageList.Images.Add(o.Icon));
            UxTheme.SetWindowTheme(lvArchiveDetails.Handle, "Explorer", 0);
            SysImageListHelper.SetListViewImageList(lvArchiveDetails, lst, false);
            SevenZipExtractor.SetLibraryPath(IntPtr.Size == 8 ? "7z64.dll" : "7z32.dll");
            _pathArchive = pathArchive;

            Shown += ShowArchiveContent;
        }
Ejemplo n.º 24
0
        // HACK: GetAllColorNames collects the available color names by brute forcing the OS function.
        //   Since there is currently no known way to retrieve all possible color names,
        //   the method below just tries all indices from 0 to 0xFFF ignoring errors.
        public List <String> GetAllColorNames()
        {
            List <String> allColorNames = new List <String>();

            for (UInt32 i = 0; i < 0xFFF; i++)
            {
                IntPtr typeNamePtr = UxTheme.GetImmersiveColorNamedTypeByIndex(i);
                if (typeNamePtr != IntPtr.Zero)
                {
                    IntPtr typeName = (IntPtr)Marshal.PtrToStructure(typeNamePtr, typeof(IntPtr));
                    allColorNames.Add(Marshal.PtrToStringUni(typeName));
                }
            }

            return(allColorNames);
        }
Ejemplo n.º 25
0
        private void DrawBorder(System.Drawing.Graphics g)
        {
            IntPtr hTheme = UxTheme.OpenThemeData(this.Handle, "EDIT");

            // Makes sure Windows XP is running and
            //  a .manifest file exists for the EXE.
            if (Environment.OSVersion.Version.Major >= 5 &&
                Environment.OSVersion.Version.Minor > 0 &&
                System.IO.File.Exists(Application.ExecutablePath + ".manifest") &&
                (hTheme != IntPtr.Zero))
            {
                //Get DC
                IntPtr hDC = g.GetHdc();

                int state = (int)UxTheme.ETS_NORMAL;
                switch (this.m_state)
                {
                case State.Disabled:
                    state = (int)UxTheme.ETS_DISABLED;
                    break;

                default:
                    break;
                }

                try
                {
                    RECT r = new RECT(this.ClientRectangle.Left, this.ClientRectangle.Right, this.ClientRectangle.Top, this.ClientRectangle.Bottom);

                    //Render button
                    IntPtr hr = UxTheme.DrawThemeBackground(hTheme, hDC, UxTheme.EP_EDITTEXT, state, r, null);
                }
                finally
                {
                    //Release DC
                    g.ReleaseHdc(hDC);
                    UxTheme.CloseThemeData(hTheme);
                }
            }
            else
            {
                using (Graphics y = this.CreateGraphics())
                {
                    ControlPaint.DrawBorder(y, ClientRectangle, SystemColors.Control, ButtonBorderStyle.Inset);
                }
            }
        }
Ejemplo n.º 26
0
        public TaskbarButton(VisibleWindow window)
        {
            if (window == null)
            {
                throw new ArgumentNullException("window", "window is null.");
            }
            Window = window;
            Icon   = window.Icon;
            Image  = Icon.ToBitmap();
            Text   = window.Title;
            Margin = Padding.Empty;

            //TODO: find a way to do this managed...
            //      it just doesnt work to call this and use
            //      a managed VisualStyleRenderer instead =/
            UxTheme.SetWindowTheme(base.Handle, "Taskband", null);
        }
        public static Image GetDwmWindowButton(AeroTitlebarButtonPart button, TitlebarButtonState state)
        {
            const int tmtAtlasrect  = 8002;
            const int tmtImagecount = 2401;

            using (var vs = GetDwmWindowVisualTheme())
            {
                using (var hInstance = LoadAeroTheme())
                {
                    Image result;
                    using (var nWindow = CreateNativeWindow())
                    {
                        using (var theme = LoadAeroThemeData(nWindow))
                        {
                            var atlas = GetImageAtlasFromTheme(vs, hInstance);
                            if (atlas == null)
                            {
                                return(null);
                            }

                            var rect = GetThemeRect(theme, (int)button, (int)state, tmtAtlasrect);

                            result = Slice(atlas, rect.Location, rect.Size);
                            if (state != TitlebarButtonState.None)
                            {
                                //Slice the buttons
                                UxTheme.GetThemeInt(theme, (int)button, (int)state, tmtImagecount, out var count);

                                var buttonSize = rect.Height / count;

                                var startPoint = Point.Empty;
                                var btnSize    = new Size(rect.Width, buttonSize);
                                startPoint.Offset(0, buttonSize * ((int)state - 1));

                                var buttonRect = new Rectangle(startPoint, btnSize);
                                buttonRect.Inflate(-1, -1);
                                result = Slice(result, buttonRect.Location, buttonRect.Size);
                            }
                        }
                    }

                    return(result);
                }
            }
        }
Ejemplo n.º 28
0
        // HACK: GetAllColorNames collects the available color names by brute forcing the OS function.
        //   Since there is currently no known way to retrieve all possible color names,
        //   the method below just tries all indices from 0 to 0xFFF ignoring errors.
        public List <string> GetAllColorNames()
        {
            var allColorNames = new List <string>();

            for (uint i = 0; i < 0xFFF; i++)
            {
                var typeNamePtr = UxTheme.GetImmersiveColorNamedTypeByIndex(i);
                if (typeNamePtr == IntPtr.Zero)
                {
                    continue;
                }

                var typeName = (IntPtr)Marshal.PtrToStructure(typeNamePtr, typeof(IntPtr));
                allColorNames.Add(Marshal.PtrToStringUni(typeName));
            }

            return(allColorNames);
        }
Ejemplo n.º 29
0
        public Font GetFont(IDeviceContext dc, FontProperty prop)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            //valid values are 0xa29 to 0xa29
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)FontProperty.GlyphFont, (int)FontProperty.GlyphFont))
            {
                throw new InvalidEnumArgumentException(nameof(prop), (int)prop, typeof(FontProperty));
            }

            User32.LOGFONTW logfont = new User32.LOGFONTW();

            using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties))
            {
                HandleRef hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc);
                lastHResult = UxTheme.GetThemeFont(new HandleRef(this, Handle), hdc, part, state, (int)prop, ref logfont);
            }

            Font font = null;

            //check for a failed HR.
            if (lastHResult.Succeeded())
            {
                try
                {
                    font = Font.FromLogFont(logfont);
                }
                catch (Exception e)
                {
                    if (ClientUtils.IsSecurityOrCriticalException(e))
                    {
                        throw;
                    }

                    //Looks like the font was not true type
                    font = null;
                }
            }

            return(font);
        }
Ejemplo n.º 30
0
        static void Main()
        {
            try
            {
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                WindowManager.Start();

                Application.Run(new Taskbar());
            }
            finally
            {
                WindowManager.Stop();
                UxTheme.CloseThemes();
                AppBar.UnregisterAll();
            }
        }