Meir Rivkin
asked on
color combo
hi,
i have a custom draw combo box which displayes colors and text.
the code lookes like this:
public ColorComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler( OnDrawItem );
this.DropDown += new System.EventHandler( OnDropDown );
this.SelectedIndexChanged += new System.EventHandler( OnSelectedIndexChanged );
}
private void OnDropDown(object sender, System.EventArgs e)
{
this.Text = "";
this.ForeColor = this.BackColor = (Color)SelectedItem;
}
private void OnDrawItem(object sender, System.Windows.Forms.DrawI temEventAr gs e)
{
Graphics grfx = e.Graphics;
Color brushColor = (Color)this.Items[ e.Index ];
SolidBrush brush = new SolidBrush( brushColor );
grfx.FillRectangle(brush, e.Bounds);
string strFilterIndex = "filter" + e.Index.ToString();
grfx.DrawString(strFilterI ndex, e.Font, new SolidBrush(Color.Black), e.Bounds );
}
private void OnSelectedIndexChanged(obj ect sender, System.EventArgs e)
{
this.Text = "";
this.ForeColor = this.BackColor = (Color)SelectedItem;
}
public Color SelectedColor
{
get
{
return this.BackColor;
}
set
{
this.BackColor = value;
}
}
i have 2 problems:
1. the combo style is DropDown, and for some reason when i pick a color from the combobox, the name of the color is being displayed (only when i select it).
for example: Color[Red]
2. i use DrawString to display the word "filter" on each one of the colors, i wanna to invert the text color (currently they are all black).
i familiar with the function user32.dll but i couldn't get it to work.
cheers
i have a custom draw combo box which displayes colors and text.
the code lookes like this:
public ColorComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler( OnDrawItem );
this.DropDown += new System.EventHandler( OnDropDown );
this.SelectedIndexChanged += new System.EventHandler( OnSelectedIndexChanged );
}
private void OnDropDown(object sender, System.EventArgs e)
{
this.Text = "";
this.ForeColor = this.BackColor = (Color)SelectedItem;
}
private void OnDrawItem(object sender, System.Windows.Forms.DrawI
{
Graphics grfx = e.Graphics;
Color brushColor = (Color)this.Items[ e.Index ];
SolidBrush brush = new SolidBrush( brushColor );
grfx.FillRectangle(brush, e.Bounds);
string strFilterIndex = "filter" + e.Index.ToString();
grfx.DrawString(strFilterI
}
private void OnSelectedIndexChanged(obj
{
this.Text = "";
this.ForeColor = this.BackColor = (Color)SelectedItem;
}
public Color SelectedColor
{
get
{
return this.BackColor;
}
set
{
this.BackColor = value;
}
}
i have 2 problems:
1. the combo style is DropDown, and for some reason when i pick a color from the combobox, the name of the color is being displayed (only when i select it).
for example: Color[Red]
2. i use DrawString to display the word "filter" on each one of the colors, i wanna to invert the text color (currently they are all black).
i familiar with the function user32.dll but i couldn't get it to work.
cheers
and obviously inherit your class from the ComboBox
ASKER
what about invertRect?
ControlPaint.FillReversibl eRectangle (UseRect,C olor.Yello w);
ControlPaint.DrawReversibl eFrame(Use Rect,Color .Black,Fra meStyle.Da shed);
This inverts the rectangle in 'UseRect' and also ensures that the colour specifier in the 2nd parameter will be visible against the new background.
It also draws a dashed border around the rectangle, to make it appear selected.
Smg.
ControlPaint.DrawReversibl
This inverts the rectangle in 'UseRect' and also ensures that the colour specifier in the 2nd parameter will be visible against the new background.
It also draws a dashed border around the rectangle, to make it appear selected.
Smg.
ASKER
smegghead : overriding the functions doesn't change a bit.
regarding inverRect, i wanna invert just the text color so if the item painted black the text will be white etc.
cheers
regarding inverRect, i wanna invert just the text color so if the item painted black the text will be white etc.
cheers
why don't you just do a simple calculation on the RGB elements of the background colour to determine the brightness of the colour..
like
if ((R+G+B)>(128*3)) Fore=Color.Black;else Fore=Color.White;
like
if ((R+G+B)>(128*3)) Fore=Color.Black;else Fore=Color.White;
ASKER
cause i want to display smarter color invertion, for example:
if the background is blue[0,0,255] the text color will be yellow[255,255,0]
if the background is cyan[0,255,255] the text color will be red[255,0,0]
always compliment to 255...
cheers
if the background is blue[0,0,255] the text color will be yellow[255,255,0]
if the background is cyan[0,255,255] the text color will be red[255,0,0]
always compliment to 255...
cheers
what would you use if the color was
128,128,128 ??
128,128,128 ??
ASKER
right, thats good enough
ASKER
what about the 1st problem, when the combo displayes the color name on the rectangle's item?
its weird cause i'm not using any other DrawString or something like that in my code.
its weird cause i'm not using any other DrawString or something like that in my code.
if you email me your code I'll have a look at it.. my email is in my profile.
From Raymond Chen blog (Micro$oft guru):
Why highlighting by inverting colors is a bad idea
http://blogs.msdn.com/oldnewthing/archive/2003/10/31/55518.aspx
Bob
Why highlighting by inverting colors is a bad idea
http://blogs.msdn.com/oldnewthing/archive/2003/10/31/55518.aspx
Bob
that was my point about 128,128,128 - 5 comments above.
I advised that he uses either black or white depending on the 'brightness' of the other color. (comment at 3:40pm)
Smg.
I advised that he uses either black or white depending on the 'brightness' of the other color. (comment at 3:40pm)
Smg.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I've done this in the past and used this method.
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(B
}
protected override void OnPaintBackground(PaintEve
{
e.Graphics.FillRectangle(B
}
protected override void OnDrawItem(DrawItemEventAr
{
// Your code
}