예제 #1
0
        public void DrawBackground(IDeviceContext graphics, int partId, int stateId, Rectangle bounds, Rectangle clipRect)
        {
            var b = new RECT(bounds);
            var o = new DrawThemeBackgroundOptions(clipRect);             // {OmitBorder = true, OmitContent = true};

            using (var hdc = new Gdi32.SafeDCHandle(graphics))
                DrawThemeBackgroundEx(hTheme, hdc, partId, stateId, ref b, o);
        }
예제 #2
0
        public void DrawText(IDeviceContext graphics, int partId, int stateId, Rectangle bounds, string text, TextFormatFlags fmt = TextFormatFlags.Default, DrawThemeTextOptions?options = null, Font font = null)
        {
            var b  = new RECT(bounds);
            var dt = options ?? DrawThemeTextOptions.Default;             //new DrawThemeTextOptions(true) {AntiAliasedAlpha = true, BorderSize = 10, BorderColor = Color.Red, ApplyOverlay = true, ShadowType = TextShadowType.Continuous, ShadowColor = Color.White, ShadowOffset = new Point(2, 2), GlowSize = 18, TextColor = Color.White, Callback = DrawTextCallback };

            using (var hdc = new Gdi32.SafeDCHandle(graphics))
                using (new Gdi32.SafeDCObjectHandle(hdc, font?.ToHfont() ?? IntPtr.Zero))
                    DrawThemeTextEx(hTheme, hdc, partId, stateId, text, text.Length, (User32.DrawTextFlags)fmt, ref b, ref dt);
        }
예제 #3
0
 public Bitmap GetBitmap(IDeviceContext graphics, int partId, int stateId, int propId)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
     {
         IntPtr hBmp;
         if (0 == GetThemeBitmap(hTheme, hdc, partId, stateId, propId, 0, out hBmp))
         {
             return(Image.FromHbitmap(hBmp));
         }
     }
     return(null);
 }
예제 #4
0
 public Size?GetPartSize(IDeviceContext graphics, int partId, int stateId, Rectangle?destRect, ThemeSize themeSize)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
     {
         Size sz;
         if (0 != GetThemePartSize(hTheme, hdc, partId, stateId, destRect, themeSize, out sz))
         {
             return(null);
         }
         return(sz);
     }
 }
예제 #5
0
 public int?GetMetric(IDeviceContext graphics, int partId, int stateId, int propId)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
     {
         int i;
         if (0 == GetThemeMetric(hTheme, hdc, partId, stateId, propId, out i))
         {
             return(i);
         }
     }
     return(null);
 }
예제 #6
0
 public Padding?GetMargins(IDeviceContext graphics, int partId, int stateId, int propId)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
     {
         RECT rc;
         if (0 == GetThemeMargins(hTheme, hdc, partId, stateId, propId, IntPtr.Zero, out rc))
         {
             return(new Padding(rc.Left, rc.Top, rc.Right, rc.Bottom));
         }
     }
     return(null);
 }
예제 #7
0
 public Font GetFont(IDeviceContext graphics, int partId, int stateId, int propId)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
     {
         Gdi32.LOGFONT f;
         if (0 == GetThemeFont(hTheme, hdc, partId, stateId, propId, out f))
         {
             return(f.ToFont());
         }
     }
     return(null);
 }
        /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

            var screenDc = Win32.GetDC(IntPtr.Zero);

            using (var memDc = new Gdi32.SafeDCHandle(Gdi32.CreateCompatibleDC(screenDc)))
            {
                var hBitmap   = IntPtr.Zero;
                var oldBitmap = IntPtr.Zero;

                try
                {
                    hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                    oldBitmap = Gdi32.SelectObject(memDc, hBitmap);

                    var size        = new Win32.Size(bitmap.Width, bitmap.Height);
                    var pointSource = new Win32.Point(0, 0);
                    var topPos      = new Win32.Point(Left, Top);
                    var blend       = new Win32.Blendfunction
                    {
                        BlendOp             = Win32.AcSrcOver,
                        BlendFlags          = 0,
                        SourceConstantAlpha = opacity,
                        AlphaFormat         = Win32.AcSrcAlpha
                    };

                    Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc.DangerousGetHandle(),
                                              ref pointSource, 0, ref blend,
                                              Win32.UlwAlpha);
                }
                finally
                {
                    Win32.ReleaseDC(IntPtr.Zero, screenDc);
                    if (hBitmap != IntPtr.Zero)
                    {
                        Gdi32.SelectObject(memDc, oldBitmap);
                        //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                        Gdi32.DeleteObject(hBitmap);

                        Size = bitmap.Size;
                    }
                }
            }
        }
예제 #9
0
        public Rectangle?GetBackgroundContentRect(IDeviceContext graphics, int partId, int stateId, Rectangle bounds)
        {
            RECT b = new RECT(bounds);

            using (var hdc = new Gdi32.SafeDCHandle(graphics))
            {
                RECT rc;
                if (0 == GetThemeBackgroundContentRect(hTheme, hdc, partId, stateId, ref b, out rc))
                {
                    return(rc);
                }
            }
            return(null);
        }
예제 #10
0
 public void DrawParentBackground(IWin32Window childWindow, IDeviceContext graphics, Rectangle?bounds = null)
 {
     using (var hdc = new Gdi32.SafeDCHandle(graphics))
         DrawThemeParentBackground(new HandleRef(childWindow, childWindow.Handle), hdc, bounds);
 }