简体   繁体   English

使用C#WinForms突出显示所选的自定义控件

[英]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. 因此,我计划为这两个事件实现Windows风格。 For focus I have the control drawing a solid and a dotted line around the control using the following code in the Paint event. 为了获得焦点,我使控件在Paint事件中使用以下代码在控件周围绘制了实线和虚线。

 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). 对于Focus事件,我只希望突出显示图像(类似于Windows资源管理器)。 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 . 类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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