Beispiel #1
0
 internal static extern void DrawThemeBackground(
     IntPtr hTheme,
     IntPtr hdc,
     int iPartId,
     int iStateId,
     ref NativeStructs.RECT pRect,
     ref NativeStructs.RECT pClipRect);
 internal extern static int FillRect(
     IntPtr hDC,
     ref NativeStructs.RECT lprc,
     IntPtr hbr);
 internal static extern int DrawTextW(
     IntPtr hdc,
     string lpString,
     int nCount,
     ref NativeStructs.RECT lpRect,
     uint uFormat);
Beispiel #4
0
        // TODO: Use VisualStyles stuff in .NET 2.0 instead
        /// <summary>
        /// Draws a button in the appropriate system theme (Aero vs. Luna vs. Classic).
        /// </summary>
        /// <remarks>
        /// Note to implementors: This may be implemented as a simple thunk to ControlPaint.DrawButton().
        /// </remarks>
        public static void DrawThemedButton(
            Control hostControl,
            Graphics g,
            int x,
            int y,
            int width,
            int height,
            UI.ButtonState state)
        {
            IntPtr hTheme = OpenTheme(hostControl);

            if (hTheme != IntPtr.Zero)
            {
                NativeStructs.RECT rect = new NativeStructs.RECT();
                rect.left = x;
                rect.top = y;
                rect.right = x + width;
                rect.bottom = y + height;

                int iState;
                switch (state)
                {
                    case UI.ButtonState.Disabled:
                        iState = NativeConstants.PBS_DISABLED;
                        break;

                    case UI.ButtonState.Hot:
                        iState = NativeConstants.PBS_HOT;
                        break;

                    default:
                    case UI.ButtonState.Normal:
                        iState = NativeConstants.PBS_NORMAL;
                        break;

                    case UI.ButtonState.Pressed:
                        iState = NativeConstants.PBS_PRESSED;
                        break;
                }

                IntPtr hdc = g.GetHdc();

                SafeNativeMethods.DrawThemeBackground(
                    hTheme,
                    hdc,
                    NativeConstants.BP_PUSHBUTTON,
                    iState,
                    ref rect,
                    ref rect);

                g.ReleaseHdc(hdc);

                SafeNativeMethods.CloseThemeData(hTheme);
                hTheme = IntPtr.Zero;
            }
            else
            {
                System.Windows.Forms.ButtonState swfState;

                switch (state)
                {
                    case UI.ButtonState.Disabled:
                        swfState = System.Windows.Forms.ButtonState.Inactive;
                        break;

                    default:
                    case UI.ButtonState.Hot:
                    case UI.ButtonState.Normal:
                        swfState = System.Windows.Forms.ButtonState.Normal;
                        break;

                    case UI.ButtonState.Pressed:
                        swfState = System.Windows.Forms.ButtonState.Pushed;
                        break;
                }

                ControlPaint.DrawButton(g, x, y, width, height, swfState);
            }

            GC.KeepAlive(hostControl);
        }
Beispiel #5
0
        /// <summary>
        /// Measures text with the given graphics context, font, string, location, and anti-aliasing flag.
        /// </summary>
        /// <param name="g">The Graphics context to measure for.</param>
        /// <param name="font">The Font to measure with.</param>
        /// <param name="text">The string of text to measure.</param>
        /// <param name="antiAliasing">Whether the font should be rendered with anti-aliasing.</param>
        public static Size MeasureString(Graphics g, Font font, string text, bool antiAliasing, FontSmoothing fontSmoothing)
        {
            if (fontSmoothing == FontSmoothing.Smooth && antiAliasing)
            {
                PixelOffsetMode oldPOM = g.PixelOffsetMode;
                g.PixelOffsetMode = PixelOffsetMode.Half;

                TextRenderingHint oldTRH = g.TextRenderingHint;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;

                StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();
                format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;

                SizeF sf = g.MeasureString(text, font, new PointF(0, 0), format);
                sf.Height = font.GetHeight();

                g.PixelOffsetMode   = oldPOM;
                g.TextRenderingHint = oldTRH;
                return(Size.Ceiling(sf));
            }
            else if (fontSmoothing == FontSmoothing.Sharp || !antiAliasing)
            {
                IntPtr hdc        = IntPtr.Zero;
                IntPtr hFont      = IntPtr.Zero;
                IntPtr hOldObject = IntPtr.Zero;

                try
                {
                    hdc        = g.GetHdc();
                    hFont      = CreateFontObject(font, antiAliasing);
                    hOldObject = SafeNativeMethods.SelectObject(hdc, hFont);

                    NativeStructs.RECT rect = new NativeStructs.RECT();
                    rect.left   = 0;
                    rect.top    = 0;
                    rect.right  = rect.left;
                    rect.bottom = rect.top;

                    int result = SafeNativeMethods.DrawTextW(
                        hdc,
                        text,
                        text.Length,
                        ref rect,
                        NativeConstants.DT_CALCRECT |
                        NativeConstants.DT_LEFT |
                        NativeConstants.DT_NOCLIP |
                        NativeConstants.DT_NOPREFIX |
                        NativeConstants.DT_SINGLELINE |
                        NativeConstants.DT_TOP);

                    if (result == 0)
                    {
                        NativeMethods.ThrowOnWin32Error("DrawTextW returned 0");
                    }

                    return(new Size(rect.right - rect.left, rect.bottom - rect.top));
                }

                finally
                {
                    if (hOldObject != IntPtr.Zero)
                    {
                        SafeNativeMethods.SelectObject(hdc, hOldObject);
                        hOldObject = IntPtr.Zero;
                    }

                    if (hFont != IntPtr.Zero)
                    {
                        SafeNativeMethods.DeleteObject(hFont);
                        hFont = IntPtr.Zero;
                    }

                    if (hdc != IntPtr.Zero)
                    {
                        g.ReleaseHdc(hdc);
                        hdc = IntPtr.Zero;
                    }
                }
            }
            else
            {
                throw new InvalidEnumArgumentException("fontSmoothing = " + (int)fontSmoothing);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Renders text with the given graphics context, font, string, location, and anti-aliasing flag.
        /// </summary>
        /// <param name="g">The Graphics context to render to.</param>
        /// <param name="font">The Font to render with.</param>
        /// <param name="text">The string of text to draw.</param>
        /// <param name="pt">The offset of where to start drawing (upper-left of rendering rectangle).</param>
        /// <param name="antiAliasing">Whether the font should be rendered with anti-aliasing.</param>
        public static void DrawText(Graphics g, Font font, string text, Point pt, bool antiAliasing, FontSmoothing fontSmoothing)
        {
            if (fontSmoothing == FontSmoothing.Smooth && antiAliasing)
            {
                PixelOffsetMode oldPOM = g.PixelOffsetMode;
                g.PixelOffsetMode = PixelOffsetMode.Half;

                TextRenderingHint oldTRH = g.TextRenderingHint;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;

                StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();
                format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;

                g.DrawString(text, font, Brushes.Black, pt, format);

                g.PixelOffsetMode = oldPOM;
                g.TextRenderingHint = oldTRH;
            }
            else if (fontSmoothing == FontSmoothing.Sharp || !antiAliasing)
            {
                IntPtr hdc = IntPtr.Zero;
                IntPtr hFont = IntPtr.Zero;
                IntPtr hOldObject = IntPtr.Zero;

                try
                {
                    hdc = g.GetHdc();
                    hFont = CreateFontObject(font, antiAliasing);
                    hOldObject = SafeNativeMethods.SelectObject(hdc, hFont);

                    NativeStructs.RECT rect = new NativeStructs.RECT();
                    rect.left = pt.X;
                    rect.top = pt.Y;
                    rect.right = rect.left;
                    rect.bottom = rect.top;

                    int result = SafeNativeMethods.DrawTextW(
                        hdc,
                        text,
                        text.Length,
                        ref rect,
                        NativeConstants.DT_LEFT |
                            NativeConstants.DT_NOCLIP |
                            NativeConstants.DT_NOPREFIX |
                            NativeConstants.DT_SINGLELINE |
                            NativeConstants.DT_TOP);

                    if (result == 0)
                    {
                        NativeMethods.ThrowOnWin32Error("DrawTextW returned 0");
                    }
                }

                finally
                {
                    if (hOldObject != IntPtr.Zero)
                    {
                        SafeNativeMethods.SelectObject(hdc, hOldObject);
                        hOldObject = IntPtr.Zero;
                    }

                    if (hFont != IntPtr.Zero)
                    {
                        SafeNativeMethods.DeleteObject(hFont);
                        hFont = IntPtr.Zero;
                    }

                    if (hdc != IntPtr.Zero)
                    {
                        g.ReleaseHdc(hdc);
                        hdc = IntPtr.Zero;
                    }
                }
            }
            else
            {
                throw new InvalidEnumArgumentException("fontSmoothing = " + (int)fontSmoothing);
            }
        }
Beispiel #7
0
        // TODO: Use VisualStyles stuff in .NET 2.0 instead
        /// <summary>
        /// Draws a button in the appropriate system theme (Aero vs. Luna vs. Classic).
        /// </summary>
        /// <remarks>
        /// Note to implementors: This may be implemented as a simple thunk to ControlPaint.DrawButton().
        /// </remarks>
        public static void DrawThemedButton(
            Control hostControl,
            Graphics g,
            int x,
            int y,
            int width,
            int height,
            UI.ButtonState state)
        {
            IntPtr hTheme = OpenTheme(hostControl);

            if (hTheme != IntPtr.Zero)
            {
                NativeStructs.RECT rect = new NativeStructs.RECT();
                rect.left   = x;
                rect.top    = y;
                rect.right  = x + width;
                rect.bottom = y + height;

                int iState;
                switch (state)
                {
                case UI.ButtonState.Disabled:
                    iState = NativeConstants.PBS_DISABLED;
                    break;

                case UI.ButtonState.Hot:
                    iState = NativeConstants.PBS_HOT;
                    break;

                default:
                case UI.ButtonState.Normal:
                    iState = NativeConstants.PBS_NORMAL;
                    break;

                case UI.ButtonState.Pressed:
                    iState = NativeConstants.PBS_PRESSED;
                    break;
                }

                IntPtr hdc = g.GetHdc();

                SafeNativeMethods.DrawThemeBackground(
                    hTheme,
                    hdc,
                    NativeConstants.BP_PUSHBUTTON,
                    iState,
                    ref rect,
                    ref rect);

                g.ReleaseHdc(hdc);

                SafeNativeMethods.CloseThemeData(hTheme);
                hTheme = IntPtr.Zero;
            }
            else
            {
                System.Windows.Forms.ButtonState swfState;

                switch (state)
                {
                case UI.ButtonState.Disabled:
                    swfState = System.Windows.Forms.ButtonState.Inactive;
                    break;

                default:
                case UI.ButtonState.Hot:
                case UI.ButtonState.Normal:
                    swfState = System.Windows.Forms.ButtonState.Normal;
                    break;

                case UI.ButtonState.Pressed:
                    swfState = System.Windows.Forms.ButtonState.Pushed;
                    break;
                }

                ControlPaint.DrawButton(g, x, y, width, height, swfState);
            }

            GC.KeepAlive(hostControl);
        }