Treenode text different colored words - c#

I have a TreeView and each of it's Node.Text has two words.
The first and second words should have different colors. I'm already changing the color of the text with the DrawMode properties and the DrawNode event but I can't figure out how to split the Node.Text in two different colors. Someone pointed out I could use TextRenderer.MeasureText but I have no idead how/where to use it.
Someone has an idea ?
Code :
formload()
{
treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Color nodeColor = Color.Red;
if ((e.State & TreeNodeStates.Selected) != 0)
nodeColor = SystemColors.HighlightText;
TextRenderer.DrawText(e.Graphics,
e.Node.Text,
e.Node.NodeFont,
e.Bounds,
nodeColor,
Color.Empty,
TextFormatFlags.VerticalCenter);
}

Try this:
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
string[] texts = e.Node.Text.Split();
using (Font font = new Font(this.Font, FontStyle.Regular))
{
using (Brush brush = new SolidBrush(Color.Red))
{
e.Graphics.DrawString(texts[0], font, brush, e.Bounds.Left, e.Bounds.Top);
}
using (Brush brush = new SolidBrush(Color.Blue))
{
SizeF s = e.Graphics.MeasureString(texts[0], font);
e.Graphics.DrawString(texts[1], font, brush, e.Bounds.Left + (int)s.Width, e.Bounds.Top);
}
}
}
You must manage State of node to do appropriated actions.
UPDATE
Sorry, my mistake see updated version. There is no necessary to measure space size because it already contains in texts[0].

Related

Out of memory when chaging font dynamically in C#

I try to list all available fonts in ComboBox, but with font names printed in a given font family. It works, but it also crashes from time to time, with message "Out of memory", when I move mouse over opened ComboBox. Without changing fonts, it doesn't crash. Why the message occurs? And what to do to make it works?
Code:
private void tbrFontNameComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
Graphics g = e.Graphics;
Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
new SolidBrush(Color.Blue) : new SolidBrush(e.BackColor);
Brush tBrush = new SolidBrush(e.ForeColor);
g.FillRectangle(brush, e.Bounds);
Font font = new Font(tbrFontName.Items[e.Index].ToString(), 10);
e.Graphics.DrawString(tbrFontName.Items[e.Index].ToString(), font,
tBrush, e.Bounds, StringFormat.GenericDefault);
brush.Dispose();
tBrush.Dispose();
font.Dispose();
}
e.DrawFocusRectangle();
}

Winforms ComboBox Height different to ItemHeight

I am using a ComboBox in Text and DropDown mode (the default) and I want to have an ItemHeight of X (e.g. 40) but have the ComboBox's Height set to Y (e.g. 20).
The reason for this is that I am intending to use the ComboBox for a Quick Search feature in which the user keys in text and detailed results are rendered in the list items. Only one line of input is required.
Unfortunately, Winforms automatically locks the ComboBox's Height to the ItemHeight and I can see no way to change this.
How can I make the ComboBox's Height differ to the ItemHeight?
What you have to do is, first of all, change DrawMode from Normal to OwnerDrawVariable. Then you've got to handle 2 events: DrawItem and MeasureItem . They would be something like:
private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 40; //Change this to your desired item height
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ComboBox box = sender as ComboBox;
if (Object.ReferenceEquals(null, box))
return;
e.DrawBackground();
if (e.Index >= 0)
{
Graphics g = e.Graphics;
using (Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
? new SolidBrush(SystemColors.Highlight)
: new SolidBrush(e.BackColor))
{
using (Brush textBrush = new SolidBrush(e.ForeColor))
{
g.FillRectangle(brush, e.Bounds);
g.DrawString(box.Items[e.Index].ToString(),
e.Font,
textBrush,
e.Bounds,
StringFormat.GenericDefault);
}
}
}
e.DrawFocusRectangle();
}

Windows Forms small MenuItem hitbox

I am using MenuItem.DrawItem to change MenuItem's appearance based on the fact whether user either hovers or select it. Unfortunatelly the hitbox seems too small. Here's my drawing code.
private void DrawCustomMenuItem(object sender,
DrawItemEventArgs e)
{
MenuItem customItem = (MenuItem) sender;
System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
Font aFont = new Font("Arial", 10,
FontStyle.Italic, GraphicsUnit.Point);
SizeF stringSize = e.Graphics.MeasureString(
customItem.Text, aFont);
Pen p = new Pen(Color.Aqua, 2);
if ((e.State & DrawItemState.HotLight) == DrawItemState.HotLight
|| (e.State & DrawItemState.Selected) == DrawItemState.Selected)
p = new Pen(Color.Black, 2);
e.Graphics.DrawString(customItem.Text, aFont,
aBrush, e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawEllipse(p,
new Rectangle(e.Bounds.X, e.Bounds.Y,
(int) stringSize.Width,
(int) stringSize.Height));
}
It only changes color when mouse is nearby left edge of the item. It isn't surprising I guess. I have no code that changes hitbox's dimensions and it is using the default one and I am drawing outside of it. I really have no idea how to approach it. I thought I should use MenuItem.Size to achieve that. But this property doesn't exist.
EDIT:
Ok, I got it. It's all about MeasureItem event. But another problem came up. The MeasureItemEventArgs.Graphics.MeasureString() seems to return too big width for the text. Here are my current functions:
private void Mitem2OnMeasureItem(object sender, MeasureItemEventArgs e)
{
MenuItem item = sender as MenuItem;
if (item == null) return;
var size = e.Graphics.MeasureString(item.Text, f);
e.ItemHeight = (int) size.Height;
e.ItemWidth = (int) size.Width;
}
private void Mitem2OnDrawItem(object sender, DrawItemEventArgs e)
{
MenuItem item = sender as MenuItem;
if (item == null) return;
SizeF size = e.Graphics.MeasureString(item.Text, f);
e.Graphics.FillRectangle(
(e.State & DrawItemState.HotLight) == DrawItemState.HotLight ? Brushes.CornflowerBlue : Brushes.Aqua,
e.Bounds);
e.Graphics.DrawString(item.Text, f, Brushes.Black, e.Bounds.X, e.Bounds.Y);
e.DrawFocusRectangle();
}
This is how it looks like
To get a better measurement use the overload of MeasureString that takes StringFormat class:
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
var size = e.Graphics.MeasureString(
item.Text,
aFont,
new Point(1,1),
StringFormat.GenericTypographic
);
Notice the use of the TextRenderingHint with a value of AntiAlias that is set on the Graphics object.
You'll still have a slightly bigger bounding box but in my test the box is 5 pixels less wide with this code instead of your code.

Set TabPage Header Color

Greetings,
I have a tab control and I want to have 1 of the tabs have it's text color changed on a event.
I've found answers like C# - TabPage Color event
and C# Winform: How to set the Base Color of a TabControl (not the tabpage)
but using these sets all colors instead of one.
So I was hoping there is a way to implement this with the tab I wish to change as a method instead of a event?
Something like:
public void SetTabPageHeaderColor(TabPage page, Color color)
{
//Text Here
}
If you want to color the tabs, try the following code:
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
TabColors[page] = color;
tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//e.DrawBackground();
using (Brush br = new SolidBrush (TabColors[tabControl1.TabPages[e.Index]]))
{
e.Graphics.FillRectangle(br, e.Bounds);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
Rectangle rect = e.Bounds;
rect.Offset(0, 1);
rect.Inflate(0, -1);
e.Graphics.DrawRectangle(Pens.DarkGray, rect);
e.DrawFocusRectangle();
}
}
For WinForms users reading this - This ONLY works if you set your tab control's DrawMode to OwnerDrawFixed - the DrawItem event never fires if it's set to Normal.
To add to Fun Mun Pieng's answer which works beautifully on Horizontal tabs, if you were to use Vertical tabs (like I was) then you would need something like this:
private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
{
using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
{
// Color the Tab Header
e.Graphics.FillRectangle(br, e.Bounds);
// swap our height and width dimensions
var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);
// Rotate
e.Graphics.ResetTransform();
e.Graphics.RotateTransform(-90);
// Translate to move the rectangle to the correct position.
e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);
// Format String
var drawFormat = new System.Drawing.StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
// Draw Header Text
e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
}
}
I will echo the point that ROJO1969 made, if this is in WinForms - then you must set DrawMode to OwnerDrawFixed.
Special thanks goes out to this wonderful blog entry which described how to do a rotation of text on a form.
private void MainForm_Load(object sender, EventArgs e)
{
...
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
...
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
// Draw the background of the control for each item.
//e.DrawBackground();
if (e.Index == this.tabControl1.SelectedIndex)
{
Brush _BackBrush = new SolidBrush(tabControl1.TabPages[e.Index].BackColor);
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, (rect.X) + 4, rect.Y, (rect.Width) - 4, rect.Height);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
}
else
{
// 파스톤계 배경색 없앨려면 FromArgb 를 없애면 된다.
Brush _BackBrush = new SolidBrush(Color.FromArgb(50, tabControl1.TabPages[e.Index].BackColor));
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, rect.X, (rect.Y)-0, rect.Width, (rect.Height)+6);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + 5);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
If any one need to put color for tab header use try this. My tab name tabControl
tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += tabControl1_DrawItem;
declear this under main class
then,
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Color tabTextColor = Color.FromArgb(0x000001);
var color = Color.FromArgb(tabTextColor.R, tabTextColor.G, tabTextColor.B);
TextRenderer.DrawText(e.Graphics, tabControl.TabPages[e.Index].Text, e.Font, e.Bounds, color);
}
declare this function it will generate output
final out

Background color of a ListBox item (Windows Forms)

How can I set the background color of a specific item in a System.Windows.Forms.ListBox?
I would like to be able to set multiple ones if possible.
Thanks for the answer by Grad van Horck. It guided me in the correct direction.
To support text (not just background color), here is my fully working code:
//global brushes with ordinary/selected colors
private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White);
private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White);
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray);
//custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed
private void lbReports_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int index = e.Index;
if (index >= 0 && index < lbReports.Items.Count)
{
string text = lbReports.Items[index].ToString();
Graphics g = e.Graphics;
//background:
SolidBrush backgroundBrush;
if (selected)
backgroundBrush = reportsBackgroundBrushSelected;
else if ((index % 2) == 0)
backgroundBrush = reportsBackgroundBrush1;
else
backgroundBrush = reportsBackgroundBrush2;
g.FillRectangle(backgroundBrush, e.Bounds);
//text:
SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location);
}
e.DrawFocusRectangle();
}
The above adds to the given code and will show the proper text plus highlight the selected item.
Probably the only way to accomplish that is to draw the items yourself.
Set the DrawMode to OwnerDrawFixed and code something like this on the DrawItem event:
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
// Print text
e.DrawFocusRectangle();
}
The second option would be using a ListView, although they have an other way of implementations (not really data bound, but more flexible in way of columns).
// Set the background to a predefined colour
MyListBox.BackColor = Color.Red;
// OR: Set parts of a color.
MyListBox.BackColor.R = 255;
MyListBox.BackColor.G = 0;
MyListBox.BackColor.B = 0;
If what you mean by setting multiple background colors is setting a different background color for each item, this isn't possible with a ListBox, but it is with a ListView, with something like:
// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
public MainForm()
{
InitializeComponent();
this.listbox1.DrawItem += new DrawItemEventHandler(this.listbox1_DrawItem);
}
private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
var item = listbox1.Items[e.Index];
if(e.Index % 2 == 0)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds);
}
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush,e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
}

Categories

Resources