Пример #1
0
        /// <summary>
        /// Draws composited text onto the glass area of a form.
        /// </summary>
        /// <param name="dc">The <see cref="IDeviceContext"/> onto which the composited text should be drawn.</param>
        /// <param name="text">The text to draw.</param>
        /// <param name="font">The <see cref="Font"/> to apply to the drawn text.</param>
        /// <param name="bounds">The <see cref="Rectangle" /> that represents the bounds of the text.</param>
        /// <param name="padding">The <see cref="Padding"/> around the text; necessary to allow space for the glow effect.</param>
        /// <param name="foreColor">The <see cref="Color" /> to apply to the drawn text.</param>
        /// <param name="textFormat">A bitwise combination of the <see cref="TextFormatFlags" /> values.</param>
        /// <param name="glowSize">Specifies the size of a glow that will be drawn on the background prior to any text being drawn.</param>
        /// <remarks>
        /// <para>
        ///   Do not use this method to draw text on non-glass areas of a form.
        /// </para>
        /// </remarks>
        /// <exception cref="NotSupportedException">The current operating system does not support glass, or the Desktop Window Manager is not enabled.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="dc"/>, <paramref name="text"/> or <paramref name="font"/> is <see langword="null"/>.</exception>
        public static void DrawCompositedText(IDeviceContext dc, string text, Font font, Rectangle bounds, Padding padding, Color foreColor, int glowSize, TextFormatFlags textFormat)
        {
            if (!IsDwmCompositionEnabled)
            {
                throw new NotSupportedException("The current operating system does not support glass, or the Desktop Window Manager is not enabled.");
            }

            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }

            IntPtr primaryHdc = dc.GetHdc();

            try
            {
                using (SafeDeviceHandle memoryHdc = Natives.CreateCompatibleDC(primaryHdc))
                    using (SafeGDIHandle fontHandle = new SafeGDIHandle(font.ToHfont(), true))
                        using (SafeGDIHandle dib = Natives.CreateDib(bounds, primaryHdc, memoryHdc))
                        {
                            Natives.SelectObject(memoryHdc, fontHandle);

                            // Draw glowing text
                            System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
                            Natives.DTTOPTS dttOpts = new Natives.DTTOPTS();
                            dttOpts.dwSize    = Marshal.SizeOf(typeof(Natives.DTTOPTS));
                            dttOpts.dwFlags   = Natives.DrawThemeTextFlags.Composited | Natives.DrawThemeTextFlags.GlowSize | Natives.DrawThemeTextFlags.TextColor;
                            dttOpts.crText    = ColorTranslator.ToWin32(foreColor);
                            dttOpts.iGlowSize = glowSize;
                            Natives.RECT textBounds = new Natives.RECT(padding.Left, padding.Top, bounds.Width - padding.Right, bounds.Height - padding.Bottom);
                            Natives.DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, text.Length, (int)textFormat, ref textBounds, ref dttOpts);

                            // Copy to foreground
                            const int SRCCOPY = 0x00CC0020;
                            Natives.BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY);
                        }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Пример #2
0
        /// <summary>
        /// Provides the size, in pixels, of the specified text.
        /// </summary>
        /// <param name="dc">The device context in which to measure the text.</param>
        /// <param name="text">The text to measure.</param>
        /// <param name="font">The <see cref="Font"/> to apply to the measured text.</param>
        /// <param name="textFormat">A bitwise combination of the <see cref="TextFormatFlags" /> values.</param>
        /// <returns>The <see cref="Size"/>, in pixels, of <paramref name="text"/> drawn with the specified <paramref name="font"/> and format.</returns>
        /// <exception cref="NotSupportedException">The current operating system does not support glass, or the Desktop Window Manager is not enabled.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="dc"/>, <paramref name="text"/> or <paramref name="font"/> is <see langword="null"/>.</exception>
        public static Size MeasureCompositedText(IDeviceContext dc, string text, Font font, TextFormatFlags textFormat)
        {
            if (!IsDwmCompositionEnabled)
            {
                throw new NotSupportedException("The current operating system does not support glass, or the Desktop Window Manager is not enabled.");
            }

            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }

            IntPtr primaryHdc = dc.GetHdc();

            try
            {
                Rectangle bounds = new Rectangle(0, 0, int.MaxValue, int.MaxValue);

                using (SafeDeviceHandle memoryHdc = Natives.CreateCompatibleDC(primaryHdc))
                    using (SafeGDIHandle fontHandle = new SafeGDIHandle(font.ToHfont(), true))
                        using (SafeGDIHandle dib = Natives.CreateDib(bounds, primaryHdc, memoryHdc))
                        {
                            Natives.SelectObject(memoryHdc, fontHandle);

                            System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
                            Natives.RECT bounds2 = new Natives.RECT(bounds);
                            Natives.RECT rect;
                            Natives.GetThemeTextExtent(renderer.Handle, memoryHdc, 0, 0, text, text.Length, (int)textFormat, ref bounds2, out rect);
                            return(new Size(rect.Right - rect.Left, rect.Bottom - rect.Top));
                        }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }