/// <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); } } }
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); }
/// <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); } }
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); } }
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); } }
protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); e.DrawBackground(); object obj = (e.Index >= 0) ? Items [e.Index] : null; IResource res = obj as IResource; int textOffset = 0; // don't apply indent when drawing the item in the combo box itself - // only for drop-down items if (e.Bounds.X != 3 && e.Bounds.Y != 3) { if (obj != null && _resourceIndents.Contains(obj)) { textOffset = (int)_resourceIndents [obj]; } } string text = null; if (res != null) { int imageIndex = Core.ResourceIconManager.GetIconIndex(res); if (imageIndex >= 0 && imageIndex < Core.ResourceIconManager.ImageList.Images.Count) { Core.ResourceIconManager.ImageList.Draw(e.Graphics, e.Bounds.X + textOffset, e.Bounds.Y, imageIndex); textOffset += 18; } text = res.DisplayName; } else if (obj != null) { text = obj.ToString(); textOffset += 2; } else { text = ""; } IntPtr hdc = e.Graphics.GetHdc(); try { RECT rc = new RECT(e.Bounds.Left + textOffset, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom); int oldColor = Win32Declarations.SetTextColor(hdc, Win32Declarations.ColorToRGB(e.ForeColor)); BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT); Win32Declarations.DrawText(hdc, text, text.Length, ref rc, DrawTextFormatFlags.DT_NOPREFIX); Win32Declarations.SetBkMode(hdc, oldMode); Win32Declarations.SetTextColor(hdc, oldColor); } finally { e.Graphics.ReleaseHdc(hdc); } }
/// <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); }
/// <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)); }
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); } }
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); }
/* * 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)); } } } } }
/// <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); }