protected override void OnPaint(PaintEventArgs e) { // Background if (BackColor != Color.Transparent) { // If the parent is capable of providing a background brush, use it IBackgroundBrushProvider bbp = Parent as IBackgroundBrushProvider; Brush brush = bbp != null?bbp.GetBackgroundBrush(this) : new SolidBrush(BackColor); using (brush) e.Graphics.FillRectangle(brush, ClientRectangle); } // Foreground (icon) int imageIndex = GetCurrentImageIndex(); if (_imageList != null && imageIndex >= 0 && imageIndex < _imageList.Images.Count) { _imageList.Draw(e.Graphics, 0, 0, imageIndex); } }
/* * 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)); } } } } }