簡體   English   中英

使用C#WinForms突出顯示所選的自定義控件

[英]Highlight selected custom control with C# WinForms

我創建了一個自定義控件(該控件用於拖放),我想向該控件添加焦點和選定的事件。 兩者都需要在視覺上區分開。 因此,我計划為這兩個事件實現Windows風格。 為了獲得焦點,我使控件在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);
        }            
     }

對於Focus事件,我只希望突出顯示圖像(類似於Windows資源管理器)。 我對此的第一次嘗試是添加以下代碼。

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

這可以填充矩形,但是我只想突出顯示圖像本身而不是整個矩形。 我曾想過使用兩個不同的圖像,但是圖像是提供給我的,我沒有存儲它們。

所以我的問題是:如何獲得重點突出的控件圖像的最佳方法是什么?

先感謝您!

由於您的圖片不是透明的,因此可以用透明的高光顏色覆蓋它。 類似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM