简体   繁体   中英

Highlight selected custom control with C# WinForms

I have created a custom control (the control is used for drag and drop) and I want to add focus and selected events to the control. Both need to be visually distinct. So I plan to implement a windows style for both of these events. For focus I have the control drawing a solid and a dotted line around the control using the following code in the Paint event.

 if (Image != null)
     {            
        if (ContainsFocus)
        {
           // Draw a dotted line inside the client rectangle
           Rectangle insideRectangle = ClientRectangle;
           insideRectangle.Inflate(-2, -2);
           insideRectangle.Width--;
           insideRectangle.Height--;
           Pen p = new Pen(Color.Black, 1);
           p.DashStyle = DashStyle.Dot;
           g.DrawRectangle(p, insideRectangle);

           // Draw a solid line on the edge of the client rectangle
           Rectangle outsideRectangle = ClientRectangle;
           outsideRectangle.Width--;
           outsideRectangle.Height--;               
           p.DashStyle = DashStyle.Solid;
           g.DrawRectangle(p, outsideRectangle);

           Color transparentLightBlue = Color.FromArgb(100, Color.LightBlue);
           Brush solidBrush = new SolidBrush(transparentLightBlue);
           g.FillRectangle(solidBrush, ClientRectangle);
        }            
     }

For the Focus event I want just the image to be highlighted (similar to windows explorer). My first attempt at this was to add the following code.

Color transparentLightBlue = Color.FromArgb(100, Color.LightBlue);
Brush solidBrush = new SolidBrush(transparentLightBlue);
g.FillRectangle(solidBrush, ClientRectangle);

This works filling in the rectangle however I would like to just highlight the image itself instead of the entire rectangle. I've had the idea of using two different images, however the image is supplied to me and I'm not storing them.

So my question: How is the best way to get just the image of the control that has focus to highlight?

Thank you in advance!

since your image is not transparent you could overlay it with a transparent highlight color. something similar to this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM