Hot Track Effect for Windows Form UserControl

What is the easiest way to implement a UserControl that mimics the effect of a "hot track" when drawing a border around itself when a mouse moves over it?

I tried unsuccessfully to inherit a custom control that overrides the OnMouseHover event that draws the border (it seems like the event is not firing)

Thank!

+5
source share
2 answers

You can simulate the appearance of a border around your user control by using the carefully installed Panel control in your UC and monitoring the position of the mouse while tracking low-level Windows messages.

On the UserControl design surface, add a panel and panel size so that only a small portion of the UC design surface is visible (see note * below). The visible part of the design surface will be your colored frame, so set its "thickness" accordingly. Add the other controls that make up your UC in the panel.

Your control might look like this:
enter image description here

IMessageFilter UC. PreFilterMessage() UC UC BackColor , UC, , . BackColor Panel , , UC, ahs border.

, :

public partial class UserControl1 : UserControl, IMessageFilter
{
    public UserControl1() {
        InitializeComponent();
        Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
        if (!this.IsDisposed && this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition))) {
            this.BackColor = Color.Green; // Or whatever border color you want.
        } else {
            this.BackColor = SystemColors.Control;  // Back to the UC default border color.
        }
        return false;
   }
}

. , Panel BackColor, . , BackColor , (Control).

+2

"MouseEnter" "MouseLeave", BorderStyle ( ). MouseEnter , , MouseLeave , . MouseHover , , .

0

All Articles