Exemple #1
0
        public Size MeasureText(Graphics g, string text, Font font, int maxWidth)
        {
            Size   result;
            IntPtr hdc = g.GetHdc();

            try
            {
                IntPtr hFont   = _fontCache.GetHFont(font);
                IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);
                SIZE   sz      = new SIZE();
                Win32Declarations.GetTextExtentPoint32(hdc, text, text.Length, ref sz);
                if (sz.cx < maxWidth)
                {
                    result = new Size(sz.cx, sz.cy);
                }
                else
                {
                    RECT rc     = new RECT(0, 0, maxWidth, Screen.PrimaryScreen.Bounds.Height);
                    int  height = Win32Declarations.DrawText(hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_WORDBREAK);
                    result = new Size(maxWidth, height);
                }

                Win32Declarations.SelectObject(hdc, oldFont);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Calculates the rectangle needed for rendering the text string.
        /// </summary>
        /// <param name="control">Control to which the text will be rendered.
        /// It's used for getting the device context and setting the initial text bounds
        /// (the latter is ignored in the single-line case).</param>
        /// <param name="text">Text to be rendered.</param>
        /// <param name="font">Font in which the text will be rendered.</param>
        /// <param name="bounds">Initial size that should be expanded to fit the text.</param>
        /// <param name="dtf">Additional parameters that control rendering of the text.</param>
        /// <returns>Size of the text's bounding rectangle.</returns>
        public static Size GetTextSize(Control control, string text, Font font, Size bounds, DrawTextFormatFlags dtf)
        {
            using (Graphics g = control.CreateGraphics())
            {
                IntPtr hdc = g.GetHdc();
                try
                {
                    IntPtr hFont   = _fontCache.GetHFont(font);
                    IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);
                    RECT   rc      = new RECT(0, 0, bounds.Width, bounds.Height);

                    Win32Declarations.DrawText(hdc, text, text.Length, ref rc,
                                               dtf | DrawTextFormatFlags.DT_CALCRECT);

                    int height = rc.bottom - rc.top;
                    //height += 1;
                    Size sz = new Size(rc.right - rc.left, height);

                    Win32Declarations.SelectObject(hdc, oldFont);

                    return(sz);
                }
                finally
                {
                    g.ReleaseHdc(hdc);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Renders some text to a graphics device.
        /// </summary>
        /// <param name="graphics">Graphics device to render the text into.</param>
        /// <param name="text">Text to render.</param>
        /// <param name="rect">Bounding rectangle for the text to fit into.</param>
        /// <param name="font">Font in which the text is rendered.</param>
        /// <param name="color">Text color.</param>
        /// <param name="dtf">Formatting flags.</param>
        public static void DrawText(Graphics graphics, string text, Rectangle rect, Font font, Color color, DrawTextFormatFlags dtf)
        {
            IntPtr hdc = graphics.GetHdc();

            try
            {
                // Font
                IntPtr hFont   = _fontCache.GetHFont(font);
                IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);

                // Bounding rectangle
                RECT rc = new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);

                // Color
                int            textColor = Win32Declarations.ColorToRGB(color);
                int            oldColor  = Win32Declarations.SetTextColor(hdc, textColor);
                BackgroundMode oldMode   = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                // Render the text
                Win32Declarations.DrawText(hdc, text, text.Length, ref rc, dtf);

                // Do deinit
                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SetTextColor(hdc, oldColor);
                Win32Declarations.SelectObject(hdc, oldFont);
            }
            finally
            {
                graphics.ReleaseHdc(hdc);
            }
        }
Exemple #4
0
        /// <summary><seealso cref="OptimalWidth"/>
        /// Calculates the preferred width on a per-tab basis.
        /// </summary>
        private void CalcPreferredWidth(TabBarTab tab)
        {
            IntPtr hdc = (IntPtr)0;

            try
            {
                if (IsHandleCreated)
                {
                    hdc = Win32Declarations.GetDC(Handle);
                }
                else
                {
                    hdc = Win32Declarations.GetDC((IntPtr)0);
                }

                IntPtr oldFont = Win32Declarations.SelectObject(hdc, _fontHandle);
                Win32Declarations.SelectObject(hdc, oldFont);
                SIZE sz = new SIZE();
                Win32Declarations.GetTextExtentPoint32(hdc, tab.Text, tab.Text.Length, ref sz);

                int prefWidth = sz.cx + 2 * 8;
                tab.PreferredWidth = Math.Max(50, prefWidth);
                tab.Width          = tab.PreferredWidth;
            }
            finally
            {
                if (hdc != (IntPtr)0)
                {
                    Win32Declarations.ReleaseDC(Handle, hdc);
                    hdc = (IntPtr)0;
                }
            }
        }
Exemple #5
0
        private void DrawTab(Graphics g, int index)
        {
            Rectangle rc = GetTabRect(index);

            if (rc.IsEmpty)
            {
                return;
            }

            GraphicsPath gp = BuildBorderPath(rc);

            using ( gp )
            {
                if (index == _activeTabIndex)
                {
                    g.FillPath(GUIControls.ColorScheme.GetBrush(_colorScheme, "ResourceTypeTabs.ActiveBackground",
                                                                rc, SystemBrushes.Control), gp);
                }
                else
                {
                    g.FillPath(GUIControls.ColorScheme.GetBrush(_colorScheme, "ResourceTypeTabs.InactiveBackground",
                                                                rc, SystemBrushes.Control), gp);

                    Rectangle rcGradient = new Rectangle(rc.Left, rc.Bottom - 6, rc.Width, 6);
                    g.FillRectangle(GUIControls.ColorScheme.GetBrush(_colorScheme, "ResourceTypeTabs.InactiveBackgroundBottom",
                                                                     rcGradient, SystemBrushes.Control), rcGradient);
                }

                g.DrawPath(GUIControls.ColorScheme.GetPen(_colorScheme,
                                                          (index == _activeTabIndex) ? "PaneCaption.Border" : "ResourceTypeTabs.Border",
                                                          Pens.Black), gp);
            }

            string tabText = ((TabBarTab)_tabs [index]).Text;
            IntPtr hdc     = g.GetHdc();

            try
            {
                Color clrText = (index == _activeTabIndex)
                    ? GUIControls.ColorScheme.GetColor(_colorScheme, "ResourceTypeTabs.ActiveText", Color.Black)
                    : GUIControls.ColorScheme.GetColor(_colorScheme, "ResourceTypeTabs.InactiveText", Color.Black);
                int            oldColor  = Win32Declarations.SetTextColor(hdc, ColorTranslator.ToWin32(clrText));
                IntPtr         oldFont   = Win32Declarations.SelectObject(hdc, _fontHandle);
                BackgroundMode oldBkMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                RECT rect = Win32Declarations.RectangleToRECT(rc);
                Win32Declarations.DrawText(hdc, tabText, tabText.Length, ref rect,
                                           DrawTextFormatFlags.DT_CENTER | DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_NOPREFIX |
                                           DrawTextFormatFlags.DT_END_ELLIPSIS | DrawTextFormatFlags.DT_SINGLELINE);

                Win32Declarations.SetBkMode(hdc, oldBkMode);
                Win32Declarations.SelectObject(hdc, oldFont);
                Win32Declarations.SetTextColor(hdc, oldColor);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }
Exemple #6
0
        protected override void OnPaint(PaintEventArgs e)
        {
            // Paint background
            if (BackColor != Color.Transparent)
            {
                using (Brush brush = new SolidBrush(BackColor))
                    e.Graphics.FillRectangle(brush, ClientRectangle);
            }

            base.OnPaint(e);

            // Paint foreground
            IntPtr hdc = e.Graphics.GetHdc();

            try
            {
                IntPtr hFont   = _fontCache.GetHFont(_underline ? _underlineFont : Font);
                IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);

                RECT rc        = new RECT(0, 0, Bounds.Width, Bounds.Height);
                int  textColor = Enabled
                    ? Win32Declarations.ColorToRGB(ForeColor)
                    : Win32Declarations.ColorToRGB(SystemColors.GrayText);

                int            oldColor = Win32Declarations.SetTextColor(hdc, textColor);
                BackgroundMode oldMode  = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                int postfixLeft = 0;
                if (_postfixText.Length > 0)
                {
                    Win32Declarations.DrawText(hdc, Text, Text.Length, ref rc,
                                               GetTextFormatFlags() | DrawTextFormatFlags.DT_CALCRECT);
                    postfixLeft = rc.right;
                }
                Win32Declarations.DrawText(hdc, Text, Text.Length, ref rc, GetTextFormatFlags());

                if (_postfixText.Length > 0)
                {
                    Win32Declarations.SetTextColor(hdc, ColorTranslator.ToWin32(Color.Black));
                    if (_underline)
                    {
                        Win32Declarations.SelectObject(hdc, _fontCache.GetHFont(Font));
                    }
                    rc.left  = postfixLeft;
                    rc.right = Bounds.Width;
                    Win32Declarations.DrawText(hdc, _postfixText, _postfixText.Length, ref rc, GetTextFormatFlags());
                }

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SetTextColor(hdc, oldColor);
                Win32Declarations.SelectObject(hdc, oldFont);
            }
            finally
            {
                e.Graphics.ReleaseHdc(hdc);
            }
        }
Exemple #7
0
        public static Bitmap ConvertIco2Bmp(Icon icon, Brush backBrush)
        {
            Bitmap bmp;

            // Declare the raw handles
            IntPtr hdcDisplay = IntPtr.Zero, hdcMem = IntPtr.Zero, hBitmap = IntPtr.Zero;

            try
            {
                // Get the display's Device Context so that the generated bitmaps were compatible
                hdcDisplay = Win32Declarations.CreateDC("DISPLAY", null, null, IntPtr.Zero);
                if (hdcDisplay == IntPtr.Zero)
                {
                    throw new Exception("Failed to get the display device context.");
                }

                // Create a pixel format compatible device context, and a bitmap to draw into using this context
                hdcMem = Win32Declarations.CreateCompatibleDC(hdcDisplay);
                if (hdcMem == IntPtr.Zero)
                {
                    throw new Exception("Could not cerate a compatible device context.");
                }
                hBitmap = Win32Declarations.CreateCompatibleBitmap(hdcDisplay, icon.Width, icon.Height);
                if (hBitmap == IntPtr.Zero)
                {
                    throw new Exception("Could not cerate a compatible offscreen bitmap.");
                }
                Win32Declarations.SelectObject(hdcMem, hBitmap);

                // Attach a GDI+ Device Context to the offscreen facility, and render the scene
                using (Graphics gm = Graphics.FromHdc(hdcMem))
                {
                    gm.FillRectangle(backBrush, 0, 0, icon.Width, icon.Height);
                    gm.DrawIcon(icon, 0, 0);
                }

                // Wrap the offscreen bitmap into a .NET bitmap in order to save it
                bmp = Image.FromHbitmap(hBitmap);
            }
            finally
            {
                // Cleanup the native resources in use
                if (hBitmap != IntPtr.Zero)
                {
                    Win32Declarations.DeleteObject(hBitmap);
                }
                if (hdcMem != IntPtr.Zero)
                {
                    Win32Declarations.DeleteDC(hdcMem);
                }
                if (hdcDisplay != IntPtr.Zero)
                {
                    Win32Declarations.DeleteDC(hdcDisplay);
                }
            }
            return(bmp);
        }
Exemple #8
0
        private Size MeasureText(IntPtr hdc, Font font, string text)
        {
            SIZE   sz      = new SIZE();
            IntPtr hFont   = _fontCache.GetHFont(font);
            IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont);

            Win32Declarations.GetTextExtentPoint32(hdc, text, text.Length, ref sz);
            Win32Declarations.SelectObject(hdc, oldFont);
            return(new Size(sz.cx, sz.cy));
        }
Exemple #9
0
        /// <summary>
        /// Draws the formatted string on a given graphics
        /// </summary>
        /// <param name="hdc">The device context to draw the string in.</param>
        /// <param name="parameters">Text formatting parameters</param>
        /// <exception cref="ArgumentNullException"><i>g</i> is null</exception>
        /// <exception cref="ArgumentNullException"><i>font</i> is null</exception>
        public int Draw(IntPtr hdc, Rectangle rect, RichTextParameters parameters)
        {
            Font hFont = GetParametrizedFont(parameters);
            RECT rc    = new RECT();

            rc.left   = rect.Left;
            rc.top    = rect.Top;
            rc.bottom = rect.Bottom;

            RectangleF bounds;

            IntPtr         oldFont    = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont));
            int            oldColor   = Win32Declarations.SetTextColor(hdc, Win32Declarations.ColorToRGB(myStyle.ForegroundColor));
            int            oldBkColor = Win32Declarations.SetBkColor(hdc, Win32Declarations.ColorToRGB(myStyle.BackgroundColor));
            BackgroundMode oldBkMode  = Win32Declarations.SetBkMode(hdc, myStyle.BackgroundColor == Color.Transparent ? BackgroundMode.TRANSPARENT : BackgroundMode.OPAQUE);

            Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_NOCLIP);
            if (rc.bottom > rect.Bottom)
            {
                rc.bottom = rect.Bottom;
            }
            if (rc.right > rect.Right)
            {
                rc.right = rect.Right;
            }
            bounds = new RectangleF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);

            Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_NOCLIP);

            Win32Declarations.SetBkMode(hdc, oldBkMode);
            Win32Declarations.SetBkColor(hdc, oldBkColor);
            Win32Declarations.SetTextColor(hdc, oldColor);
            Win32Declarations.SelectObject(hdc, oldFont);

            switch (myStyle.Effect)
            {
            case TextStyle.EffectStyle.StrikeOut:
                StrikeOut(hdc, bounds);
                break;

            case TextStyle.EffectStyle.StraightUnderline:
                UnderlineStraight(hdc, bounds);
                break;

            case TextStyle.EffectStyle.WeavyUnderline:
                UnderlineWeavy(hdc, bounds);
                break;
            }

            return(rc.right - rc.left);
        }
Exemple #10
0
        /// <summary>
        /// Gets size of the string in the given graphics
        /// </summary>
        /// <param name="hdc">The device context to calculate size in</param>
        /// <param name="parameters">Formatting parameters to use</param>
        /// <returns>Size of the string when drawn in a given graphics</returns>
        /// <exception cref="ArgumentNullException"><i>g</i> is null.</exception>
        public SizeF GetSize(IntPtr hdc, RichTextParameters parameters)
        {
            Font hFont = GetParametrizedFont(parameters);
            RECT rc    = new RECT();

            rc.left = 0;
            rc.top  = 0;

            IntPtr oldFont = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont));

            Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX);
            Win32Declarations.SelectObject(hdc, oldFont);

            return(new SizeF(rc.right - rc.left, rc.bottom - rc.top));
        }
Exemple #11
0
 protected override void WndProc(ref Message m)
 {
     if (m.Msg == Win32Declarations.WM_NCPAINT && BorderStyle == BorderStyle.FixedSingle)
     {
         IntPtr hdc      = Win32Declarations.GetWindowDC(Handle);
         IntPtr brush    = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(_borderColor));
         IntPtr oldBrush = Win32Declarations.SelectObject(hdc, brush);
         RECT   rect     = new RECT(0, 0, Width, Height);
         Win32Declarations.FrameRect(hdc, ref rect, brush);
         Win32Declarations.SelectObject(hdc, oldBrush);
         Win32Declarations.DeleteObject(brush);
         Win32Declarations.ReleaseDC(Handle, hdc);
         m.Result = IntPtr.Zero;
     }
     else
     {
         base.WndProc(ref m);
     }
 }
Exemple #12
0
        public int GetSymbolByOffset(int x, RichTextParameters parameters, IntPtr hdc)
        {
            if (x < 0)
            {
                return(-1);
            }

            Font hFont = GetParametrizedFont(parameters);
            RECT rc    = new RECT();

            rc.left = 0;
            rc.top  = 0;

            int currentX = 0;

            IntPtr oldFont = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont));

            try
            {
                for (int i = 0; i < PartText.Length; i++)
                {
                    Win32Declarations.DrawText(hdc, PartText.Substring(i, 1), 1, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_NOCLIP);
                    currentX += rc.right - rc.left;

                    if (currentX > x)
                    {
                        return(i);
                    }
                }

                return(-1);
            }
            finally
            {
                Win32Declarations.SelectObject(hdc, oldFont);
            }
        }
Exemple #13
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (_customDrawFailed)
            {
                return;
            }

            try
            {
                int left   = ClientRectangle.Left;
                int top    = ClientRectangle.Top;
                int right  = ClientRectangle.Right;
                int bottom = ClientRectangle.Bottom;

                bool drawPressed = _pressed || (_pressing && _hot);

                string backBrushId = drawPressed
                    ? "Sidebar.Button.BackgroundPressed"
                    : "Sidebar.Button.Background";

                Brush backBrush = GUIControls.ColorScheme.GetBrush(_colorScheme, backBrushId, ClientRectangle,
                                                                   SystemBrushes.Control);
                e.Graphics.FillPath(backBrush, _borderPath);

                Pen borderPen = GUIControls.ColorScheme.GetPen(_colorScheme, "Sidebar.Button.Border",
                                                               SystemPens.ControlDark);
                e.Graphics.DrawPath(borderPen, _borderPath);

                if (_hot)
                {
                    Pen hotPen = GUIControls.ColorScheme.GetPen(_colorScheme, "Sidebar.Button.BorderHot",
                                                                Pens.Blue);
                    e.Graphics.DrawLine(hotPen, left + 1, top + 2, left + 1, bottom - 3);
                    e.Graphics.DrawLine(hotPen, left + 2, top + 1, right - 3, top + 1);
                    e.Graphics.DrawLine(hotPen, right - 2, top + 2, right - 2, bottom - 3);
                    e.Graphics.DrawLine(hotPen, left + 2, bottom - 2, right - 3, bottom - 2);
                    e.Graphics.DrawRectangle(hotPen, left + 2, top + 2, ClientRectangle.Width - 5, ClientRectangle.Height - 5);
                }
                else
                {
                    string leftPenId  = drawPressed ? "Sidebar.Button.BorderDarkPressed" : "Sidebar.Button.BorderLight";
                    string rightPenId = drawPressed ? "Sidebar.Button.BorderLight" : "Sidebar.Button.BorderDark";

                    Pen leftPen = GUIControls.ColorScheme.GetPen(_colorScheme, leftPenId, Pens.White);
                    e.Graphics.DrawLine(leftPen, left + 1, top + 2, left + 1, bottom - 3);
                    e.Graphics.DrawLine(leftPen, left + 2, top + 1, right - 3, top + 1);

                    Pen rightPen = GUIControls.ColorScheme.GetPen(_colorScheme, rightPenId, SystemPens.ControlDark);
                    e.Graphics.DrawLine(rightPen, right - 2, top + 2, right - 2, bottom - 3);
                    e.Graphics.DrawLine(rightPen, left + 2, bottom - 2, right - 3, bottom - 2);
                }

                IntPtr         hdc     = e.Graphics.GetHdc();
                IntPtr         oldFont = Win32Declarations.SelectObject(hdc, _fontHandle);
                BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                int delta          = drawPressed ? 1 : 0;
                int topSpace       = 6;
                int iconAreaHeight = 20;

                int  maxTextHeight = ClientRectangle.Height - topSpace - iconAreaHeight - 2;
                SIZE sz            = new SIZE();
                Win32Declarations.GetTextExtentPoint32(hdc, Text, Text.Length, ref sz);

                string textToDraw = Text;
                if (sz.cx > maxTextHeight)
                {
                    // calculate how many characters fit if we leave space for the ellipsis
                    SIZE szClip = new SIZE();
                    int  charsFit;
                    Win32Declarations.GetTextExtentExPoint(hdc, Text, Text.Length, maxTextHeight - 10,
                                                           out charsFit, IntPtr.Zero, out szClip);
                    if (charsFit > 0)
                    {
                        textToDraw = Text.Substring(0, charsFit) + "...";
                    }
                    else
                    {
                        textToDraw = Text.Substring(0, 1);
                    }

                    Win32Declarations.GetTextExtentPoint32(hdc, textToDraw, textToDraw.Length, ref sz);
                }

                if (_angle == 270)
                {
                    Win32Declarations.TextOut(hdc, ClientRectangle.Right - 2 + delta, ClientRectangle.Top + topSpace + delta,
                                              textToDraw, textToDraw.Length);
                }
                else if (_angle == 90)
                {
                    Win32Declarations.TextOut(hdc, ClientRectangle.Left + 2 + delta, ClientRectangle.Top + topSpace + sz.cx + delta,
                                              textToDraw, textToDraw.Length);
                }

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SelectObject(hdc, oldFont);
                e.Graphics.ReleaseHdc(hdc);

                if (_icon != null)
                {
                    int iconX = (ClientRectangle.Width - 16) / 2;
                    e.Graphics.DrawIcon(_icon, ClientRectangle.Left + iconX, ClientRectangle.Bottom - iconAreaHeight + delta);
                }
            }
            catch (Exception ex)
            {
                Core.ReportBackgroundException(ex);
                _customDrawFailed = true;
            }
        }
Exemple #14
0
        public int DrawText(Graphics g, string text, Font font, Color color, Rectangle rc, StringFormat format)
        {
            int        height;
            RectangleF rcClip = g.ClipBounds;
            RECT       rect   = new RECT(rc.Left, rc.Top, rc.Right, rc.Bottom);
            IntPtr     hdc    = g.GetHdc();

            try
            {
                IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0);
                if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1)
                {
                    Win32Declarations.DeleteObject(clipRgn);
                    clipRgn = IntPtr.Zero;
                }
                Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom);

                IntPtr         hFont     = _fontCache.GetHFont(font);
                IntPtr         oldFont   = Win32Declarations.SelectObject(hdc, hFont);
                int            textColor = Win32Declarations.ColorToRGB(color);
                int            oldColor  = Win32Declarations.SetTextColor(hdc, textColor);
                BackgroundMode oldMode   = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                DrawTextFormatFlags flags = 0;
                if ((format.FormatFlags & StringFormatFlags.NoWrap) != 0)
                {
                    flags |= DrawTextFormatFlags.DT_SINGLELINE;
                }
                else
                {
                    flags |= DrawTextFormatFlags.DT_WORDBREAK;
                }
                if (format.Alignment == StringAlignment.Center)
                {
                    flags |= DrawTextFormatFlags.DT_CENTER;
                }
                else if (format.Alignment == StringAlignment.Far)
                {
                    flags |= DrawTextFormatFlags.DT_RIGHT;
                }

                if (format.LineAlignment == StringAlignment.Center)
                {
                    flags |= DrawTextFormatFlags.DT_VCENTER;
                }
                if (format.Trimming == StringTrimming.EllipsisCharacter)
                {
                    flags |= DrawTextFormatFlags.DT_END_ELLIPSIS;
                }
                if (format.HotkeyPrefix == HotkeyPrefix.None)
                {
                    flags |= DrawTextFormatFlags.DT_NOPREFIX;
                }

                height = Win32Declarations.DrawText(hdc, text, text.Length, ref rect, flags);

                Win32Declarations.SelectClipRgn(hdc, clipRgn);
                Win32Declarations.DeleteObject(clipRgn);

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SetTextColor(hdc, oldColor);
                Win32Declarations.SelectObject(hdc, oldFont);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
            return(height);
        }
Exemple #15
0
        /*
         * private int GetWorkspaceTextWidth( Graphics g )
         * {
         *      IntPtr hdc = g.GetHdc();
         *      IntPtr oldFont = Win32Declarations.SelectObject( hdc, _fontHandle );
         *      SIZE sz = new SIZE();
         *      string	text = WorkspaceName;
         *      Win32Declarations.GetTextExtentPoint32( hdc, text, text.Length, ref sz );
         *      Win32Declarations.SelectObject( hdc, oldFont );
         *      g.ReleaseHdc( hdc );
         *      return sz.cx;
         * }
         */

        /// <summary>
        /// Draws the workspace button.
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            // Background (try to retrieve a brush from the parent)
            IBackgroundBrushProvider bbp = Parent as IBackgroundBrushProvider;
            Brush brushBack = bbp != null?bbp.GetBackgroundBrush(this) : new SolidBrush(BackColor);

            using (brushBack)
                e.Graphics.FillRectangle(brushBack, ClientRectangle);

            // Do not try to paint a killed workspace
            if ((_workspace != null) && (_workspace.IsDeleted))
            {
                return;
            }

            if (Core.State == CoreState.ShuttingDown)
            {
                return;
            }

            Color colorLink = Color.Blue;             // Color of the links (unread counter text and underlining)

            // Foreground
            if (Active)
            {             // The button represents an active (currently selected) workspace
                /*
                 * using( GraphicsPath gp = GdiPlusTools.BuildRoundRectPath( ClientRectangle ) )
                 * {
                 *      e.Graphics.FillPath( ColorScheme.GetBrush( _colorScheme,
                 *                                                 "WorkspaceBar.ActiveButtonBackground", ClientRectangle, SystemBrushes.Control ), gp );
                 * }
                 *
                 * Rectangle innerRect = ClientRectangle;
                 * innerRect.Inflate( -1, -1 );
                 * Pen borderPen = new Pen( ColorScheme.GetColor( _colorScheme,
                 *                                             "WorkspaceBar.ActiveButtonBorder", SystemColors.Control ), 2.0f );
                 * using( borderPen )
                 * {
                 *      e.Graphics.DrawRectangle( borderPen, innerRect );
                 * }
                 *
                 * using( Brush cornerBrush = new SolidBrush( BackColor ) )
                 * {
                 *      e.Graphics.FillRectangle( cornerBrush, innerRect.Left - 1, innerRect.Top - 1, 1, 1 );
                 *      e.Graphics.FillRectangle( cornerBrush, innerRect.Right, innerRect.Top - 1, 1, 1 );
                 *      e.Graphics.FillRectangle( cornerBrush, innerRect.Left - 1, innerRect.Bottom, 1, 1 );
                 *      e.Graphics.FillRectangle( cornerBrush, innerRect.Right, innerRect.Bottom, 1, 1 );
                 * }
                 */
            }
            else
            {             // The button's workspace is not active
                if (_hot)
                {         // The button is hovered, display UI cues
                    /*
                     * e.Graphics.DrawRectangle( ColorScheme.GetPen( _colorScheme,
                     *                                            "WorkspaceBar.ActiveButtonBorder", SystemPens.Control ),
                     *                        ClientRectangle.Left, ClientRectangle.Top,
                     *                        ClientRectangle.Right - 4, ClientRectangle.Bottom - 1 );
                     */
                }
            }

            /*
             *
             * Rectangle	client = ClientRectangle;
             * IntPtr	hdc = e.Graphics.GetHdc();
             * ArrayList	arIconIndices = new ArrayList();	// Indices of the icons in the resource image list that should be drawn on the control
             * ArrayList	arIconPositions = new ArrayList();	// X-coordinates of those icons
             * try
             * {
             *      IntPtr hOldFont = Win32Declarations.SelectObject( hdc, _fontHandle );
             *      int rgbTextColor = Win32Declarations.ColorToRGB( Enabled ? ForeColor : SystemColors.GrayText ); // Title color
             *      int rgbOldColor = Win32Declarations.SetTextColor( hdc, rgbTextColor );
             *      BackgroundMode oldMode = Win32Declarations.SetBkMode( hdc, Active ? BackgroundMode.OPAQUE : BackgroundMode.TRANSPARENT );
             *      int	rgbOldBackColor = Win32Declarations.SetBkColor( hdc, Win32Declarations.ColorToRGB(SystemColors.Control) );
             *      int	nCurPos = client.Left;	// Current position at which the next label will be placed
             *
             *      // Workspace title text
             *      string text = WorkspaceName; // Take the workspace name
             *
             *      // Measure the title label size
             *      RECT rc = new RECT( client.Left, client.Top, client.Right, client.Bottom );
             *      Win32Declarations.DrawText( hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_CALCRECT );
             *      Size sizeTitle = new Size( rc.right - rc.left, rc.bottom - rc.top );
             *
             *      // Draw the title
             *      Rectangle	rcText = new Rectangle(new Point(nCurPos, client.Top + (client.Height - sizeTitle.Height) / 2), sizeTitle);
             *      rc = new RECT(rcText.Left,  rcText.Top,  rcText.Right, rcText.Bottom);
             *      Win32Declarations.DrawText( hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE );
             *      nCurPos = rcText.Right;
             *      nCurPos += _nGap;
             *
             *      //////////////////////////////
             *      // Paint the Unread Counters
             *      // We cannot paint the icons now because HDC is in use for text, save their types and positions for later use
             *      Win32Declarations.SetTextColor( hdc, Win32Declarations.ColorToRGB( Color.Blue ));
             *      lock( _unreadCounters )
             *      {
             *              foreach( DictionaryEntry de in _unreadCounters )
             *              {
             *                      string resType = (string) de.Key;
             *                      int counter = (int) de.Value;
             *
             *                      // Icon (cannot draw now as HDC is in use)
             *                      arIconIndices.Add( Core.ResourceIconManager.GetDefaultIconIndex( resType ) );
             *                      arIconPositions.Add( nCurPos );
             *                      nCurPos += 16; // Icon width
             *                      nCurPos += _nIconTextGap; // Gap between the icon and its text
             *
             *                      // Item text
             *                      string sCounter = counter.ToString();
             *
             *                      // Measure
             *                      rc = new RECT( nCurPos, client.Top, client.Right, client.Bottom );
             *                      Win32Declarations.DrawText( hdc, sCounter, sCounter.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_CALCRECT );
             *                      Size sizeCounter = new Size( rc.right - rc.left, rc.bottom - rc.top );
             *
             *                      // Draw
             *                      Rectangle rcCounter = new Rectangle( new Point( nCurPos, client.Top + (client.Height - sizeCounter.Height) / 2 ), sizeCounter );
             *                      rc = new RECT( rcCounter.Left, rcCounter.Top, rcCounter.Right, rcCounter.Bottom );
             *                      Win32Declarations.DrawText( hdc, sCounter, sCounter.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE );
             *                      nCurPos = rcCounter.Right;
             *                      nCurPos += _nGap;
             *              }
             *
             *              Win32Declarations.SetBkMode( hdc, oldMode );
             *              Win32Declarations.SetTextColor( hdc, rgbOldColor );
             *              Win32Declarations.SetBkColor( hdc, rgbOldBackColor );
             *              Win32Declarations.SelectObject( hdc, hOldFont );
             *      }
             * }
             * finally
             * {
             *      e.Graphics.ReleaseHdc( hdc );
             * }
             *
             * ///////////////////
             * // Draw the icons
             * if(arIconIndices.Count == arIconPositions.Count)
             * {
             *      int	nIconTop = client.Top + (client.Height - 16) / 2;
             *      for(int a = 0; a < arIconIndices.Count; a++)
             *              Core.ResourceIconManager.ImageList.Draw( e.Graphics, (int) arIconPositions[a], nIconTop, 16, 16, (int) arIconIndices[a] );
             * }
             * else
             *      Trace.WriteLine( "Icon indices and icon positions lists are desynchronized.", "[WB]" );
             */

            IntPtr hdc = e.Graphics.GetHdc();

            try
            {
                IntPtr         hOldFont        = Win32Declarations.SelectObject(hdc, _hFontCounter);
                int            rgbTextColor    = Win32Declarations.ColorToRGB(Enabled ? ForeColor : SystemColors.GrayText);   // Title color
                int            rgbOldColor     = Win32Declarations.SetTextColor(hdc, rgbTextColor);
                BackgroundMode oldMode         = Win32Declarations.SetBkMode(hdc, Active ? BackgroundMode.OPAQUE : BackgroundMode.TRANSPARENT);
                int            rgbOldBackColor = Win32Declarations.SetBkColor(hdc, Win32Declarations.ColorToRGB(SystemColors.Control));

                // Workspace title text
                string text = WorkspaceName;                 // Take the workspace name
                RECT   rc   = RECTFromRectangle(_rectTitle);
                Win32Declarations.DrawText(hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE);

                ///////////////////////////////////
                // Paint the Unread Counters text
                // (icons cannot be painted while the HDC is in use, wait for it to be released)
                Win32Declarations.SetTextColor(hdc, Win32Declarations.ColorToRGB(colorLink));
                if (_drawings != null)
                {
                    foreach (Drawing drawing in _drawings)
                    {
                        if (drawing.What != Drawing.Type.CounterLabel)
                        {
                            continue;
                        }

                        // Update the text by taking it from the unread counters map
                        object value = _unreadCounters[drawing.ResType];
                        if (value != null)
                        {
                            drawing.Text = value.ToString();
                        }

                        // Paint the text
                        rc = RECTFromRectangle(drawing.Bounds);
                        Win32Declarations.DrawText(hdc, drawing.Text, drawing.Text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE);
                    }
                }

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SetTextColor(hdc, rgbOldColor);
                Win32Declarations.SetBkColor(hdc, rgbOldBackColor);
                Win32Declarations.SelectObject(hdc, hOldFont);
            }
            finally
            {
                e.Graphics.ReleaseHdc(hdc);
            }

            //////////////////////////
            // Unread Counters Icons
            // and underline the hovered text
            if (_drawings != null)
            {
                using (Brush brushUnderline = new SolidBrush(colorLink))
                {
                    foreach (Drawing drawing in _drawings)
                    {
                        // Draw the icon
                        if (drawing.What == Drawing.Type.CounterIcon)
                        {
                            Core.ResourceIconManager.ImageList.Draw(e.Graphics, drawing.Bounds.Left, drawing.Bounds.Top, drawing.Bounds.Width, drawing.Bounds.Height, drawing.IconIndex);
                        }
                        // Draw underlining
                        else if ((drawing.What == Drawing.Type.CounterLabel) && (drawing.ResType == _sUnreadResTypeHovered))
                        {
                            e.Graphics.FillRectangle(brushUnderline, new Rectangle(drawing.Bounds.Left, drawing.Bounds.Bottom - 1, drawing.Bounds.Width, 1));
                        }
                    }
                }
            }
        }
Exemple #16
0
        /// <summary>
        /// Measures the optimal button width.
        /// </summary>
        private int CalcButtonWidth()
        {
            if ((IsDisposed) || (Core.State == CoreState.ShuttingDown))
            {
                return(0);
            }

            Rectangle client   = ClientRectangle;
            int       nCurPos  = client.Left;      // Current position at which the next label will be placed
            ArrayList drawings = new ArrayList();

            using (Graphics g = CreateGraphics())
            {
                IntPtr hdc = g.GetHdc();
                try
                {
                    IntPtr hOldFont = Win32Declarations.SelectObject(hdc, _hFontCounter);                     // Select the font that will be used for drawing

                    // Workspace title text
                    string text = WorkspaceName;                     // Take the workspace name

                    // Measure the title label size
                    RECT rc = new RECT(client.Left, client.Top, client.Right, client.Bottom);
                    Win32Declarations.DrawText(hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_CALCRECT);
                    Size sizeTitle = new Size(rc.right - rc.left, rc.bottom - rc.top);
                    _rectTitle = new Rectangle(new Point(nCurPos, client.Top + (client.Height - sizeTitle.Height) / 2), sizeTitle);
                    nCurPos    = _rectTitle.Right;
                    drawings.Add(new Drawing(_rectTitle, text));                     // Add the title to the drawings

                    //////////////////////////////
                    // Measure the Unread Counters
                    Size sizeIcon = Core.ResourceIconManager.ImageList.ImageSize;
                    int  nIconTop = client.Top + (client.Height - sizeIcon.Height) / 2;
                    lock (_unreadCounters)
                    {
                        foreach (DictionaryEntry de in _unreadCounters)
                        {
                            string resType  = (string)de.Key;
                            string sCounter = de.Value.ToString();

                            // Gap before this counter
                            nCurPos += _nGap;

                            // Icon
                            drawings.Add(new Drawing(resType, new Rectangle(new Point(nCurPos, nIconTop), sizeIcon), Core.ResourceIconManager.GetDefaultIconIndex(resType)));
                            nCurPos += sizeIcon.Width;                            // Icon width
                            nCurPos += _nIconTextGap;                             // Gap between the icon and its text

                            // Label
                            rc = new RECT(nCurPos, client.Top, client.Right, client.Bottom);
                            Win32Declarations.DrawText(hdc, sCounter, sCounter.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_CALCRECT);
                            Size      sizeCounter = new Size(rc.right - rc.left, rc.bottom - rc.top);
                            Rectangle rcCounter   = new Rectangle(new Point(nCurPos, client.Top + (client.Height - sizeCounter.Height) / 2), sizeCounter);
                            drawings.Add(new Drawing(resType, rcCounter, sCounter));
                            nCurPos = rcCounter.Right;
                        }
                        Win32Declarations.SelectObject(hdc, hOldFont);
                    }
                }
                finally
                {
                    g.ReleaseHdc(hdc);
                }
            }

            // Store the updated drawing data
            _drawings = (Drawing[])drawings.ToArray(typeof(Drawing));

            // Return the calculated width
            return(nCurPos);
        }