Link to home
Start Free TrialLog in
Avatar of jongund
jongund

asked on

creating custom highlight/selection colors with windows from controls in C# and .NET

I am trying to create custom highlight colors for menus and listboxes.  So as the selection movs my colors are used to highlight instead of the system colors.

Thank you for any help you can provide.

I tried this but it all it does is over cover up the highlighted element with th enew color, cannot ready the text anymore:

   private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBox1.SelectedIndex;
            Rectangle rc = listBox1.GetItemRectangle(i);
            SolidBrush myBrush = new SolidBrush(Color.Beige);
            Graphics g = Graphics.FromHwnd(listBox1.Handle);
            g.FillRectangle(myBrush, rc);
        }
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Did you have a look to the Graphics class and its methods to understand what is going on ?... or don't you think that using something else than a SolidBrush could solve the issue ?
It is probably because the default behavior of the listbox is to draw the text in white when selected.
Perhaps you have to redraw the text as well depending on the colors you choose.
For example if you write the following code to make the color transparent you'll see something:
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBox1.SelectedIndex;
            Rectangle rc = listBox1.GetItemRectangle(i);
            Color customColor = Color.FromArgb(90, Color.Red);
            SolidBrush myBrush = new SolidBrush(customColor);
            Graphics g = Graphics.FromHwnd(listBox1.Handle);
            g.FillRectangle(myBrush, rc);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg 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
Hey, 4 days ago I told you that you just have to add this line of code:
g.DrawString(listBox1.Items[i].ToString(), this.Font, Brushes.Black, rc);
May you at least tell us if you agree with this solution so that other people who may be interested by this issue could know if this working ?
Dispite my solution works (ID#24346966), jongund didn't take the time to answer. I re-ask him to confirm that. Nevertheless he didn't care.
Since my solution works, I should get the points. After all he got what he needed.