/// <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); } }
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); }
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); } }
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)); } } } } }
public void Draw(TreeNode node, IntPtr hdc, Rectangle rect) { if (!IsHandled(node) || node.Bounds.IsEmpty || node.IsEditing) { return; } CustomTreeView.CustomTreeView treeView = (CustomTreeView.CustomTreeView)node.TreeView; int offset = CalculateOffset(node); RichText.RichText text = (RichText.RichText)myNodes[node]; Rectangle contentRect = CalculateContentRectangle(node, hdc, offset); rect.X += offset; rect.Width -= offset; Rectangle fullRect = new Rectangle(contentRect.X - 1, node.Bounds.Top, contentRect.Width + 5, node.Bounds.Height); //g.SetClip(fullRect); TreeNode dropHiliteNode = treeView.DraggingOver ? treeView.DropHighlightedNode : null; bool hasFocus = node.TreeView.Focused; bool drawSelected = treeView.DraggingOver ? (node == dropHiliteNode) : node.IsSelected && hasFocus; bool drawNonfocusedSelection = node.IsSelected && !node.TreeView.HideSelection && (!hasFocus || treeView.DraggingOver); Color backColor; if (drawSelected) { backColor = SystemColors.Highlight; } else if (drawNonfocusedSelection) { backColor = SystemColors.Control; } else { backColor = treeView.BackColor; } IntPtr hBrush = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(backColor)); RECT lineRect = new RECT(fullRect.Left - 1, node.Bounds.Top, fullRect.Right + 1, node.Bounds.Bottom); Win32Declarations.FillRect(hdc, ref lineRect, hBrush); Win32Declarations.DeleteObject(hBrush); if (drawSelected) { text = (RichText.RichText)text.Clone(); text.SetColors(SystemColors.HighlightText, SystemColors.Highlight); } else if (drawNonfocusedSelection) { text = (RichText.RichText)text.Clone(); text.SetColors(SystemColors.WindowText, SystemColors.Control); } text.Draw(hdc, contentRect); if (hasFocus && treeView.SelectedNode == node && treeView.NeedFocusRect() && dropHiliteNode == null) { RECT rc = new RECT(fullRect.Left - 1, fullRect.Top, fullRect.Right + 1, fullRect.Bottom); Win32Declarations.DrawFocusRect(hdc, ref rc); } }