Link to home
Start Free TrialLog in
Avatar of Meir Rivkin
Meir RivkinFlag for Israel

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.DrawItemEventArgs 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(strFilterIndex, e.Font, new SolidBrush(Color.Black), e.Bounds );
            }

            private void OnSelectedIndexChanged(object 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
Avatar of smegghead
smegghead
Flag of United Kingdom of Great Britain and Northern Ireland image

rather than using events to draw the items.. try overriding the functions..

I've done this in the past and used this method.

            protected override void OnPaint(PaintEventArgs e)
            {
                  e.Graphics.FillRectangle(Brushes.Brown,this.ClientRectangle);
            }
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                  e.Graphics.FillRectangle(Brushes.Red,this.ClientRectangle);
            }
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                  // Your code
            }
and obviously inherit your class from the ComboBox
Avatar of Meir Rivkin

ASKER

what about invertRect?
     ControlPaint.FillReversibleRectangle(UseRect,Color.Yellow);
      ControlPaint.DrawReversibleFrame(UseRect,Color.Black,FrameStyle.Dashed);

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.
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
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;
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
what would you use if the color was

128,128,128 ??
right, thats good enough
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.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial