SelectObject() private method

private SelectObject ( IntPtr hDC, IntPtr hObject ) : IntPtr
hDC System.IntPtr
hObject System.IntPtr
return System.IntPtr
Beispiel #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            var outerBounds = new Rectangle(0, 0, Width, Height);

            using (var target = new Bitmap(outerBounds.Width, outerBounds.Height, PixelFormat.Format32bppArgb))
            {
                using (var targetGraphics = Graphics.FromImage(target))
                {
                    // The top and bottom borders extend over the sides of the window.
                    // The left and right borders do no. This means that we need to
                    // update the bounds to make it seem like the left and right
                    // borders do extend outside of the window.

                    if (Border == DropShadowBorder.Left || Border == DropShadowBorder.Right)
                    {
                        outerBounds = new Rectangle(
                            outerBounds.Left,
                            outerBounds.Top - _borderWidth,
                            outerBounds.Width,
                            outerBounds.Height + _borderWidth * 2
                            );
                    }

                    if (Border == DropShadowBorder.Left || Border == DropShadowBorder.Top)
                    {
                        DrawImage(targetGraphics, _imageCache.CornerNW,
                                  new Point(outerBounds.Left, outerBounds.Top)
                                  );
                    }

                    if (Border == DropShadowBorder.Right || Border == DropShadowBorder.Top)
                    {
                        DrawImage(targetGraphics, _imageCache.CornerNE,
                                  new Point(outerBounds.Right - _cornerSize, outerBounds.Top)
                                  );
                    }

                    if (Border == DropShadowBorder.Bottom || Border == DropShadowBorder.Left)
                    {
                        DrawImage(targetGraphics, _imageCache.CornerSW,
                                  new Point(outerBounds.Left, outerBounds.Bottom - _cornerSize)
                                  );
                    }

                    if (Border == DropShadowBorder.Bottom || Border == DropShadowBorder.Right)
                    {
                        DrawImage(targetGraphics, _imageCache.CornerSE,
                                  new Point(outerBounds.Right - _cornerSize, outerBounds.Bottom - _cornerSize)
                                  );
                    }

                    if (Border == DropShadowBorder.Top)
                    {
                        DrawBorder(targetGraphics, _imageCache.BorderN,
                                   new Rectangle(
                                       outerBounds.Left + _cornerSize,
                                       outerBounds.Top,
                                       outerBounds.Width - _cornerSize * 2,
                                       _borderWidth
                                       )
                                   );
                    }

                    if (Border == DropShadowBorder.Bottom)
                    {
                        DrawBorder(targetGraphics, _imageCache.BorderS,
                                   new Rectangle(
                                       outerBounds.Left + _cornerSize,
                                       outerBounds.Bottom - _borderWidth,
                                       outerBounds.Width - _cornerSize * 2,
                                       _borderWidth
                                       )
                                   );
                    }

                    if (Border == DropShadowBorder.Left)
                    {
                        DrawBorder(targetGraphics, _imageCache.BorderW,
                                   new Rectangle(
                                       outerBounds.Left,
                                       outerBounds.Top + _cornerSize,
                                       _borderWidth,
                                       outerBounds.Height - _cornerSize * 2
                                       )
                                   );
                    }

                    if (Border == DropShadowBorder.Right)
                    {
                        DrawBorder(targetGraphics, _imageCache.BorderE,
                                   new Rectangle(
                                       outerBounds.Right - _borderWidth,
                                       outerBounds.Top + _cornerSize,
                                       _borderWidth,
                                       outerBounds.Height - _cornerSize * 2
                                       )
                                   );
                    }
                }

                // Get device contexts
                var screenDc   = PI.GetDC(IntPtr.Zero);
                var memDc      = PI.CreateCompatibleDC(screenDc);
                var hBitmap    = IntPtr.Zero;
                var hOldBitmap = IntPtr.Zero;

                try
                {
                    // Get handle to the new bitmap and select it into the current device context
                    hBitmap    = target.GetHbitmap(Color.FromArgb(0));
                    hOldBitmap = PI.SelectObject(memDc, hBitmap);  // Set parameters for layered window update

                    var newSize        = new PI.SIZE(target.Size); // Size window to match bitmap
                    var sourceLocation = new PI.POINT(Point.Empty);
                    var newLocation    = new PI.POINT(Location);   // Same as this window

                    var blend = new PI.BLENDFUNCTION
                    {
                        BlendOp             = PI.AC_SRC_OVER, // Only works with a 32bpp bitmap
                        BlendFlags          = 0,              // Always 0
                        SourceConstantAlpha = 255,            // Set to 255 for per-pixel alpha values
                        AlphaFormat         = PI.AC_SRC_ALPHA // Only works when the bitmap contains an alpha channel
                    };

                    // Update the window
                    PI.UpdateLayeredWindow(
                        Handle, screenDc, ref newLocation, ref newSize,
                        memDc, ref sourceLocation, 0, ref blend,
                        PI.ULW_ALPHA
                        );
                }
                finally
                {
                    // Release device context
                    PI.ReleaseDC(IntPtr.Zero, screenDc);

                    if (hBitmap != IntPtr.Zero)
                    {
                        PI.SelectObject(memDc, hOldBitmap);
                        PI.DeleteObject(hBitmap); // Remove bitmap resources
                    }

                    PI.DeleteDC(memDc);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Draw text without a glowing background, for use on a composition element.
        /// </summary>
        /// <param name="g">Graphics reference.</param>
        /// <param name="text">Text to be drawn.</param>
        /// <param name="font">Font to use for text.</param>
        /// <param name="bounds">Bounding area for the text.</param>
        /// <param name="state">State of the source element.</param>
        /// <param name="color"><see cref="Color"/> of the text.</param>
        /// <param name="copyBackground">Should existing background be copied into the bitmap.</param>
        /// <param name="sf">StringFormat of the memento.</param>
        public static void DrawCompositionText(Graphics g,
                                               string text,
                                               Font font,
                                               Rectangle bounds,
                                               PaletteState state,
                                               Color color,
                                               bool copyBackground,
                                               StringFormat sf)
        {
            // Get the hDC for the graphics instance and create a memory DC
            IntPtr gDC = g.GetHdc();

            try
            {
                IntPtr mDC = PI.CreateCompatibleDC(gDC);

                PI.BITMAPINFO bmi = new PI.BITMAPINFO
                {
                    biWidth       = bounds.Width,
                    biHeight      = -(bounds.Height),
                    biCompression = 0,
                    biBitCount    = 32,
                    biPlanes      = 1
                };
                bmi.biSize = (uint)Marshal.SizeOf(bmi);

                // Create a device independent bitmap and select into the memory DC
                IntPtr hDIB = PI.CreateDIBSection(gDC, ref bmi, 0, out _, IntPtr.Zero, 0);
                PI.SelectObject(mDC, hDIB);

                if (copyBackground)
                {
                    // Copy existing background into the bitmap
                    PI.BitBlt(mDC, 0, 0, bounds.Width, bounds.Height,
                              gDC, bounds.X, bounds.Y, 0x00CC0020);
                }

                // Select the font for use when drawing
                IntPtr hFont = font.ToHfont();
                PI.SelectObject(mDC, hFont);

                // Get renderer for the correct state
                VisualStyleRenderer renderer = new VisualStyleRenderer(state == PaletteState.Normal ? VisualStyleElement.Window.Caption.Active :
                                                                       VisualStyleElement.Window.Caption.Inactive);

                // Create structures needed for theme drawing call
                PI.RECT textBounds = new PI.RECT
                {
                    left   = 0,
                    top    = 0,
                    right  = (bounds.Right - bounds.Left),
                    bottom = (bounds.Bottom - bounds.Top)
                };
                PI.DTTOPTS dttOpts = new PI.DTTOPTS
                {
                    dwSize  = Marshal.SizeOf(typeof(PI.DTTOPTS)),
                    dwFlags = PI.DTT_COMPOSITED | PI.DTT_TEXTCOLOR,
                    crText  = ColorTranslator.ToWin32(color)
                };

                // Always draw text centered
                TextFormatFlags textFormat = TextFormatFlags.SingleLine |
                                             TextFormatFlags.HorizontalCenter |
                                             TextFormatFlags.VerticalCenter;
                ////Seb   |  TextFormatFlags.EndEllipsis;


                // Perform actual drawing
                //PI.DrawThemeTextEx(renderer.Handle,
                //                   mDC, 0, 0,
                //                   text, -1, (int)StringFormatToFlags(sf),
                //                   ref textBounds, ref dttOpts);
                PI.DrawThemeTextEx(renderer.Handle,
                                   mDC, 0, 0,
                                   text, -1, (int)textFormat,
                                   ref textBounds, ref dttOpts);

                // Copy to foreground
                PI.BitBlt(gDC,
                          bounds.Left, bounds.Top,
                          bounds.Width, bounds.Height,
                          mDC, 0, 0, 0x00CC0020);

                // Dispose of allocated objects
                PI.DeleteObject(hFont);
                PI.DeleteObject(hDIB);
                PI.DeleteDC(mDC);
            }
            catch
            {
                // ignored
            }
            finally
            {
                // Must remember to release the hDC
                g.ReleaseHdc(gDC);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Draw text with a glowing background, for use on a composition element.
        /// </summary>
        /// <param name="g">Graphics reference.</param>
        /// <param name="text">Text to be drawn.</param>
        /// <param name="font">Font to use for text.</param>
        /// <param name="bounds">Bounding area for the text.</param>
        /// <param name="state">State of the source element.</param>
        /// <param name="color">Color of the text.</param>
        /// <param name="copyBackground">Should existing background be copied into the bitmap.</param>
        public static void DrawCompositionGlowingText(Graphics g,
                                                      string text,
                                                      Font font,
                                                      Rectangle bounds,
                                                      PaletteState state,
                                                      Color color,
                                                      bool copyBackground)
        {
            try
            {
                // Get the hDC for the graphics instance and create a memory DC
                IntPtr gDC = g.GetHdc();
                IntPtr mDC = PI.CreateCompatibleDC(gDC);

                PI.BITMAPINFO bmi = new PI.BITMAPINFO();
                bmi.biSize        = Marshal.SizeOf(bmi);
                bmi.biWidth       = bounds.Width;
                bmi.biHeight      = -(bounds.Height + GLOW_EXTRA_HEIGHT * 2);
                bmi.biCompression = 0;
                bmi.biBitCount    = 32;
                bmi.biPlanes      = 1;

                // Create a device independant bitmp and select into the memory DC
                IntPtr hDIB = PI.CreateDIBSection(gDC, bmi, 0, 0, IntPtr.Zero, 0);
                PI.SelectObject(mDC, hDIB);

                if (copyBackground)
                {
                    // Copy existing background into the bitmap
                    PI.BitBlt(mDC, 0, 0, bounds.Width, bounds.Height + GLOW_EXTRA_HEIGHT * 2,
                              gDC, bounds.X, bounds.Y - GLOW_EXTRA_HEIGHT, 0x00CC0020);
                }

                // Select the font for use when drawing
                IntPtr hFont = font.ToHfont();
                PI.SelectObject(mDC, hFont);

                // Get renderer for the correct state
                VisualStyleRenderer renderer = new VisualStyleRenderer(state == PaletteState.Normal ? VisualStyleElement.Window.Caption.Active :
                                                                       VisualStyleElement.Window.Caption.Inactive);

                // Create structures needed for theme drawing call
                PI.RECT textBounds = new PI.RECT();
                textBounds.left   = 0;
                textBounds.top    = 0;
                textBounds.right  = (bounds.Right - bounds.Left);
                textBounds.bottom = (bounds.Bottom - bounds.Top) + (GLOW_EXTRA_HEIGHT * 2);
                PI.DTTOPTS dttOpts = new PI.DTTOPTS();
                dttOpts.dwSize    = Marshal.SizeOf(typeof(PI.DTTOPTS));
                dttOpts.dwFlags   = PI.DTT_COMPOSITED | PI.DTT_GLOWSIZE | PI.DTT_TEXTCOLOR;
                dttOpts.crText    = ColorTranslator.ToWin32(color);
                dttOpts.iGlowSize = 11;

                // Always draw text centered
                TextFormatFlags textFormat = TextFormatFlags.SingleLine |
                                             TextFormatFlags.HorizontalCenter |
                                             TextFormatFlags.VerticalCenter |
                                             TextFormatFlags.EndEllipsis;

                // Perform actual drawing
                PI.DrawThemeTextEx(renderer.Handle,
                                   mDC, 0, 0,
                                   text, -1, (int)textFormat,
                                   ref textBounds, ref dttOpts);

                // Copy to foreground
                PI.BitBlt(gDC,
                          bounds.Left, bounds.Top - GLOW_EXTRA_HEIGHT,
                          bounds.Width, bounds.Height + (GLOW_EXTRA_HEIGHT * 2),
                          mDC, 0, 0, 0x00CC0020);

                // Dispose of allocated objects
                PI.DeleteObject(hFont);
                PI.DeleteObject(hDIB);
                PI.DeleteDC(mDC);

                // Must remember to release the hDC
                g.ReleaseHdc(gDC);
            }
            catch
            {
            }
        }