c#悬停时在控件周围绘制矩形

时间:2016-02-09 04:42:41

标签: c# winforms

我重复了OnPaint类的Control方法来创建表情符号。表情符号已经创建。但是当我徘徊时,我会在它周围添加一个矩形。

public class EmoticonControl : Control
{
    public EmoticonControl(string unicode, Image image)
    {
        this.unicode = unicode;
        this.image = image;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Rectangle clipRectangle = e.ClipRectangle;
        graphics.DrawImage(this.image, 0, 0, this.Size.Width, this.Size.Height);

        // When hovered
        Brush brush = new SolidBrush(this.selectedColor);
        graphics.FillRectangle(new SolidBrush(selectionColor), clipRectangle);
        graphics.DrawRectangle(new Pen(selectionBorderColor), clipRectangle.X, clipRectangle.Y, clipRectangle.Width - 1, clipRectangle.Height - 1);
        //
    }
    private string unicode;
    private Image image;

    private Color selectedColor = SystemColors.Highlight;
    private Color selectionColor = Color.FromArgb(50, 0, 0, 150);
    private Color selectionBorderColor = SystemColors.Highlight;
}

3 个答案:

答案 0 :(得分:2)

您可以使用UIElement.IsMouseOver属性来了解您的控件是否悬停。

protected override void OnPaint(PaintEventArgs e)
{
    Graphics graphics = e.Graphics;
    Rectangle clipRectangle = e.ClipRectangle;
    graphics.DrawImage(this.image, 0, 0, this.Size.Width, this.Size.Height);

    // When hovered
    if (IsMouseOver)
    {
        Brush brush = new SolidBrush(this.selectedColor);
        graphics.FillRectangle(new SolidBrush(selectionColor), clipRectangle);
        graphics.DrawRectangle(new Pen(selectionBorderColor), clipRectangle.X, clipRectangle.Y, clipRectangle.Width - 1, clipRectangle.Height - 1);
    }
}

答案 1 :(得分:1)

原始版本

这应该适合你:

public class EmoticonControl : Control
{
    public EmoticonControl(string unicode, Image image)
    {
        this.unicode = unicode;
        this.image = image;
    }

    // Set hover, request invalidation:
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        hover = true;
        this.Invalidate();
    }

    // Unset hover, request invalidation:
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        hover = false;
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Rectangle clipRectangle = e.ClipRectangle;
        graphics.DrawImage(this.image, 0, 0, this.Size.Width, this.Size.Height);

        // Only do this when hovering:
        if (hover)
        {
            Brush brush = new SolidBrush(this.selectedColor);
            graphics.FillRectangle(new SolidBrush(selectionColor), clipRectangle);
            graphics.DrawRectangle(new Pen(selectionBorderColor), clipRectangle.X, clipRectangle.Y, clipRectangle.Width - 1, clipRectangle.Height - 1);
        }
    }

    // Added flag to track hover status:
    private bool hover = false;

    private string unicode;
    private Image image;

    private Color selectedColor = SystemColors.Highlight;
    private Color selectionColor = Color.FromArgb(50, 0, 0, 150);
    private Color selectionBorderColor = SystemColors.Highlight;
}

更新版本:

(包括一些"最佳做法"修复原始代码)

public class EmoticonControl : Control
{       
    public EmoticonControl(string unicode, Image image)
    {
        this.unicode = unicode;
        this.image = image;
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        hover = true;
        this.Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        hover = false;
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        DrawImage(e.Graphics);
        DrawSelectionRectangle(e.Graphics);
    }

    private void DrawSelectionRectangle(Graphics graphics)
    {
        if (hover)
        {
            using (var brush = new SolidBrush(selectionColor))
            {
                graphics.FillRectangle(brush, this.ClientRectangle);
            }
            using (var pen = new Pen(selectionBorderColor))
            {
                graphics.DrawRectangle(pen,
                    new Rectangle()
                    {
                        Width = (this.Width - 1),
                        Height = (this.Height - 1)
                    });
            }
        }
    }

    private void DrawImage(Graphics graphics)
    {
        if (image != null) graphics.DrawImage(
            this.image, 0, 0, this.Size.Width, this.Size.Height);
    }

    private bool hover = false;
    private Image image;
    private string unicode;

    private Color selectionColor = Color.FromArgb(50, 0, 0, 150);
    private Color selectionBorderColor = SystemColors.Highlight;
}

答案 2 :(得分:0)

您可能需要在鼠标离开时设置一个标志并输入事件并检查绘图事件中的标志,如下所示:

If(hover)
{
    ControlPaint.DrawBorder(e.Graphics,this.DisplayRectangle,Color.Red,ButtonBorderStyle.Solid)
}