Solved

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

Posted on 2009-05-08
7
870 Views
Last Modified: 2013-12-14
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);
        }
0
Comment
Question by:jongund
  • 5
7 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24346910
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 ?
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24346931
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

0
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 500 total points
ID: 24346966
OK, I've checked, I was right : depending on the color you choose you have to redraw the string accordingly. The following code gives you an example.
The previous code I posted give you the possibility to add some transparency.
The following MSDN link give you the oportunity to go deeper and create your custom listbox if you want to have more controle on drawing stuffs but it seems that you don't want to go so deep:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.aspx
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);
    g.DrawString(listBox1.Items[i].ToString(), this.Font, Brushes.Black, rc);
}

Open in new window

0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24381826
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 ?
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24553841
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Aptana 3.6 Preview Problem On Mac 35 126
Java eclipse error 2 70
Nest Related files not working in Visual Studio 4 77
Java Help 2 85
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question