Exemple #1
0
        /// <summary>
        /// Sets opacity and/or transparent color.
        /// </summary>
        /// <param name="allowTransparency">Set or remove WS_EX_LAYERED style that is required for transparency. If false, other parameters are not used.</param>
        /// <param name="opacity">Opacity from 0 (completely transparent) to 255 (opaque). Does not change if null. If less than 0 or greater than 255, makes 0 or 255.</param>
        /// <param name="colorKey">Make pixels of this color completely transparent. Does not change if null. The alpha byte is not used.</param>
        /// <exception cref="AuWndException"/>
        /// <remarks>
        /// Uses API <msdn>SetLayeredWindowAttributes</msdn>.
        /// On Windows 7 works only with top-level windows, on newer OS also with controls.
        /// </remarks>
        public void SetTransparency(bool allowTransparency, int?opacity = null, ColorInt?colorKey = null)
        {
            var  est     = ExStyle;
            bool layered = (est & WS2.LAYERED) != 0;

            if (allowTransparency)
            {
                uint col = 0, f = 0; byte op = 0;
                if (colorKey != null)
                {
                    f |= 1; col = (uint)colorKey.GetValueOrDefault().ToBGR();
                }
                if (opacity != null)
                {
                    f |= 2; op = (byte)AMath.MinMax(opacity.GetValueOrDefault(), 0, 255);
                }

                if (!layered)
                {
                    SetExStyle(est | WS2.LAYERED);
                }
                if (!Api.SetLayeredWindowAttributes(this, col, op, f))
                {
                    ThrowUseNative();
                }
            }
            else if (layered)
            {
                SetExStyle(est & ~WS2.LAYERED);                 //tested: resets attributes, ie after adding WS2.LAYERED the window will be normal
            }
        }
Exemple #2
0
 public void SetLevel(int level)
 {
     this.level = (ushort)AMath.MinMax(level, 0, 0xffff);
 }