views:

962

answers:

1

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!

+1  A: 

Look at my post here.

arbiter