I created my custom Tooltip, but what I need is that it will be with the same style as system draws it when you do not switch on the OwnerDraw flag.
How can I create custom tooltip which looks exactly like the "original" one?
ttSessionInfo.ToolTipTitle = UiTranslator.Instance.GetLabel(UiLabels.DC_DSE_Session);
var toolTipSessionsText = sessions.Aggregate(
new StringBuilder(),
(p_strBuilder, p_session) => p_strBuilder.AppendLine(string.Format("{0}: {1}", p_session.SessionName,
p_session.IsConnected ? connectedText : disconnectedText))).ToString();
ttSessionInfo.SetToolTip(LiveUpdatePb, toolTipSessionsText);
Result is:
I need the same tooltip exactly to show on another control, but to paint ,lets say, the second row "Alex Session: Connected" with red color.
I added a sample that re-implements ToolTip class and codes to use it.
class:
class CustomToolTip : ToolTip
{
public CustomToolTip()
{
this.OwnerDraw = true;
this.Popup += new PopupEventHandler(this.OnPopup);
this.Draw += new DrawToolTipEventHandler(this.OnDraw);
}
string m_EndSpecialText;
Color m_EndSpecialTextColor =Color.Red;
public Color EndSpecialTextColor
{
get { return m_EndSpecialTextColor; }
set { m_EndSpecialTextColor = value; }
}
public string EndSpecialText
{
get { return m_EndSpecialText; }
set { m_EndSpecialText = value; }
}
private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
{
e.ToolTipSize = new Size(200, 100);
}
private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
{
Graphics g = e.Graphics;
LinearGradientBrush b = new LinearGradientBrush(e.Bounds,
Color.GreenYellow, Color.MintCream, 45f);
g.FillRectangle(b, e.Bounds);
g.DrawRectangle(new Pen(Brushes.Red, 1), new Rectangle(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width - 1, e.Bounds.Height - 1));
//g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Silver,
// new PointF(e.Bounds.X + 6, e.Bounds.Y + 6)); // shadow layer
g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Black,
new PointF(e.Bounds.X + 5, e.Bounds.Y + 5)); // top layer
SolidBrush brush = new SolidBrush(EndSpecialTextColor);
g.DrawString(EndSpecialText, new Font(e.Font, FontStyle.Bold), brush,
new PointF(e.Bounds.X + 5, e.Bounds.Bottom - 15)); // top layer
brush.Dispose();
b.Dispose();
}
}
following the use of above class
private void button1_Click(object sender, EventArgs e)
{
CustomToolTip toolTip1 = new CustomToolTip();
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(button1, "Click me to execute.");
toolTip1.EndSpecialText = "Hello I am special";
}
Related
The basic TabControl doesn't fit my needs design wise and needed a redesign.
The things that i needed were:
No borders
Gray background with white text for the selected tab
Black background with gray text for not selected tab
No doted line inside the selected tab (this one is a bit low priority)
I managed to fix some of these issues by creating a new class except for the last two. Seeing my limited experience (first time doing something like this) I was hoping somebody here could point me in the right direction.
Below an illustration of what it currently is and how I would want it to look. And of course the code of the class I used to do this.
What it looks like:
What i want it to look like:
The code:
class CustomTabControl : TabControl
{
public CustomTabControl(): base()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == this.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(64,64,64), Color.FromArgb(0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.White;
}
else
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(0,0,0), Color.FromArgb(0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Gray;
}
string tabName = this.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle(recTab.X +3 , recTab.Y+3, recTab.Width, recTab.Height );
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
Rectangle r = this.GetTabRect(this.TabPages.Count - 1);
RectangleF tf = new RectangleF(r.X + r.Width, r.Y - 2, this.Width - (r.X + r.Width) + 0, r.Height + 4);
Brush b = Brushes.Black;
e.Graphics.FillRectangle(b, tf);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0005)
{
int Width = unchecked((short)m.LParam);
int Height = unchecked((short)((uint)m.LParam >> 16));
Region = new Region(new Rectangle(4, 2, Width - 8, Height - 6));
}
base.WndProc(ref m);
}
}
(And suggestions on improving this class are welcome.)
I'm using a theme with a custom TabControl control.
The control draws text for each tab using the DrawString method like so:
using (SolidBrush B = new SolidBrush(FC))
{
G.DrawString(TabPages[i].Text, Theme.GlobalFont(FontStyle.Regular, 10), B, new Point(TabRect.X + 50, TabRect.Y + 12));
}
After changing the text to Hebrew, and changing the control's RightToLeftLayout to true, the text is flipped like so:
I tried to search online and found out that you can use Transform to modify the text, however I believe this is not the case since it should be a simple task. Here's the full class for the control:
class FirefoxMainTabControl : TabControl
{
#region " Private "
private Graphics G;
private Rectangle TabRect;
#endregion
private Color FC = Color.Blue;
#region " Control "
public FirefoxMainTabControl()
{
DoubleBuffered = true;
ItemSize = new Size(43, 152);
Alignment = TabAlignment.Left;
SizeMode = TabSizeMode.Fixed;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
try
{
for (int i = 0; i <= TabPages.Count - 1; i++)
{
TabPages[i].BackColor = Color.White;
TabPages[i].ForeColor = Color.FromArgb(66, 79, 90);
TabPages[i].Font = Theme.GlobalFont(FontStyle.Regular, 10);
}
}
catch
{
}
}
protected override void OnPaint(PaintEventArgs e)
{
G = e.Graphics;
G.SmoothingMode = SmoothingMode.HighQuality;
G.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
base.OnPaint(e);
G.Clear(Color.FromArgb(66, 79, 90));
for (int i = 0; i <= TabPages.Count - 1; i++)
{
TabRect = GetTabRect(i);
if (SelectedIndex == i)
{
using (SolidBrush B = new SolidBrush(Color.FromArgb(52, 63, 72)))
{
G.FillRectangle(B, TabRect);
}
FC = Helpers.GreyColor(245);
using (SolidBrush B = new SolidBrush(Color.FromArgb(255, 175, 54)))
{
G.FillRectangle(B, new Rectangle(TabRect.Location.X - 3, TabRect.Location.Y + 1, 5, TabRect.Height - 2));
}
}
else
{
FC = Helpers.GreyColor(192);
using (SolidBrush B = new SolidBrush(Color.FromArgb(66, 79, 90)))
{
G.FillRectangle(B, TabRect);
}
}
using (SolidBrush B = new SolidBrush(FC))
{
G.DrawString(TabPages[i].Text, Theme.GlobalFont(FontStyle.Regular, 10), B, new Point(TabRect.X + 50, TabRect.Y + 12));
}
if ((ImageList != null))
{
if (!(TabPages[i].ImageIndex < 0))
{
G.DrawImage(ImageList.Images[TabPages[i].ImageIndex], new Rectangle(TabRect.X + 19, TabRect.Y + ((TabRect.Height / 2) - 10), 18, 18));
}
}
}
}
#endregion
}
Here is an odd one and it will be a bit long to describe so bear with me on this one.
I have a control made for c# 4 client profile called Toggleswitch (tsw). This toggleswitch is design to act like the metro switch, and that is does splendidly ... until it is added to a Tap control/Tap page then :
the rendering acts all weird not drawing the background properly and the on/off label don’t show up. This affects other controls as well, it like the Whole form won't draw.
also both VS 2010 and 2013 design editors properties fields freeze up so I can't access the other controls
and if left in normal the program won't build ... basically crashed VS..
BUT, if I change the tap control from normal to either flatbutton or button then it stops and runs smoothly as always and that is fine, but then I started wondering why it don't render in the normal
So here is what I have tried
the tsw extends Buttonbase for clickability. So I changed that to control then the OnPaint gets called in a unlimited loop so that don’t help, also the click don’t Work but that is less important right now
I extended of button, no help there still weird
override every single method from both button and buttonbase to see if that helped still nothing.
After looking at the forms designer I found that the only property the gets changed is the UseVisualStyleBackColor when false it works when true it don't, so I made an override that forces it to be false, still doesn't work :/
I even tried to remove the partial keyword.
I spend half a day reading about the defriend methods of both tap and button controls
even found a custom control test the extended of button that worked fine, but there was no reason for it because the only different from what I could see was an if statement in the OnPaint about some rendering.
.... Nothing works ....
Here is the togglewitch in its entirety:
public partial class ToggleSwith : ButtonBase
{
object _Lock = new object();
private Color _OffColor = SystemColors.ControlDarkDark;
private Color _OnColor = Color.LimeGreen;
private bool _ColorText = false;
// private bool _BeenDrawn = false;
private bool _Checked = false;
[Browsable(false)]
public override string Text { get { return null; } }
[Browsable(false)]
public override bool AutoSize{get{return false;}}
[Browsable(false)]
public override ContentAlignment TextAlign { get { return ContentAlignment.MiddleLeft; } }
[Category("Appearance")]
public virtual Color OffColor { get { return _OffColor; } set { _OffColor = value; Invalidate(); } }
[Category("Appearance")]
public virtual Color OnColor { get { return _OnColor; } set { _OnColor = value; Invalidate(); } }
[Category("Appearance")]
public virtual bool ColorText { get { return _ColorText; } set { _ColorText = value; Invalidate(); } }
[Category("Appearance")]
public virtual bool Checked { get { return _Checked; } set { _Checked = value; Invalidate(); } }
new public bool UseVisualStyleBackColor { get { return false; } set{} }
protected override Size DefaultSize { get { return new Size(80, 18); } }
protected override Size DefaultMinimumSize { get { return new Size(40, 13); } }
public event EventHandler ToggleChanged;
public ToggleSwith()
{
this.Size = new Size(80, 18);
InitializeComponent();
this.Click += ToggleSwith_Click;
}
void ToggleSwith_Click(object sender, EventArgs e)
{
lock (_Lock)
{
if (_Checked) _Checked = false;
else _Checked = true;
}
OnToggleChanged();
}
private void OnToggleChanged()
{
if (ToggleChanged != null)
{
EventArgs ea = new EventArgs();
ToggleChanged(this, ea);
}
}
protected override void OnPaint(PaintEventArgs e)
{
this.Controls.Clear();
int toggleblocsize=(int)this.Size.Width/8;
int indent = 23;
int labelx = 2;
// Declare and instantiate a new pen.
SolidBrush OffPen = new SolidBrush(this.OffColor);
SolidBrush OnPen = new SolidBrush(this.OnColor);
SolidBrush BackgroundPen = new SolidBrush(this.BackColor);
SolidBrush BlackPen = new SolidBrush(SystemColors.ControlText);
Pen ControlDarkPen = new Pen(SystemColors.ControlDark);
Pen ControlPen = new Pen(SystemColors.Control);
e.Graphics.FillRectangle(BackgroundPen,0, 0, this.Size.Width,this.Size.Height);
e.Graphics.DrawRectangle(ControlDarkPen, indent, -0, this.Size.Width - (indent+1), this.Size.Height-1);
Label l = new Label();
l.Font = this.Font;
l.Size = new Size((indent-labelx),this.Size.Height);
// if(this.Size.Height <13)l.Location= new Point(labelx,-1);
// else l.Location = new Point(labelx, 0);
l.TextAlign = TextAlign;
l.ForeColor = this.ForeColor;
if (this._Checked)
{
//ligth
e.Graphics.FillRectangle(OnPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, this.Size.Width - (toggleblocsize+1), -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(BlackPen, this.Size.Width - (toggleblocsize), -1, this.Size.Width, this.Size.Height + 1);
if (ColorText) l.ForeColor = OnColor;
l.Text = "On";
}
else
{
//ligth
e.Graphics.FillRectangle(OffPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, indent , -0, (toggleblocsize + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(BlackPen, (indent+1) , -1,(toggleblocsize), this.Size.Height + 1);
if (ColorText) l.ForeColor = this.ForeColor;
l.Text = "Off";
}
this.Controls.Add(l);
}
public override Size GetPreferredSize(Size proposedSize)
{
return new Size(80, 18);
}
}
It is not that I can't live with a flat tapcontrol it is just the why it doesn't work that bugs me ...
Any help would be greatly appreciated :)
Lastly some visual documentation of the problems:
Here is how it should work:
This is with a tap on normal
Got it fixed so that it now work, turns out that there was a few things wrong.
First of the base class was wrong, it should be a control
public class ToggleSwith : Control
and the on paint event changed to this
protected override void OnPaint(PaintEventArgs e)
{
//this.Controls.Clear();
int toggleblocsize = (int)this.Size.Width / 6;
int indent = 23;
int labelx = 2;
// Declare and instantiate a new pen.
SolidBrush OffPen = new SolidBrush(this.OffColor);
SolidBrush OnPen = new SolidBrush(this.OnColor);
Rectangle texttangle = new Rectangle(new Point(labelx, 0), new Size(indent - labelx, this.Size.Height));
SolidBrush BackgroundBrush = new SolidBrush(this.Parent.BackColor);
SolidBrush ToggleBrush = new SolidBrush(SystemColors.ControlText);
SolidBrush TextBrush = new SolidBrush(SystemColors.ControlText);
Pen ControlDarkPen = new Pen(SystemColors.ControlDark);
Pen ControlPen = new Pen(SystemColors.Control);
e.Graphics.FillRectangle(BackgroundBrush, 0, 0, this.Size.Width, this.Size.Height);
e.Graphics.FillRectangle(new SolidBrush(BackColor), indent, 0, this.Size.Width - indent, this.Size.Height);
e.Graphics.DrawRectangle(ControlDarkPen, indent, -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
if (!this.Enabled)
{
OffPen = new SolidBrush(SystemColors.Control);
OnPen = new SolidBrush(SystemColors.Control);
ToggleBrush = new SolidBrush(SystemColors.ControlDark);
TextBrush = new SolidBrush(SystemColors.Control);
}
if (this._Checked)
{
//ligth
e.Graphics.FillRectangle(OnPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, this.Size.Width - (toggleblocsize + 1), -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(ToggleBrush, this.Size.Width - (toggleblocsize), -1, this.Size.Width, this.Size.Height + 1);
if (ColorText && this.Enabled) TextBrush = new SolidBrush(OnColor);//_label.ForeColor = OnColor;
// _label.Text = "On";
// Draw string to screen.
TextRenderer.DrawText(e.Graphics, OnText, this.Font, texttangle, TextBrush.Color, this.Parent.BackColor,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.GlyphOverhangPadding);
}
else
{
//ligth
e.Graphics.FillRectangle(OffPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, indent, -0, (toggleblocsize + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(ToggleBrush, (indent + 1), -1, (toggleblocsize), this.Size.Height + 1);
if (ColorText && this.Enabled) TextBrush = new SolidBrush(SystemColors.ControlText);
// Draw string to screen.
TextRenderer.DrawText(e.Graphics, OffText, this.Font, texttangle, TextBrush.Color, this.Parent.BackColor,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.GlyphOverhangPadding);
}
that got the graphics result i was looking for.
A menu tool strip is used in WinForm application. On checking menu option, it opens sub menus. When mouse enters in the boundary of sub menu the back color is change to green. Now, I want to change this color to red, when mouse leave the boundary of sub-menu.
Any suggestions ?
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush;
Rectangle r = new Rectangle(this.Bounds.Width - 20, 2, 16, 17);
// If click on Del(Close Icon)
if (bOnDel)
{
brush = new SolidBrush(Color.LightBlue);
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.Blue;
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}
// If didn't click on Del(Close Icone)
if (!bOnDel)
{
brush = new SolidBrush(Color.FromKnownColor(KnownColor.Transparent));
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.FromKnownColor(KnownColor.Transparent);
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}
//Code for Drawing Cross Lines
brush = new SolidBrush(Color.Gray);
Rectangle rCross = new Rectangle(this.Bounds.Width - 15, 8, 6, 6);
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Right, rCross.Top), new Point(rCross.Left, rCross.Bottom));
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Left, rCross.Top), new Point(rCross.Right, rCross.Bottom));
}
Use the MouseLeave event for the ToolStripMenuItem to change the BackColor Property:
private void yourToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).BackColor = Color.Red;
}
You can look into using the MouseMove Event, make sure that your rectangle is declared outside of the Paint Event and Invalidate the Control using the Rectangle as the region. Here is an example base on your code, I declared a boolean entered and rectangle r in the beginning of the Class. You would put any highlight changes in your paint event. This is more like what I think you want.
public partial class CustomControl1 : ToolStripMenuItem
{
Rectangle r;
bool entered;
public CustomControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush;
r = new Rectangle(this.Bounds.Width - 20, 2, 16, 17);
// If MouseEnter Del(Close Icon)
if (entered)
{
brush = new SolidBrush(Color.LightBlue);
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.Blue;
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}
// If Mouse Not Entered Del(Close Icone)
if (!entered)
{
brush = new SolidBrush(Color.FromKnownColor(KnownColor.Transparent));
e.Graphics.FillRectangle(brush, r);
brush.Color = Color.FromKnownColor(KnownColor.Transparent);
e.Graphics.DrawRectangle(new Pen(brush, 1), r);
}
//Code for Drawing Cross Lines
brush = new SolidBrush(Color.Gray);
Rectangle rCross = new Rectangle(this.Bounds.Width - 15, 8, 6, 6);
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Right, rCross.Top), new Point(rCross.Left, rCross.Bottom));
e.Graphics.DrawLine(new Pen(brush, 2), new Point(rCross.Left, rCross.Top), new Point(rCross.Right, rCross.Bottom));
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (r.Contains(e.X, e.Y) && !entered)
{
entered = true;
Invalidate(r);
}
else if (!r.Contains(e.X, e.Y) && entered)
{
entered = false;
Invalidate(r);
}
}
}
I need to create a dropdown menu, or combobox, for a Windows Forms application which contains a small image and then a string of text next to it. Basically, you can think of each 'row' in the dropdown as needing to have an icon and then the name of the icon to the right of the icon. I am having trouble doing this -- in fact, I've been completely unsuccessful. Does anyone know of a way to accomplish this task? Any help will be greatly appreciated. Thanks!
I was able to come up with some very simple code to perform this (see snippet below). The code creates a control that is a dropdown control which shows a small colored square and that color's name in the same row (see photo). Thanks for the links provided for this back when it was originally posted! Hopefully this control can help someone else out in the future.
Image:
Code:
class ColorSelector : ComboBox
{
public ColorSelector()
{
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
}
// Draws the items into the ColorSelector object
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
DropDownItem item = new DropDownItem(Items[e.Index].ToString());
// Draw the colored 16 x 16 square
e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
// Draw the value (in this case, the color name)
e.Graphics.DrawString(item.Value, e.Font, new
SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
base.OnDrawItem(e);
}
}
public class DropDownItem
{
public string Value
{
get { return value; }
set { this.value = value; }
}
private string value;
public Image Image
{
get { return img; }
set { img = value; }
}
private Image img;
public DropDownItem() : this("")
{}
public DropDownItem(string val)
{
value = val;
this.img = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(img);
Brush b = new SolidBrush(Color.FromName(val));
g.DrawRectangle(Pens.White, 0, 0, img.Width, img.Height);
g.FillRectangle(b, 1, 1, img.Width - 1, img.Height - 1);
}
public override string ToString()
{
return value;
}
}
Very helpful..
some optimisations :
public sealed class ColorSelector : ComboBox
{
public ColorSelector()
{
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index >= 0 && e.Index < Items.Count)
{
DropDownItem item = (DropDownItem)Items[e.Index];
e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
}
base.OnDrawItem(e);
}
}
and ...
public sealed class DropDownItem
{
public string Value { get; set; }
public Image Image { get; set; }
public DropDownItem()
: this("")
{ }
public DropDownItem(string val)
{
Value = val;
Image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(Image))
{
using (Brush b = new SolidBrush(Color.FromName(val)))
{
g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
}
}
}
public override string ToString()
{
return Value;
}
}
NOTE: This code is from user que dal's optimization
If you wish to have a string that isn't just the name of the color, change DropDownItem to have 2 arguments, the string and the color, then just change how the brush sets the color, as such:
public DropDownItem(string val, Color color)
{
Value = val;
Image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(Image))
{
using (Brush b = new SolidBrush(color))
{
g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
}
}
}
You must then change the dropdown item as such:
public DropDownItem()
: this("", Color.Empty)
{}
Hope this was helpful :)
I resolved the problem, i did this:
ComboBox MarcadorNS = new ComboBox();
MarcadorNS.Height = 30;
MarcadorNS.Width = 150;
MarcadorNS.SelectedValuePath = "Uid";
foreach (var temporalItem in GetPredefinedKinds())
{
Image ImagenCombo = new Image();
ImagenCombo.Source =
new BitmapImage(new Uri(
"Imagenes/Marcadores/" +
temporalItem.Name.ToLower() + ".png", UriKind.Absolute));
ImagenCombo.Height = 28;
ImagenCombo.Width = 28;
ImagenCombo.VerticalAlignment = VerticalAlignment.Top;
ImagenCombo.HorizontalAlignment = HorizontalAlignment.Left;
Label textoCombo = new Label();
textoCombo.VerticalAlignment = VerticalAlignment.Top;
textoCombo.HorizontalAlignment = HorizontalAlignment.Left;
textoCombo.Content = BaseDatos.NombresDeMarcadores(temporalItem.ToString());
Grid GridCombo = new Grid();
GridCombo.Uid = ObtenerMarcador(temporalItem.ToString());
StackPanel stackCombo = new StackPanel();
stackCombo.Orientation = Orientation.Horizontal;
stackCombo.Children.Add(ImagenCombo);
stackCombo.Children.Add(textoCombo);
GridCombo.Children.Add(stackCombo);
MarcadorNS.Items.Add(GridCombo);
}
Not sure about images but this should work for the strings:
comboBox.Items.Add("String");